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

Code using C In this assignment, a memory location is shared by four processes.

ID: 3750659 • Letter: C

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 memory location from 1 to a certain value by increments of one. Process 1 has a target of 100000, Process 2’s 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 <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

// 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);
}

Explanation / Answer

Here is the modified code -

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/wait.h>
#include <fcntl.h> /* For O_* constants */
#include <sys/stat.h> /* For mode constants */
#include <semaphore.h>

#define SEM_PATH "assgn_sem_path"


// prototype for create().
void create( int *, int*);
void processeOne(int * );
void processeTwo(int *);
void processeThree(int *);
void processeFour(int *);

void processeOne(int *total)
{
   sem_t *sem;
   sem = sem_open(SEM_PATH, 0);
   for(int i = 0; i < 100000; i++) {
       sem_wait(sem);
       *total = *total + 1;
       sem_post(sem);
   }
   sem_wait(sem);
   printf("From Process 1: counter = %d ",*total);
   sem_post(sem);
}

void processeTwo(int *total)
{
   sem_t *sem;
   sem = sem_open(SEM_PATH, 0);
   for(int i = 0; i < 200000; i++) {
       sem_wait(sem);
       *total = *total + 1;
       sem_post(sem);
   }
   sem_wait(sem);
   printf("From Process 2: counter = %d ",*total);
   sem_post(sem);
}
void processeThree(int *total)
{
   sem_t *sem;
   sem = sem_open(SEM_PATH, 0);
   for(int i = 0; i < 300000; i++) {
       sem_wait(sem);
       *total = *total + 1;
       sem_post(sem);
   }
   sem_wait(sem);
   printf("From Process 3: counter = %d ",*total);
   sem_post(sem);
}
void processeFour(int *total)
{
   sem_t *sem;
   sem = sem_open(SEM_PATH, 0);
   for(int i = 0; i < 500000; i++) {
       sem_wait(sem);
       *total = *total + 1;
       sem_post(sem);
   }
   sem_wait(sem);
   printf("From Process 4: counter = %d ",*total);
   sem_post(sem);
}
int main() {

   // intilize the variebles
   int *total = 0;
   int shmid;
   pid_t Id ;
   sem_t *sem;

   // 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;

   sem_unlink(SEM_PATH);
   // open a named semaphore
   sem = sem_open(SEM_PATH, O_CREAT, 0600, 1);
   if (sem == NULL) {
       perror("sem_open failed");
       exit(1);
   }

   //// 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));

   // release shared memory.

   shmdt((void *) total);
   shmctl(shmid, IPC_RMID, NULL);
   sem_close(sem);
   printf("......................End of program......................... ");
   return 0;
}

Output -

vagrant@ubuntu-xenial ~/chegg> gcc shm.c -lpthread -Wall

vagrant@ubuntu-xenial ~/chegg> ./a.out

From Process 1: counter = 425522

From Process 2: counter = 785980

From Process 3: counter = 947863

From Process 4: counter = 1100000

Child with ID: 1973 has just exited

Child with ID: 1974 has just exited

Child with ID: 1975 has just exited

Child with ID: 1976 has just exited

......................End of program.........................

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