Question: Programs below simulate the producer-consumer problem as independent processes which do not share anything by default. In this version, processes share data via a shared

Programs below simulate the producer-consumer problem as independent processes which do not share anything by default. In this version, processes share data via a shared memory segment and access the same semaphores by using their unique ID (name).

(1) Compile and run the programs given below as separate processes.

(2) Modify the code for both producer & consumer to include the PID of the process in the output statements.

(3) Start multiple producers and multiple consumers. Do you have to make any changes to the code to run properly? Explain your answer.

(4) modify the code given below by adding a third type of process which processes the data generated by the producer, before its used and printed by the consumer. In this example, the Processing process changes the case if the item generated by the producer from upper-case to lower-case and stores it back in the buffer before the consumer can retrieve and print it. Submit a copy of your program and snapshot of your output.

//Producers.c

#include

#include

#include

#include

#include

#include

#include

#include

#define BUFF_SIZE 20

typedef struct {

char buffer[BUFF_SIZE];

int nextIn;

int nextOut;

} shared_data;

shared_data *shm, *s;

char sem_name1[] = "mutex";

char sem_name2[] = "empty_slots";

char sem_name3[] = "full_slots";

sem_t *empty_slots;

sem_t *full_slots;

sem_t *mutex;

void Put(char item)

{

sem_wait(empty_slots);

sem_wait(mutex);

s->buffer[s->nextIn] = item;

s->nextIn = (s->nextIn + 1) % BUFF_SIZE;

sem_post(mutex);

printf("Producing %c ... ", item);

sem_post(full_slots);

}

void Producer()

{

int i;

for(i = 0; i < 20; i++)

{

sleep(rand()%3);

Put((char)('A'+ i % 26));

}

}

void main()

{

mutex=sem_open(sem_name1, O_CREAT,0644, 1);

full_slots=sem_open(sem_name3, O_CREAT,0644, 0);

empty_slots=sem_open(sem_name2, O_CREAT,0644, 10);

//allocate the shared memory segment

key_t key;

key = 1234;

//create the segment

int shmid;

if ((shmid = shmget(key, sizeof(shared_data), IPC_CREAT |0666)) <0)

{

perror("Shmget");

exit(1);

}

//attach to the segment

if ((shm = (shared_data *) shmat(shmid, NULL, 0))==(shared_data *) -1)

{

perror("Shmat");

exit(1);

}

s=shm;

s->nextIn = 0;

Producer( );

//detach

shmdt((void *) shm);

}

//-------------------------------------------------------Consumer.c-----------------------------

#include

#include

#include

#include

#include

#include

#define BUFF_SIZE 20

char buffer[BUFF_SIZE];

int nextIn = 0;

int nextOut = 0;

char sem_name1[] = "mutex";

char sem_name2[] = "empty_slots";

char sem_name3[] = "full_slots";

sem_t *empty_slots;

sem_t *full_slots;

sem_t *mutex;

typedef struct {

char buffer[BUFF_SIZE];

int nextIn;

int nextOut;

} shared_data;

shared_data *shm, *s;

void Get(char item)

{

sem_wait(full_slots);

sem_wait(mutex);

item = s->buffer[s->nextOut];

s->nextOut = (s->nextOut + 1) % BUFF_SIZE;

sem_post(mutex);

printf("Consuming %c ... ", item);

sem_post(empty_slots);

}

void Consumer()

{

int i;

char item;

for(i = 0; i < 10; i++)

{

sleep(rand()%9);

Get(item);

}

}

void main()

{

mutex=sem_open(sem_name1, O_CREAT,0644, 1);

full_slots=sem_open(sem_name3, O_CREAT,0644, 0);

empty_slots=sem_open(sem_name2, O_CREAT,0644, 10);

//allocate the shared memory segment

key_t key;

key = 1234;

//locate the segment

int shmid;

if ((shmid = shmget(key, sizeof(shared_data),0666)) <0)

{

perror("Shmget");

exit(1);

}

//attach to the segment

if ((shm = (shared_data *) shmat(shmid, NULL, 0))==(shared_data *) -1)

{

perror("Shmat");

exit(1);

}

s=shm;

s->nextOut = 0;

Consumer();

shmdt((void *) shm);

}

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!