Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

More Books

Students also viewed these Databases questions