Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Professional SQL Server 2012 Internals And Troubleshooting

Authors: Christian Bolton, Justin Langford

1st Edition

1118177657, 9781118177655

More Books

Students also viewed these Databases questions

Question

What is the basis for Security Concerns in Cloud Computing?

Answered: 1 week ago

Question

Describe the three main Cloud Computing Environments.

Answered: 1 week ago