Guest User

forked

a guest
May 27th, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.51 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/ipc.h>
  3. #include <sys/msg.h>
  4. #include <unistd.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <signal.h>
  9. #define MSG_SZ_MAX 255
  10.  
  11. struct message{
  12.     long mtype;
  13.     pid_t pid;
  14.     char mtext[MSG_SZ_MAX];
  15. };
  16.  
  17. int descr;
  18. struct message sendMsg, recieveMsg;
  19.  
  20. static void sigHandle(int signum);
  21.  
  22.  
  23. void main(){
  24.     descr = msgget(10, IPC_CREAT|0666);
  25.     if(descr==-1){
  26.         perror("msgget");
  27.     }
  28.    
  29.     printf("descriptor : %d\n", descr);
  30.     pid_t myPID = getpid();
  31.     printf("PID : %d\n", myPID);
  32.  
  33.     if(signal(SIGINT, sigHandle) == SIG_ERR){
  34.         perror("Can't handle a signal SIGINT.");
  35.         exit(1);
  36.     }
  37.     if(signal(SIGUSR1, sigHandle) == SIG_ERR){
  38.         perror("Can't handle a signal SIGUSR1.");
  39.         exit(1);
  40.     }
  41.     if(signal(SIGTERM, sigHandle) == SIG_ERR){
  42.         perror("Can't handle a signal SIGTERM.");
  43.         exit(1);
  44.     }
  45.    
  46.     //WHERE TO SEND
  47.     printf("Receiver's PID : ");
  48.     pid_t receiverPID;
  49.     scanf("%d", &receiverPID);
  50.  
  51.     //setting sending msg metainfo
  52.     sendMsg.mtype = receiverPID;
  53.     sendMsg.pid = myPID;
  54.  
  55.     //setting recieve msg metainfo
  56.     recieveMsg.mtype = myPID;
  57.    
  58.    
  59.     fflush(stdout);
  60.     if(fork() == 0){
  61.         //printf("child's pid = %d\n", getpid());
  62.         while(1){
  63.             //printf("pid=%d,sending...\n", getpid());
  64.  
  65.             char buf[MSG_SZ_MAX];
  66.             scanf("%s", buf);
  67.             sprintf(sendMsg.mtext, "[%d] : %s", myPID, buf);
  68.      
  69.             if(msgsnd(descr, &sendMsg, sizeof(struct message), 0) == -1){
  70.                 perror("msgsnd");
  71.                 exit(2);
  72.             }
  73.         }
  74.     } else {
  75.         //printf("parent's pid = %d\n", getpid());
  76.         while(1){
  77.             //printf("pid=%d,receiving...\n", getpid());
  78.  
  79.             if(msgrcv(descr, &recieveMsg, sizeof(struct message), myPID, IPC_NOWAIT) != -1){
  80.                 printf("%s\n", recieveMsg.mtext);
  81.             }
  82.         }
  83.     }
  84. }
  85.  
  86. static void sigHandle(int signum){
  87.     if(signum == SIGUSR1){
  88.         printf("\nOther process has been terminated.\n");
  89.         msgctl(descr, IPC_RMID, 0);
  90.         exit(0);
  91.     }
  92.     if(signum == SIGINT){
  93.         kill(recieveMsg.pid, SIGUSR1);
  94.         msgctl(descr, IPC_RMID, 0);
  95.         exit(0);
  96.     }
  97.     if(signum == SIGTERM){
  98.         printf("\nSIGTERM has been received, terminated.\n");
  99.         kill(recieveMsg.pid, SIGUSR1);
  100.         msgctl(descr, IPC_RMID, 0);
  101.         exit(0);
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment