Question

Need help with this code in Cbb.csueast bayed Apple Disney ESPN Yahoo! CH15-20172 CS 3560 011 Chegg Study Guided Solutions and Study Help I Chegg.com Requirements Write your own simple client/server set of processes. The client should use a shared memory segment to send a command message to the server. The server process should monitor the shared memory segment, and respond as follows: Hi The server prints Greetings to the screen. PID The server prints its process id to the screen. QUIT The server terminates gracefully, detaching and releasing the shared memory segment. Hints Use polling (an intentional infinite loop in the server to monitor the shared memory segment. You will need some way to coordinate sharing the memory segment. read man pages for shmdt, shmget, shmat, shmott, semct, semop, semget A 10-point sample run Terminal 1 stalica trig N]$ cc cl.c -ocl [stalica trig N]$ ./cl HI PID QUIT [stalica trig Terminal 2 Istalica trig ~J$ cc server.c Server [stalica trig J$ ./server Greetings! Server pid: 24700 GOODBYE! [stalica trig ~J$

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Executable Code:

#include <signal.h>

#include <stdio.h>

#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/shm.h>

#include <sys/sem.h>

#include <errno.h>

void hand_contr(int);

int shm_id,pid=0;

char * pshmatt=NULL;

int main()

{

     signal(SIGINT,hand_contr);

     // Create the segment

     shm_id=shmget(100,200,IPC_CREAT|IPC_EXCL|0777);

     // Attach the shared memory

     pshmatt=shmat(shm_id,NULL,0);

     while(1)

     {

          // Compare if it is "HI"

          if(strcmp(pshmatt,"HI")==0)

          {

              printf("Greetings!\n");

              fflush(stdout);

              pshmatt[0]='\0';

          }

          // Compare if it is "PID"

          else if(strcmp(pshmatt,"PID")==0)

          {

              // Get the id of process

              pid=(int)getpid();

              printf("Server pid: %i\n",pid);

              fflush(stdout);

              pshmatt[0]='\0';

          }

          // Compare if it is "QUIT"

          else if(strcmp(pshmatt,"QUIT")==0)

          {

              shmctl(shm_id,IPC_RMID,NULL);

              shmdt(pshmatt);

              printf("GOODBYE!\n");

              exit(0);

          }

     }

}

void hand_contr(int XYZ)

{

     shmctl(shm_id,IPC_RMID,NULL);

     shmdt(pshmatt);

     printf("GOODBYE!\n");

     exit(0);

}

Add a comment
Know the answer?
Add Answer to:
Need help with this code in C Write your own simple client/server set of processes. The...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT