Question: Code using C In this assignment, a memory location is shared by four processes. Each process independently tries to increase the content of the shared
Code using C
In this assignment, a memory location is shared by four processes. Each process independently tries to increase the content of the shared memory location from 1 to a certain value by increments of one. Process 1 has a target of 100000, Process 2s target is 200000, Process 3 has a taget of 300000, and the goal of 4 is 500000. When the program terminates, therefore, the shared memory variable will have a total of 1100000 (i.e. this value will be output by whichever of the four processes finishes last).
In this project, you are to modify the assignment1 to protect the critical section using semaphores.
After all the children have finished, the parent process should release the shared memory and semaphores and then terminate. Use the "wait" function so that the parent knows precisely when each of the children finishes. The parent should print the process ID of each child as the child finishes execution. Then it should release shared memory,semaphores, and print "End of Simulation".
Sample output
From Process 1: counter = 330547.
From Process 2: counter = 447860.
From Process 3: counter = 600059.
From Process 4: counter = 1100000
Child with ID 2412 has just exited.
Child with ID 2411 has just exited.
Child with ID 2413 has just exited.
Child with ID 2414 has just exited.
End of Simulation.
--------------------------------------------------------
this is the assignment 1 code :-
#include
.End of program......................... "); return 0; } void processeOne(int * total) { int i; for (i=0; i<100000; i++) { *total = *total + 1; } printf("From Process 1: counter = %d ",*total); } void processeTwo(int * total) { int i; for (i=0; i<200000; i++) { *total = *total + 1; } printf("From Process 2: counter = %d ",*total); } void processeThree(int * total) { int i; for (i=0; i<300000; i++) { *total = *total + 1; } printf("From Process 3: counter = %d ",*total); } void processeFour(int * total) { int i; for (i=0; i<500000; i++) { *total = *total + 1; } printf("From Process 4: counter = %d ",*total); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
