Question: (Shared memory) The following programs are non-completed Producer and Consumer programs such that: A shared memory with size 5 was created by the producer. You

(Shared memory)

The following programs are non-completed Producer and Consumer programs such that:

  • A shared memory with size 5 was created by the producer.

You should complete these programs such that:

  • The producer fills the shared memory with the integers: -1, 2, -3, 4, 5.
  • The consumer will convert all negative numbers to positive numbers (i.e., the integers in the shared memory will be 1, 2, 3, 4, 5). Then, the consumer terminates.
  • If the producer finds that the first integer in the memory is a positive integer then it will add 3 to all integers (i.e., the integers in the shared memory will be 4, 5, 6, 7, 8) then the producer terminates.

Producer.c

Consumer.c

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define SHMSZ 5

int main(){

int shmid; key_t key; int *shm; key=5678;

if((shmid=shmget(key,SHMSZ, IPC_CREAT | 0666))<0){

perror("shmget");exit(1);

}

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

perror("shmat");exit(1);

}

// complete it below

return 0;

}

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define SHMSZ 5

int main(){

int shmid; key_t key; int *shm; key=5678;

if((shmid=shmget(key,SHMSZ, IPC_CREAT | 0666))<0){

perror("shmget"); exit(1);

}

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

perror("shmat"); exit(1);

}

// complete it below

return 0;

}

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!