1
0
UAHCode/CPE435/Lab5/processB.c

74 lines
1.6 KiB
C
Raw Permalink Normal View History

2022-08-28 21:12:16 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#include "header.h"
main()
{
char input[100];
int msid, v;
struct text_message mess;
// Creating message queue
//msid = msgget( KEY, IPC_CREAT | 0644 );
msid = msgget( KEY, 0 );
if ( msid == -1)
{
printf("Queue not joined\n");
exit(1);
}
while (1)
{
/* Read a message of the given type */
v = msgrcv( msid, &mess, 100, KEY, 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);
//msgctl( msid, IPC_RMID, 0);
}
}
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(mess.mtext,"Exit")==0)
{
msgctl( msid, IPC_RMID, 0);
exit(0);
}
printf("Process will wait\n until Exit\n or new message is received.\n");
}
}