Computer Technology 2 Practice/Coding Flashcards
W6 Given the following:
The address of stackPtr is x:08
The address of tPtr is x:12
The address of tempPtr is x:24
The address of newPtr is x:16
The address of info is: x:20
Implement the following
Add node 1 : content 3; address is X:30
Add node 2: contents 6; address is X:36
Add node 3: contents 10; address is X:40
Delete all the nodes
Illustrate the stack by showing contents of tempPtr/stackPtr, newPtr and tPtr: after each of the above steps.
StackNode* stackPtr = NULL;
int value;
push(&stackPtr, value);
void push(StackNode* topPtr, int info)
StackNode newPtr = malloc(sizeof(StackNode));
// insert the node at stack top
if (newPtr != NULL) {
newPtr->data = info;
newPtr->nextPtr = *topPtr;
*topPtr = newPtr;
pop(&stackPtr));
int pop(StackNode* *topPtr)
StackNode* tempPtr = * topPtr;
int popValue = (* topPtr)->data;
* topPtr = (* topPtr)->nextPtr;
free(tempPtr);
W6 Explain, using examples (include node addresses), how to implement the push and pop functions of a stack (15 marks)
W6 Explain, using an example (include addresses), if this function would push a node on a stack (9 marks)
//fnt 1 call
Push (&StackPtr);
void push(StackNode** topPtr, int info)
{
StackNode newPtr = malloc(sizeof(StackNode));
if (newPtr != NULL) {
newPtr->data = info;
newPtr->nextPtr = topPtr;
topPtr = newPtr;
}
else {
printf(“%d not inserted. No memory available.\n”, info);
}
}
W6 Explain what the following code does and if it would pop a value from the stack (9 Marks)
// function to pop values from stack assuming it is not empty
int pop(StackNode** topPtr)
{
if (topPtr != Null){
StackNode* tempPtr = topPtr;
int popValue = (topPtr)->data;
topPtr = (topPtr)->nextPtr;
free(tempPtr);
return popValue;
}
else return ‘\0’;
W7 Explain, using examples (include node addresses), how to implement the enqueue and dequeue functions of a queue (15 marks)
W7 Explain if following Queue “enqueue” function will add a node to a queue (10 marks)
void enqueue(QueueNode* * headPtr, QueueNode* * tailPtr, char value)
{
QueueNodeP*newPtr = malloc(sizeof(QueueNode));
if (newPtr != NULL) {
newPtr->data = value;
newPtr->nextPtr = NULL;
if (isEmpty(*headPtr)) { // function to check if queue is empty headPtr = newPtr; } else { (tailPtr)->nextPtr = newPtr; } tailPtr = newPtr; } else { printf("%c not inserted. No memory available.\n", value); } }
W7 Explain what the following code does and if it will not dequeue successfully (10 marks)
char fnt1(QueueNode** headPtr, QueueNode** tailPtr)
{
if (headPtr != NULL){
char value = (headPtr)->data;
QueueNode* tempPtr = headPtr;
headPtr = (*headPtr)->nextPtr;
if (*headPtr == NULL) {
tailPtr = NULL;
}
free(tempPtr);
return value;
}
Else
Printf(“bla bla bla….”);
}
W8 Explain, using an example such as “ls –l –a” how the following code using a combination of the fork(), wait() and exec() command can be used to run new processes. (12 marks)
if ((pid = fork()) < 0) { /* fork a child process /
printf(“** ERROR: forking child process failed\n”);
exit(42);
}
else if (pid == 0) { /* for the child process: /
if (execvp(argv, argv) < 0) { /* execute the command /
printf(“** ERROR: exec failed\n”);
exit(1);
}
}
else { /* for the parent: /
while (wait(&status) != pid) / wait for completion */
;
}
W8 Explain, in your own words, the above code. (argc, thread_create parameters, thread join and thread code: cast void*, calculation and why the result can be displayed in function main) (10 marks)
SLIDE 32 - THREADS
W8 What would be the output of the code for the following commandline argc: (4 marks)
./CLargthread 10
./Clargthread 5 12
./CLargthread
SLIDE 32 - THREADS
W8 Explain, using sample output, what will happen if the join statement is removed and the commandline arguments are:
./Clargthread 5 (4 marks)
SLIDE 32 - THREADS
W9 Producer/consumer problem. Explain does this code prevent/result in deadlock (8 marks)
SLIDE 38
W10 Explain, using conventional deadlock modelling, if the following schedule result in deadlock: (6 marks)
SLIDE 35
W10 Fig A and Fig B show directed resource graphs for three processes (P1, P2 and P3 and three sources (R1, R2, R3). Using the detection/reduction algorithm Explain: For each of the above diagrams show: (14 marks)
If Is it deadlocked?
What is the result after reduction?
Which, if any processes are deadlocked
SLIDE 36
W10 Using Bankers algorithm answer the following:
How many resources of type A, B, C and D are there?
What are the contents of the Need matrix?
Is the system in a safe state? Provide reasoning for your answer (show the sequence in which the processes would finish)
If a request from process P2 arrives for additional resources of {0, 2, 0, 0}, can the Bankers algorithm grant the request immediately? Provide reasoning for your answer. (14 Marks)
SLIDE 37