added more code
This commit is contained in:
BIN
CPE435/Lab5/IPC Message Queues.pdf
Normal file
BIN
CPE435/Lab5/IPC Message Queues.pdf
Normal file
Binary file not shown.
BIN
CPE435/Lab5/IPC Message Queues.pptx
Normal file
BIN
CPE435/Lab5/IPC Message Queues.pptx
Normal file
Binary file not shown.
BIN
CPE435/Lab5/Lab05_Assignmnet.pdf
Normal file
BIN
CPE435/Lab5/Lab05_Assignmnet.pdf
Normal file
Binary file not shown.
7
CPE435/Lab5/header.h
Normal file
7
CPE435/Lab5/header.h
Normal file
@ -0,0 +1,7 @@
|
||||
struct text_message
|
||||
{
|
||||
long mtype;
|
||||
char mtext[100];
|
||||
#define KEY (key_t) 1234
|
||||
|
||||
};
|
BIN
CPE435/Lab5/lab5Output.png
Normal file
BIN
CPE435/Lab5/lab5Output.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 8.7 KiB |
BIN
CPE435/Lab5/processA
Executable file
BIN
CPE435/Lab5/processA
Executable file
Binary file not shown.
74
CPE435/Lab5/processA.c
Normal file
74
CPE435/Lab5/processA.c
Normal file
@ -0,0 +1,74 @@
|
||||
#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 | 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
BIN
CPE435/Lab5/processB
Executable file
BIN
CPE435/Lab5/processB
Executable file
Binary file not shown.
73
CPE435/Lab5/processB.c
Normal file
73
CPE435/Lab5/processB.c
Normal file
@ -0,0 +1,73 @@
|
||||
#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");
|
||||
|
||||
}
|
||||
}
|
||||
|
BIN
CPE435/Lab5/receiver
Executable file
BIN
CPE435/Lab5/receiver
Executable file
Binary file not shown.
45
CPE435/Lab5/receiver.c
Normal file
45
CPE435/Lab5/receiver.c
Normal file
@ -0,0 +1,45 @@
|
||||
// Adapted form IPC Message Queues slides
|
||||
// receiver.c
|
||||
// usage: ./receiver <key> <type>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/ipc.h>
|
||||
#include <sys/shm.h>
|
||||
#include <string.h>
|
||||
|
||||
struct text_message
|
||||
{
|
||||
long mtype;
|
||||
char mtext[100];
|
||||
|
||||
};
|
||||
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int msid, v;
|
||||
struct text_message mess;
|
||||
|
||||
/* Get a message handle */
|
||||
msid = msgget( (key_t) atoi ( argv[1] ), 0 );
|
||||
if ( msid == -1)
|
||||
{
|
||||
printf("Queue not joined\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Read a message of the given type */
|
||||
v = msgrcv( msid, &mess, 100, atoi( argv[2] ), IPC_NOWAIT ); // Non-blocking
|
||||
if ( v < 0 )
|
||||
{
|
||||
printf("Error receiving message\n");
|
||||
exit(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("%d %s\n", mess.mtype, mess.mtext);
|
||||
msgctl( msid, IPC_RMID, 0);
|
||||
exit(0);
|
||||
}
|
||||
}
|
BIN
CPE435/Lab5/sender
Executable file
BIN
CPE435/Lab5/sender
Executable file
Binary file not shown.
43
CPE435/Lab5/sender.c
Normal file
43
CPE435/Lab5/sender.c
Normal file
@ -0,0 +1,43 @@
|
||||
// Adapted form IPC Message Queues Slides
|
||||
// sender.c
|
||||
// usage: ./sender <key> <type> <text>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/ipc.h>
|
||||
#include <sys/shm.h>
|
||||
#include <string.h>
|
||||
|
||||
struct text_message
|
||||
{
|
||||
long mtype;
|
||||
char mtext[100];
|
||||
};
|
||||
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int msid, v;
|
||||
struct text_message mess;
|
||||
|
||||
/* Creating a message queue */
|
||||
msid = msgget((key_t) atoi( argv[1] ), IPC_CREAT | 0666 );
|
||||
if( msid == -1 )
|
||||
{
|
||||
printf("Failed to create queue\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Preparing a message */
|
||||
mess.mtype = atoi( argv[2] );
|
||||
strcpy( mess.mtext, argv[3] );
|
||||
|
||||
/* Write a message into queue */
|
||||
v = msgsnd (msid, &mess, strlen( argv[3] ) + 1, 0 );
|
||||
if ( v < 0 )
|
||||
{
|
||||
printf("Message failed to send\n");
|
||||
exit(1);
|
||||
}
|
||||
printf("Message sent\n");
|
||||
}
|
Reference in New Issue
Block a user