You are on page 1of 2

CSE 3320 Quiz 2 (Fall 2021)

4-4:40pm, 10/6 (Wednesday)


Name:
Note:
(1) Upload your answers in a pdf file in Canvas “Assignments”.
(2) Only briefly cover key points.
(3) No collaborations!

1. Read this process state diagram and answer questions: (15 points)

(1) Is this statement true: “When a process in the “running” state executes a system call, it enters the
kernel mode and moves from the “running” state to either “waiting” state or “ready” state .”? Show
your reason.
(2) A “running” process may transition to the “ready” state in response to an “interrupt”. Why is this
transition necessary?
(3) When a child process is created, what’s its state?

2. Read this code and answer questions: (25 points)

#include <stdio.h>
#include <sys/types.h>
int main()
{
fork();
fork();
printf("hello\n");
return 0;
}
(1) What’s the output of this program’s execution?
(2) In the program’s execution, initially there is only process, which is named root process. Any
processes created by the root process are named child processes. And any processes
created by the child processes are named grandchild processes. Rewrite the code so that
each process can correctly identify itself in the “printf("");” with output such as “hello from
the root”, “hello from a child”, or “hello from a grandchild”. (Hint: fork()’s return value is zero
if it returns to the newly created child process; and is a positive number if it returns to the
parent (the caller). The number is the process ID of newly created child process.)
3. In a ticket sale application there is a shared variable recording number of tickets currently
available for sale (“num_tickets”). Initially there are 5 tickets (“num_tickets = 5;”). There are 5 threads
in the program repeatedly executing the following code to sell the available tickets. (20 points)
if (num_tickets > 0) {
num_tickets---,
sell a ticket;
}
(1) Is it possible that one of the threads sells all of the five tickets and the other threads have nothing
to sell?
(2) May this program cause overselling of the tickets (e.g., sell 6 tickets while only 5 tickets are
initially available)? If yes, explain how this could happen?
(3) At most how many tickets could be oversold? Show your reason.

4. What’s the drawback of a spinlock (busy waiting)? Any methods to address the issue? (10 points)

5. We may use mutex for synchronization in a multi-producer-multi-consumer problem. Read the following
code: (30 points)

/* Initialization */
int in = 0; //index at which producer will put the next data
int out = 0; //index from which the consumer will consume next data
Int count = 0; // Number of data currently in the buffer
int buffer[BUFFER_SIZE];
Mutex mutex;
/* Producer Code */
while (true) {
// produce an item
/* an alternate place for mutex_lock(&mutex); */
while (count == BUFFER_SIZE)
; /* do nothing */
mutex_lock(&mutex);
buffer[in] = item;
in = (in + 1) % BUFFER_SIZE;
count++;
mutex_unlock(&mutex);

}
/* Consumer Code */
while (true) {
/* an alternate place for mutex_lock(&mutex); */
while (count == 0)
; /* do nothing */
mutex_lock(&mutex);
item = buffer[out];
out = (out + 1) % BUFFER_SIZE;
count--;
mutex_unlock(&mutex);
/* consume the item in next consumed */
}
(1) Is its use of mutex correct to ensure the integrity of the program? Show your reason.
(2) If we move the two mutex_lock(&mutex) statements to their respective alternate places in the
program (“/* an alternate place for mutex_lock(&mutex); */”), is there a problem with the
program? Show your reason.

You might also like