Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Project Description: In this assignment, a memory location is shared by four processes. Each process independently tries to increase the content of the shared memory

Project Description: 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 target 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).

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.

The semaphore structures and functions:

#include

// semaphore key

#define SEMKEY ((key_t) 400L)

// number of semaphores being created

#define NSEMS 1

// GLOBAL

int sem_id;// semaphore id

// semaphore buffers

static struct sembuf OP = {0,-1,0};

static struct sembuf OV = {0,1,0};

struct sembuf *P =&OP;

struct sembuf *V =&OV;

// semapore union used to generate semaphore

typedef union{

int val;

struct semid_ds *buf;

ushort *array;

} semunion;

// POP (wait()) function for semaphore to protect critical section

int POP()

{

int status;

status = semop(sem_id, P,1);

return status;

}

// VOP (signal()) function for semaphore to release protection

int VOP()

{

int status;

status = semop(sem_id, V,1);

return status;

In the main()

int value, value1;

semunion semctl_arg;

semctl_arg.val = 1;

/* Create semaphores */

sem_id = semget(SEMKEY, NSEMS, IPC_CREAT | 0666);

if(sem_id < 0) printf("Error in creating the semaphore./n");

/* Initialize semaphore */

value1 =semctl(sem_id, semnum, SETVAL, semctl_arg);

value =semctl(sem_id, semnum, GETVAL, semctl_arg);

if (value < 1) printf("Eror detected in SETVAL. ");

/* De-allocate semaphore */

semctl_arg.val = 0;

status =semctl(sem_id, 0, IPC_RMID, semctl_arg);

if( status < 0) printf("Error in removing the semaphore. ");

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Marketing The Ultimate Marketing Tool

Authors: Edward L. Nash

1st Edition

0070460639, 978-0070460638

More Books

Students also viewed these Databases questions

Question

Consider this article:...

Answered: 1 week ago