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 #include #include #include #include #include #include // prototype for create(). void create( int *, int*); void processeOne(int * ); void processeTwo(int *); void processeThree(int *); void processeFour(int *); int main() { // intilize the variebles int * total = 0; int shmid; pid_t Id ; // create a shared Memory segment shmid = shmget(IPC_PRIVATE, sizeof(int), IPC_CREAT | 0666); // print an error and exit if memory is not created succesfully if (shmid < 0) { printf("shmget error 1 "); exit(1); } // connect a shared memory segment total = (int *) shmat(shmid, NULL, 0); // print an error and exit if memory was not connected succesfully if ((int) total == -1) { printf("shmat error "); exit(1); } // assign zero value to the shared memory *total = 0; //// Process #1 /////// //create 1st processe Id = fork(); //terminate if an error accured in creating the processe if (Id <0) { printf("Error! "); return 0; } if(Id ==0) { processeOne(total); exit(0); } //// Process #2 /////// //create 2nd processe if (Id >0) { Id = fork(); //terminate if an error accured in creating the processe if (Id <0) { printf("Error! "); return 0; } if(Id ==0) { processeTwo(total); exit(0); } } //// Process #3 /////// //create 3rd processe if (Id >0) { Id = fork(); //terminate if an error accured in creating the processe if (Id <0) { printf("Error! "); return 0; } if(Id ==0) { processeThree(total); exit(0); } } //// Process #4 /////// //create 4th processe if (Id >0) { Id = fork(); //terminate if an error accured in creating the processe if (Id <0) { printf("Error! "); return 0; } if(Id ==0) { processeFour(total); exit(0); } } // printing the ID of each Child. //wait(NULL) allow the parent to know when child is done. sleep(1); printf(" "); printf("Child with ID: %d has just exited ",wait(NULL)); printf("Child with ID: %d has just exited ",wait(NULL)); printf("Child with ID: %d has just exited ",wait(NULL)); printf("Child with ID: %d has just exited ",wait(NULL)); exit(1); // release shared memory. shmdt((void *) total); shmctl(shmid, IPC_RMID, NULL); printf(".....................

.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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!