Operating system structures:
Process control block:
Four processes are ready to run. Their CPU burst times are 2, 10, 2 and 6.
Consider the following SOS semaphore implementation
of a wait call (additions to system.h
shown on top):
/*--------------------------------------*/ /* extra system.h stuff */ #define NUM_SEMS 50 /* semaphore data structures */ struct semaphore { int allocated; int count; int use_count; int id; pid_queue queue; }; struct semaphore sema[NUM_SEMS]; /*--------------------------------------*/ #include < stdio.h > /** wait **/ int WaitSemaphore(int sid) { if (!sema[sid].allocated) return -1; /* sid is invalid */ sema[sid].count--; if (sema[sid].count < 0) { insert(sema[sid].queue, current_process); pd[current_process].state = BLOCKED; Dispatcher(); } return 0; }
Write the corresponding signal code for the below prototype:
int SignalSemaphore(int sid);
For the waitcode in previous problem, where might there be a race condition in the OS itself? What solutions might the OS use to avoid any race conditions in wait? Be specific.
Consider running the following psuedo-code on a Unix system:
/* global variable */ int n=0 main() { fork() n = n + 1 printf("%d ", n) }
Assume all system calls succeed.
fork()
call is replaced with a
spawn()
call that creates a new thread instead of
a new process. Now, what are the possible outputs?
Consider the test_and_set solution for a race condition we discussed in class:
int lock /* shared */ while (1) { while (test_and_set(lock)) /* no op */ ; /* critical region */ lock = false /* remainder */ }
Consider the dining philosopher's solution as discussed in class: Provide a solution that only allows N-1 philosophers to "sit" at the table (where N is the number of philosophers in the system). You may declare any additional variables or semaphores as needed.
Return to the CS3013 Home Page
Send all questions to the TA mailing list.