daily pastebin goal
75%
SHARE
TWEET

Untitled

a guest Apr 17th, 2018 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ctype.h>
  2. #include <errno.h>
  3. #include <sys/types.h>
  4. #include <sys/wait.h>
  5. #include <errno.h>
  6. #include <sys/ipc.h>
  7. #include <sys/msg.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <unistd.h>
  12.  
  13. struct my_msgbuf {
  14.     long mtype;
  15.     char mtext[200];
  16. };
  17.  
  18. int checkCLArgs(char *num1, char *num2) {
  19.     int i;
  20.  
  21.     for(i = 0; num1[i] != '\0'; i++) {
  22.         if(!isdigit(num1[i]))
  23.             return 1;
  24.     }
  25.  
  26.     for(i = 0; num2[i] != '\0'; i++) {
  27.         if(!isdigit(num2[i]))
  28.             return 1;
  29.     }
  30.  
  31.     return 0;
  32. }
  33.  
  34. void error(const char *errMsg) {
  35.     fprintf(stderr, "%s\n", errMsg);
  36.     exit(-1);
  37. }
  38.  
  39. int main(int argc, char **argv) {
  40.  
  41.     if(argc != 3)
  42.         error("Usage: ./main N M\nN - number of cannibal processes, M - number of missionary processes");
  43.  
  44.     if(checkCLArgs(argv[1], argv[2]))
  45.         error("Please enter positive integers only!");
  46.  
  47.     int i;
  48.     pid_t pid;
  49.     struct my_msgbuf buf;
  50.     int msqid;
  51.     key_t key = (key_t) getuid();
  52.     char text[]="Hello from boat!\n";
  53.  
  54.     int N = (int) strtol(argv[1], (char **)NULL, 10);
  55.     int M = (int) strtol(argv[2], (char **)NULL, 10);
  56.  
  57.     if(N <= 0 || M <= 0)
  58.         error("Value of N or M is not greater than 0");
  59.  
  60.     printf("N = %d, M = %d\n", N, M);
  61.  
  62.     int cannibalArr[N];
  63.     int missionaryArr[M];
  64.  
  65.     for(i = 0; i < N; i++) {
  66.         pid = fork();
  67.  
  68.         switch(pid) {
  69.             case -1:
  70.                 error("Error while creating cannibal proccess!");
  71.             case 0:
  72.                 execl("./person", "C", NULL);
  73.                 exit(1);
  74.             default:
  75.                 continue;
  76.         }
  77.     }
  78.  
  79.     for(i = 0; i < M; i++) {
  80.         pid = fork();
  81.  
  82.         switch(pid) {
  83.             case -1:
  84.                 error("Error while creating missionary proccess!");
  85.             case 0:
  86.                 execl("./person", "M", NULL);
  87.                 exit(1);
  88.             default:
  89.                 continue;
  90.         }
  91.     }
  92.  
  93.     printf("Boat: created %d cannibals and %d missionaries\n", N, M);
  94.  
  95.     if ((msqid = msgget(key, 0600 | IPC_CREAT)) == -1) {
  96.         error("msgget");
  97.     }
  98.  
  99.     while(1) {
  100.         sleep(2);
  101.         if (msgsnd(msqid, (struct msgbuf *)&buf, strlen(text)+1, 0) == -1)
  102.             error("msgsnd");
  103.     }
  104.  
  105.     return 0;
  106. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top