Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the following code example for allocating and releasing processes (i.e.

ID: 3861350 • Letter: C

Question

Consider the following code example for allocating and releasing processes (i.e., tracking number of processes), #define MAX_PROCS 255 int number_of_processes = 0;/* the implementation of fork() calls this function */int allocate_process () {int new_pid; if (number_of_processes == MAX_PROCS) return -1; else {/* allocate process resources and assign the PID to new_pid */++number_of_processes; return new_pid;}}/* the implementation of exit() calls this function */void release_process() {/* release process resources */--number_of_processes;} a. Identify the race condition(s). b. Assume you have a mutex lock named mutex with the operations acquire () and release (). Indicate where the locking needs to be placed to prevent the race condition(s). c. Could we replace the integer variable int number_of_processes = 0; with the atomic integer atomic_t number_of_processes = 0; to prevent the race condition(s)? Why or why not?

Explanation / Answer

1. No race conditions on the variable number_of_processes.

2. Acquire() call must be placed upon entering each function and release() call should be placed immediately before exiting each function.

3. No, it will not help as the main reason is because the race occurs in the allocate_process() function where number_of_processes is first tested in the if statement, which is updated afterwards based upon the value of the test. It is possible that number_of_processes = 254 at the time of the test, but because of the race condition, is set to 255 by another thread before it was incremented.

I hope you got the answer. Let me know for any queries.

Happy Chegging!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote