aboutsummaryrefslogtreecommitdiffstats
path: root/csci4061/102620_breakout
diff options
context:
space:
mode:
authorMatthew Strapp <msattr@gmail.com>2020-10-26 08:12:34 -0500
committerMatthew Strapp <msattr@gmail.com>2020-10-26 08:12:34 -0500
commitd7afb98ca2d9ed18400310f42d3f3c8177959280 (patch)
tree600442970cb3afb8ab2f56f2a290cfe76d2d0d10 /csci4061/102620_breakout
parente (diff)
downloadhomework-d7afb98ca2d9ed18400310f42d3f3c8177959280.tar
homework-d7afb98ca2d9ed18400310f42d3f3c8177959280.tar.gz
homework-d7afb98ca2d9ed18400310f42d3f3c8177959280.tar.bz2
homework-d7afb98ca2d9ed18400310f42d3f3c8177959280.tar.lz
homework-d7afb98ca2d9ed18400310f42d3f3c8177959280.tar.xz
homework-d7afb98ca2d9ed18400310f42d3f3c8177959280.tar.zst
homework-d7afb98ca2d9ed18400310f42d3f3c8177959280.zip
Do breakouts
Diffstat (limited to '')
-rw-r--r--csci4061/102620_breakout/msg_queue_template.c107
1 files changed, 107 insertions, 0 deletions
diff --git a/csci4061/102620_breakout/msg_queue_template.c b/csci4061/102620_breakout/msg_queue_template.c
new file mode 100644
index 0000000..676291e
--- /dev/null
+++ b/csci4061/102620_breakout/msg_queue_template.c
@@ -0,0 +1,107 @@
+/*
+* Recitation Section Number:
+* Breakout Number:
+* Member Name (X500)
+* Member Name (X500)
+* Member Name (X500)
+* Member Name (X500)
+*/
+
+#include <stdio.h>
+#include <sys/ipc.h>
+#include <sys/msg.h>
+#include <zconf.h>
+#include <string.h>
+#include <stdlib.h>
+
+#define PERM 0666
+#define MSGSIZE 100
+#define NCHILD 3
+
+// structure for message queue
+typedef struct msg_buffer {
+ long mtype;
+ char mtext[MSGSIZE];
+} message;
+
+int main(void) {
+ key_t key;
+ int msgid;
+ pid_t pid1, pid2;
+ message msg;
+
+ // generate unique key
+ key = ftok("recitation", 4061);
+
+ // creates a message queue
+ msgid = msgget(key, PERM | IPC_CREAT);
+
+
+ // sender child process
+ pid1 = fork();
+ if (pid1 == 0) {
+ for (int i = 0; i < NCHILD; i++) {
+ msg.mtype = 111;
+ memset(msg.mtext, '\0', MSGSIZE);
+ sprintf(msg.mtext, "Hello child %d", i);
+ // send message to other child processes
+ msgsnd(msgid, &msg, MSGSIZE, 0);
+
+
+ // display the message
+ printf("[%d] Data sent is : %s \n", i, msg.mtext);
+ }
+
+ for (int i = 0; i < NCHILD; i++) {
+ msg.mtype = 222;
+ memset(msg.mtext, '\0', MSGSIZE);
+ sprintf(msg.mtext, "Message %d", i);
+ // send message to other child processes
+ msgsnd(msgid, &msg, MSGSIZE, 0);
+
+ // display the message
+ printf("[%d] Data sent is : %s \n", i, msg.mtext);
+ }
+
+ for (int i = 0; i < NCHILD; i++) {
+ msg.mtype = 333;
+ memset(msg.mtext, '\0', MSGSIZE);
+ sprintf(msg.mtext, "Bye %d", i);
+ // send message to other child processes
+ msgsnd(msgid, &msg, MSGSIZE, 0);
+
+ // display the message
+ printf("[%d] Data sent is : %s \n", i, msg.mtext);
+ }
+
+ exit(0);
+ } else if (pid1 < 0) {
+ printf("fork1 error\n");
+ return -1;
+ }
+
+ // receiver child processes
+ for (int j = 0; j < NCHILD; j++) {
+ if ((pid2 = fork()) == 0) {
+ // receive message
+ msgrcv(msgid, &msg, MSGSIZE, +222, 0);
+
+
+ // display the message
+ printf("[%d] Data received is : %s \n", j, msg.mtext);
+
+ exit(0);
+ } else if (pid2 < 0) {
+ printf("fork2 error\n");
+ return -1;
+ }
+ }
+
+ while (wait(NULL) > 0);
+
+ // to destroy the message queue
+ msgctl(msgid, IPC_RMID, NULL);
+
+
+ return 0;
+} \ No newline at end of file