#include #include #include #include #include #include #include #include int descr; struct message n,l; struct message{ long mtype; pid_t pid; char mtext[100]; }; static void sigHandle(int signum); void main(){ if(signal(SIGINT, sigHandle) == SIG_ERR){ perror("Can't handle a signal SIGINT."); exit(1); } if(signal(SIGUSR1, sigHandle) == SIG_ERR){ perror("Can't handle a signal SIGUSR1."); exit(1); } n.mtype=2; n.pid=getpid(); l.mtype=1; descr = msgget(100,IPC_CREAT|0666); printf("descriptor : %d\n", descr); printf("PID : %d\n", getpid()); if(descr==-1){ printf("msgget"); } while(1){ strcpy(n.mtext, "[msg2] : "); char buf[100]; scanf("%s", buf); strcat(n.mtext, buf); int res1=msgsnd(descr,&n,sizeof(struct message),0); if(res1==-1){ printf("msgsnd"); } int res2; do{ res2=msgrcv(descr,&l,sizeof(struct message),1,IPC_NOWAIT); if(res2!=-1){ printf("%s\n",l.mtext); } }while(res2!=-1); } } static void sigHandle(int signum){ if(signum == SIGUSR1){ printf("Other pal has quited.\n"); exit(0); } if(signum == SIGINT){ printf("\nSIGINT has been received, terminated.\n"); kill(l.pid, SIGUSR1); msgctl(descr, IPC_RMID, 0); exit(0); } }