#include #include #include #include #include #include #include "header.h" main() { char input[100]; int msid, v; struct text_message mess; // Creating message queue msid = msgget( KEY, IPC_CREAT | 0666 ); if ( msid == -1) { printf("Queue not joined\n"); exit(1); } while (1) { printf("Enter a message: "); fgets(input, sizeof(input), stdin); input[strcspn(input, "\n")] = 0; /* Preparing a message */ mess.mtype = KEY; strcpy( mess.mtext, input); /* Write a message into queue */ v = msgsnd (msid, &mess, strlen( input ) + 1, 0); if ( v < 0 ) { printf("\nMessage failed to send\n"); exit(1); } printf("Message sent\n"); if (strcmp(input,"Exit")==0) { msgctl( msid, IPC_RMID, 0); exit(0); } printf("Process will wait\n until Exit\n or new message is received.\n"); /* Read a message of the given type */ v = msgrcv( msid, &mess, 100, mess.mtype, 0); // Blocking if ( v < 0 ) { printf("Error receiving message\n"); exit(1); } else { if (strcmp(mess.mtext,"Exit")==0) { printf("Exit received\n"); msgctl( msid, IPC_RMID, 0); exit(0); } else { printf("Received: %s\n", mess.mtext); } } } }