Problem 1. Consider the following code example for allocating and releasing, which can be concurrently called by multiple processes to fork new processes. #define MAX_PROCESSES 255 int number_of_processes = 0; // a shared variable /* the implementation of fork() calls this function */ int allocate_process() { int new_pid; if (number_of_processes == MAX_PROCESSES) return -1; else { /* code to allocate necessary process resources */ ++number_of_processes; return new_pid; } } /* the implementation of exit() calls this function */ void release_process() { /* code to release process resources */ --number_of_processes; } Using the Semaphore structure and its atomic wait() and signal() operations to solve a critical section (mutual exclusion) problem. To prevent race condition(s) and solve the critical section problem in the code given above, (Hints: 1. The code here is NOT all the same as an example used in class; 2. In this problem, the Semaphore software tool needs to be used as a Binary Semaphore (Case 1) and the use of a Binary Semaphore (Case 1) to solve a critical section (mutual exclusion) problem is illustrated in the in-class lecture notes.) 1. WRITE C statements to declare and initialize a Semaphore variable named proc_S using the Semaphore structure defined in Slide 3.25 2. RE-WRITE the allocate_process() function by calling wait() and/or signal()defined in Slide 3.26 3. RE-WRITE the release_process() function by calling wait() and/or signal() defined in Slide 3.26
Problem 1. Consider the following code example for allocating and releasing, which can be concurrently called by multiple processes
to fork new processes.
#define MAX_PROCESSES 255
int number_of_processes = 0; // a shared variable
/* the implementation of fork() calls this function */
int allocate_process() {
int new_pid;
if (number_of_processes == MAX_PROCESSES)
return -1;
else {
/* code to allocate necessary process resources */
++number_of_processes;
return new_pid;
}
}
/* the implementation of exit() calls this function */
void release_process() {
/* code to release process resources */
--number_of_processes;
}
Using the Semaphore structure and its atomic wait() and signal() operations to solve a critical section (mutual exclusion)
problem. To prevent race condition(s) and solve the critical section problem in the code given above,
(Hints: 1. The code here is NOT all the same as an example used in class; 2. In this problem, the Semaphore software tool needs to be
used as a Binary Semaphore (Case 1) and the use of a Binary Semaphore (Case 1) to solve a critical section (mutual exclusion)
problem is illustrated in the in-class lecture notes.)
1. WRITE C statements to declare and initialize a Semaphore variable named proc_S using the Semaphore structure defined in
Slide 3.25
2. RE-WRITE the allocate_process() function by calling wait() and/or signal()defined in Slide 3.26
3. RE-WRITE the release_process() function by calling wait() and/or signal() defined in Slide 3.26
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 2 images