Question
Using the shm_producer.c and shm_consumer.c programs (shown below), write two programs that play a trivia game using shared memory. Your programs must use shm_producer.c and
Using the shm_producer.c and shm_consumer.c programs (shown below), write two programs that play a trivia game using shared memory. Your programs must use shm_producer.c and shm_consumer.c as templates. In order to synchronize the question-answer-result sequence, you will force processes to sleep. Create separate server and client programs (two .c files) according to the following design requirements:
-
The server is assumed to be executed before the client.
-
The server creates the shared memory and writes a string to shared memory containing the trivia
question and sleeps for some period of time (e.g. 10 seconds) to allow the client to get user input.
-
The client, which is started after the server, reads and displays the question from shared memory.
-
The client allows the user to type an answer and writes it to the same shared memory location,
overwriting the question, and sleeps for some period of time (e.g. 10 seconds) to wait for the
result from the server.
-
After the servers sleep period, it reads the clients answer, displays it, and compares it to the
correct answer (which is known by the server). The server writes the result message (correct or
incorrect) to the same shared memory location.
-
After the clients sleep period, it reads the result and displays it to the user.
Below are examples of how your output might look (user input is in bold). The trivia question in both examples is What is the capital of Greece?. In example #1, the clients user types the correct answer Athens. In example #2, the clients user types the incorrect answer London.
Server example #1: Sent question. Sleeping... Answer received: Athens Server example #2: Sent question. Sleeping... Answer received: London | Client example #1: What is the capitol of Greece? Waiting for result... Correct! Client example #2: What is the capitol of Greece? Waiting for result... Wrong |
shm_producer.c:
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main()
{
const int SIZE = 4096;
const char *name = "OS";
const char *message0= "Studying ";
const char *message1= "Operating Systems ";
const char *message2= "Is Fun!";
int shm_fd;
void *ptr;
/* create the shared memory segment */
shm_fd = shm_open(name, O_CREAT | O_RDWR, 0666);
/* configure the size of the shared memory segment */
ftruncate(shm_fd,SIZE);
/* now map the shared memory segment in the address space of the process */
ptr = mmap(0,SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
if (ptr == MAP_FAILED) {
printf("Map failed ");
return -1;
}
/**
* Now write to the shared memory region.
*
* Note we must increment the value of ptr after each write.
*/
sprintf(ptr,"%s",message0);
ptr += strlen(message0);
sprintf(ptr,"%s",message1);
ptr += strlen(message1);
sprintf(ptr,"%s",message2);
ptr += strlen(message2);
return 0;
}
shm_consumer.c:
#include
#include
#include
#include
#include
#include
int main()
{
const char *name = "OS";
const int SIZE = 4096;
int shm_fd;
void *ptr;
int i;
/* open the shared memory segment */
shm_fd = shm_open(name, O_RDONLY, 0666);
if (shm_fd == -1) {
printf("shared memory failed ");
exit(-1);
}
/* now map the shared memory segment in the address space of the process */
ptr = mmap(0,SIZE, PROT_READ, MAP_SHARED, shm_fd, 0);
if (ptr == MAP_FAILED) {
printf("Map failed ");
exit(-1);
}
/* now read from the shared memory region */
printf("%s",ptr);
/* remove the shared memory segment */
if (shm_unlink(name) == -1) {
printf("Error removing %s ",name);
exit(-1);
}
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started