Dmitry Soldatov avatar Dmitry Soldatov committed 4e18b99

Improved code

Comments (0)

Files changed (5)

Binary file modified.

Binary file modified.

     unsigned flags = 0;
     char buf[100];
     struct _msg_info mi;
-    itoa(getpid(), buf, 10);
-    printf("M1: pid=%s\n", buf);
+    int rcv_id, coid, chid;
+    int processes_to_interact = 2;
     // Creating channel
-    int chid = ChannelCreate(flags);
-    itoa(chid, buf, 10);
-    printf("M1: chid=%s\n", buf);
+    int mychid = ChannelCreate(flags);
+    itoa(mychid, buf, 10);
     // Spawning P1 and P2; passing channel ID as argument
     pid_t pid_p2 = spawnl(P_NOWAIT, "/home/boh/lab1/m2", (const char*) buf, NULL);
-    pid_t pid_p3 = 0;//spawnl(P_NOWAIT, "/home/boh/lab1/m3", (const char*) buf, NULL);
-    // Receiving P? channel ID
-    int rcv_id = MsgReceive(chid, buf, 1, NULL);
-    printf("M1: Received chid=%s\n", buf);
-    MsgInfo(rcv_id, &mi);
-    MsgReply(rcv_id, 0, buf, 0);
-    printf("M1: Replied\n");
-    chid = atoi(buf);
-    // Connecting to P? channel
-    printf("M1: Connecting to pid=%d chid=%d\n",mi.pid, chid);
-    int coid = ConnectAttach(0, mi.pid, chid, 0, flags);
-    printf("M1: Connected to chid=%d\n",chid);
-    // Sending message to P?
-    if (mi.pid == pid_p2)
-        strcpy(buf, "P1 send message to P2\n");
-    else if (mi.pid == pid_p3)
-        strcpy(buf, "P1 send message to P3\n");
-    else
-        printf("M1: WTF? Communicating with unknown process!");
-    printf("M1: Sending %s\n", buf);
-    MsgSend(coid, buf, sizeof(buf), buf, sizeof(buf));
-    // Receiving reply
-    //MsgReceive(coid, &buf, sizeof(buf), NULL);
-    printf("M1: Got reply: %s\n", buf);
+    pid_t pid_p3 = spawnl(P_NOWAIT, "/home/boh/lab1/m3", (const char*) buf, NULL);
+
+    while (processes_to_interact > 0) {
+        rcv_id = -1;
+        // Receiving P? channel ID
+        rcv_id = MsgReceive(mychid, buf, sizeof(buf), NULL);
+        MsgInfo(rcv_id, &mi);
+        MsgReply(rcv_id, 0, buf, 0);
+        chid = atoi(buf);
+        // Connecting to P? channel
+        coid = ConnectAttach(0, mi.pid, chid, 0, flags);
+        // Sending message to P?
+        if (mi.pid == pid_p2)
+            strcpy(buf, "P1 send message to P2");
+        else if (mi.pid == pid_p3)
+            strcpy(buf, "P1 send message to P3");
+        else
+            strcpy(buf, "P1 send message to P?");
+        MsgSend(coid, buf, sizeof(buf), buf, sizeof(buf));
+        printf("M1: %s\n", buf);
+        ConnectDetach(coid);
+        MsgReceive(mychid, buf, sizeof(buf), NULL);
+        processes_to_interact--;
+    }
+
     return EXIT_SUCCESS;
 }
 
 #include <string.h>
 
 int main(int argc, char *argv[]) {
-    int i = 0;
-    while (i<argc) {
-        printf("M2: arg[%d] = %s\n", i, argv[i]);
-        i++;
-    }
     unsigned flags = 0;
     char buf[100];
-    itoa(getppid(), buf, 10);
-    printf("M2: ppid=%s\n", buf);
     int chid = ChannelCreate(flags);
-    itoa(chid, buf, 10);
-    printf("M2: chid=%s\n",buf);
     int pchid = atoi(argv[0]);
-    printf("M2: pchid=%d\n",pchid);
     int coid = ConnectAttach(0, getppid(), pchid, 0, flags);
     itoa(chid, buf, 10);
     MsgSend(coid, buf, 100, buf, 100);
-    printf("M2: Sent chid=%s to M1\n", buf);
-    MsgReceive(coid, buf, 0, NULL);
-    //ConnectDetach(coid);
-    //coid = ConnectAttach(0, getppid(), chid, 0, flags);
     int rcv_id = MsgReceive(chid, buf, sizeof(buf), NULL);
-    printf("M2: Received from M1: %s\n", buf);
-    strcpy(buf, "P2 OK\n");
+    printf("M2: %s\n", buf);
+    strcpy(buf, "P2 OK");
     MsgReply(rcv_id, 0, buf, sizeof(buf));
-    printf("M2: Replied\n");
-//    MsgSend(coid, &buf, 100, NULL, 0);
     return EXIT_SUCCESS;
 }
 
 int main(int argc, char *argv[]) {
     unsigned flags = 0;
     char buf[100];
-    itoa(getppid(), buf, 10);
-    printf("M3: ppid=%s\n", buf);
     int chid = ChannelCreate(flags);
-    itoa(chid, buf, 10);
-    printf("M3: pchid=%s\n",buf);
     int pchid = atoi(argv[0]);
     int coid = ConnectAttach(0, getppid(), pchid, 0, flags);
     itoa(chid, buf, 10);
-    MsgSend(coid, buf, sizeof(buf), buf, sizeof(buf));
-    printf("M3: Sent chid=%s to M1\n", buf);
-    ConnectDetach(coid);
-    MsgReceive(chid, buf, sizeof(buf), NULL);
+    MsgSend(coid, buf, 100, buf, 100);
+    int rcv_id = MsgReceive(chid, buf, sizeof(buf), NULL);
     printf("M3: %s\n", buf);
-    strcpy(buf, "P3 OK\n");
-    MsgSend(chid, buf, sizeof(buf), buf, sizeof(buf));
+    strcpy(buf, "P3 OK");
+    MsgReply(rcv_id, 0, buf, sizeof(buf));
     return EXIT_SUCCESS;
 }
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.