Quipex

signalqueue

May 12th, 2017
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.26 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.     printf("descriptor : %d\n", descr);
  26.     printf("PID : %d\n", getpid());
  27.  
  28.     if(signal(SIGINT, sigHandle) == SIG_ERR){
  29.         perror("Can't handle a signal SIGINT.");
  30.         exit(1);
  31.     }
  32.     if(signal(SIGUSR1, sigHandle) == SIG_ERR){
  33.         perror("Can't handle a signal SIGUSR1.");
  34.         exit(1);
  35.     }
  36.     if(signal(SIGTERM, sigHandle) == SIG_ERR){
  37.         perror("Can't handle a signal SIGTERM.");
  38.         exit(1);
  39.     }
  40.  
  41.     printf("Receiver's PID : ");
  42.     pid_t pidRes;
  43.     scanf("%d", &pidRes);
  44.  
  45.     sendMsg.mtype = pidRes;
  46.     sendMsg.pid = getpid();
  47.     recieveMsg.mtype = getpid();
  48.    
  49.  
  50.     if(descr==-1){
  51.         perror("msgget");
  52.     }
  53.  
  54.     while(1){
  55.        
  56.         strcpy(sendMsg.mtext, "[");
  57.         //strcat(sendMsg.mtext, (char*)getpid);
  58.         char senderpid[10];
  59.         snprintf(senderpid, 10, "%ld", (long)getpid());
  60.         strcat(sendMsg.mtext, senderpid);
  61.         strcat(sendMsg.mtext, "] : ");
  62.  
  63.         /*
  64.         *sendMsg.mtext += sprintf(sendMsg.mtext, "%s", "[");
  65.         *sendMsg.mtext += sprintf(sendMsg.mtext, "%ld", (long)getpid());
  66.         *sendMsg.mtext += sprintf(sendMsg.mtext, "%s", "] : ");*/
  67.  
  68.         char buf[MSG_SZ_MAX];
  69.         /**
  70.         *INPUT
  71.         */
  72.         scanf("%s", buf);
  73.         strcat(sendMsg.mtext, buf);
  74.         //fgets(buf, MSG_SZ_MAX, stdin);
  75.         //*sendMsg.mtext += sprintf(sendMsg.mtext, "%s", buf);
  76.  
  77.         if(msgsnd(descr, &sendMsg, sizeof(struct message), 0) == -1){
  78.             perror("msgsnd");
  79.             exit(2);
  80.         }
  81.  
  82.         while(1){
  83.             if(msgrcv(descr, &recieveMsg, sizeof(struct message), getpid(), IPC_NOWAIT) != -1){
  84.                 printf("%s\n", recieveMsg.mtext);
  85.             } else break;
  86.         }
  87.     }
  88. }
  89.  
  90. static void sigHandle(int signum){
  91.     if(signum == SIGUSR1){
  92.         printf("\nOther process has been terminated.\n");
  93.         exit(0);
  94.     }
  95.     if(signum == SIGINT){
  96.         kill(recieveMsg.pid, SIGUSR1);
  97.         msgctl(descr, IPC_RMID, 0);
  98.         exit(0);
  99.     }
  100.     if(signum == SIGTERM){
  101.         printf("\nSIGTERM has been received, terminated.\n");
  102.         kill(recieveMsg.pid, SIGUSR1);
  103.         msgctl(descr, IPC_RMID, 0);
  104.         exit(0);
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment