Question: Can someone help me fix my C program so it outputs the correct sample output. I am suppose to create 4 processes. Each of these
Can someone help me fix my C program so it outputs the correct sample output.
I am suppose to create 4 processes. Each of these processes will share a variable called "total". Each will increment the variable total by one to 100,000, 200,000, 300,000 and 500,000 respectively.
Do Not Give Me Any Code Or Advice Similar To This Link Answer Provide (If You Do, I Will Automatically Down Vote!): https://www.chegg.com/homework-help/questions-and-answers/please-code-c-purpose-show-shared-memory-unix-environment-assignment-create-4-processes-pr-q23751513?trackid=43c9658b&strackid=26065fd9&ii=2
Sample Output: From Process 1: counter = 270547. From Process 2: counter = 347860. From Process 3: counter = 400001. From Process 4: counter = 500000.
C Program Code: #include
#define SHMKEY ((key_t) 1497)
// Shared Memory typedef struct { int value; } shared_mem;
shared_mem *total;
/*----------------------------------------------------------------------* * This function increases the value of shared variable "total" * by one with target of 100000 *----------------------------------------------------------------------*/ void process1 () { int k = 0;
while (k < 100000) { total->value = total->value + 1; k++; }
printf ("From process1 total = %d ", total->value); }
/*----------------------------------------------------------------------* * This function increases the value of shared memory variable "total" * by one with a target 200000 *----------------------------------------------------------------------*/
void process2 () { int k = 0;
while (k < 200000) { total->value = total->value + 1; k++; }
printf ("From process1 total = %d ", total->value); }
/*----------------------------------------------------------------------* * This function increases the value of shared memory variable "total" * by one with a target 300000 *----------------------------------------------------------------------*/ void process3 () { int k = 0;
while (k < 300000) { total->value = total->value + 1; k++; }
printf ("From process1 total = %d ", total->value); }
/*----------------------------------------------------------------------* * This function increases the vakue of shared memory variable "total" * by one with a target 500000. *----------------------------------------------------------------------*/ void process4 () { int k = 0;
while (k < 500000) { total->value = total->value + 1; k++; }
printf ("From process4 total = %d ", total->value); }
/*----------------------------------------------------------------------* * MAIN() *----------------------------------------------------------------------*/ int main() { int shmid; int pid1; int pid2; int pid3; int pid4; int ID; int status;
char *shmadd; shmadd = (char *) 0;
/* Create and connect to a shared memory segment*/ if ((shmid = shmget (SHMKEY, sizeof(int), IPC_CREAT | 0666)) < 0) { perror ("shmget"); exit (1); }
if ((total = (shared_mem *) shmat (shmid, shmadd, 0)) == (shared_mem *) -1) { perror ("shmat"); exit (0); }
// Initialize shared memory to 0 total->value = 0;
/* Create 4 processes */ if ((pid1 = fork()) == 0) process1();
if ((pid1 != 0) && (pid2 = fork()) == 0) process2();
if ((pid1 != 0 ) && (pid2 != 0) && (pid3 = fork()) == 0 ) process3();
if ((pid1 != 0 ) && (pid2 != 0) && (pid3 != 0) && (pid4 = fork()) == 0 ) process4();
/* Parent wait for child processes to finish */ waitpid(pid1, NULL, 0 ); waitpid(pid2, NULL, 0 ); waitpid(pid3, NULL, 0 ); waitpid(pid4, NULL, 0 );
if ((pid1 != 0) && (pid2 != 0) && (pid3 != 0) && (pid4 != 0)) { waitpid(pid1); printf("Child with ID %d has just exited. ", pid1);
waitpid(pid2); printf("Child with ID %d has just exited. ", pid2);
waitpid(pid3); printf("Child with ID %d has just exited. ", pid3);
waitpid(pid4); printf("Child with ID %d has just exited. ", pid4);
// To detach a shared memory if (shmdt(total) == -1) { perror ("shmdt"); exit (-1); }
// Remove a shared memory shmctl(shmid, IPC_RMID, NULL);
printf ("\t\t End of Program ");
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
