Question: modify following program to take input from user infinitely till we press ^c #include #include #include #include #include #include #include #define SHM_SIZE 1024 // size

modify following program to take input from user infinitely till we press ^c

#include

#include

#include

#include

#include

#include

#include

#define SHM_SIZE 1024 // size of shared memory segment

int main() {

// create a shared memory segment

int shm_id = shmget(IPC_PRIVATE, SHM_SIZE, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);

if (shm_id < 0) {

perror("shmget");

exit(1);

}

// attach the shared memory segment

char *shm_ptr = shmat(shm_id, NULL, 0);

if (shm_ptr == (void *) -1) {

perror("shmat");

exit(1);

}

// create a child process to act as the producer

int pid = fork();

if (pid == 0) {

printf("Producer: writing to shared memory... ");

strcpy(shm_ptr, "Hello, Consumer!");

printf("Producer: done. ");

exit(0);

} else {

// parent process acts as the consumer

wait(NULL);

printf("Consumer: reading from shared memory... ");

printf("Consumer: %s ", shm_ptr);

printf("Consumer: done. ");

}

// detach the shared memory segment

if (shmdt(shm_ptr) == -1) {

perror("shmdt");

exit(1);

}

// remove the shared memory segment

if (shmctl(shm_id, IPC_RMID, NULL) == -1) {

perror("shmctl");

exit(1);

}

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!