/* clavierEcran.c */ #include #include #include #include #include #include #include #include #include "scanLine.h" #include "message.h" void clavier(int msg_id) { msgtext message; printf("Entrez des lignes et terminez par fin : \n"); message.mtype = DATA; do { scanLine(message.mdata.texte,TAILLE); if (msgsnd(msg_id,(struct msgbuf *)&message,sizeof(data),0)==-1) { perror("msgsnd"); exit(EXIT_FAILURE); } } while (strcmp(message.mdata.texte,"fin")!=0); } void ecran(int msg_id) { msgtext message; do { if (msgrcv(msg_id,(struct msgbuf *)&message,sizeof(data),DATA,0)==-1) { perror("msgrcv"); exit(EXIT_FAILURE); } printf("Recu %s\n",message.mdata.texte); } while (strcmp(message.mdata.texte,"fin")!=0); } int main(void) { key_t msg_cle; int msg_id; pid_t pid; msg_cle = IPC_PRIVATE; if (msg_cle == -1) { perror("ftok"); exit(EXIT_FAILURE); } msg_id = msgget(msg_cle, IPC_CREAT | IPC_EXCL | 0666); if (msg_id == -1) { perror("msgget"); exit(EXIT_FAILURE); } pid = fork(); switch (pid) { case -1 : perror("fork"); exit(EXIT_FAILURE); break; case 0 : ecran(msg_id); exit(EXIT_SUCCESS); break; default : clavier(msg_id); break; } wait(NULL); if ((msgctl(msg_id,IPC_RMID,NULL))==-1) { perror("msgctl"); exit(EXIT_FAILURE); } return EXIT_SUCCESS; }