View the step-by-step solution to:

It doesn't achieve the correct sample output. Sample output: From Process 1: counter = 270547. From Process 2: counter = 347860. From Process 3:

I need help me with my C code provided below. It doesn't achieve the correct sample output.


Sample output:


From Process 1: counter = 270547.

From Process 2: counter = 347860.

From Process 3: counter = 400001.

From Process 4: counter = 500000.

Child with ID: 2412 has just exited.

Child with ID: 2411 has just exited.

Child with ID: 2413 has just exited.

Child with ID: 2415 has just exited.

               End of Simulation.


Code Provided:


#include <stdio.h>

#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/shm.h>

#include <stdlib.h>

#include <unistd.h>

#include <errno.h>


#define SHMKEY ((key_t) 3495)



// 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 = %dn", 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 = %dn", 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 = %dn", 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 = %dn", 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 then 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.n", pid1);


       waitpid(pid2);

       printf("Child with ID %d has just exited.n", pid2);


       waitpid(pid3);

       printf("Child with ID %d has just exited.n", pid3);


       waitpid(pid4);

       printf("Child with ID %d has just exited.n", pid4);



         // To detach a shared memory

       if (shmdt(total) == -1)   {

           perror ("shmdt");

           exit (-1);

       }


       // Remove a shared memory

       shmctl(shmid, IPC_RMID, NULL);


       printf ("tt End of Programn");


   }


}

Top Answer

Here is the working code. The values doesnt match because it is about processes that may varies from one system to another.... View the full answer

Sign up to view the full answer

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question
Ask a homework question - tutors are online