Advertisement
Guest User

C source code

a guest
Dec 14th, 2010
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.73 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/ipc.h>
  4. #include <sys/sem.h>
  5. #include <stdlib.h>
  6. #include <mqueue.h>
  7.  
  8. #define PROCESS_COUNT 10
  9.  
  10. struct my_msgbuf {
  11.    long mtype;
  12.    int marker;
  13. };
  14.  
  15. struct sembuf sops[2];
  16. struct my_msgbuf mqbuf;
  17.  
  18. int semid,msqid;
  19.  
  20. void child(int procnum);
  21.  
  22. int main(){
  23.  int pid,i;
  24.  int flg = 0600 | IPC_CREAT;
  25.  key_t skey = 999, mkey=888;
  26.  mqbuf.mtype = 1;
  27.  mqbuf.marker = 0;
  28.  
  29.  /* create a semaphore set with 10 semaphore: */
  30.  if ((semid = semget(skey, PROCESS_COUNT, flg)) == -1) {
  31.   perror("semget");
  32.   exit(1);
  33.  }
  34.  
  35.  if ((msqid = msgget(mkey, flg)) == -1) {
  36.   perror("msgget");
  37.   exit(1);
  38.  }
  39.  
  40.  printf("Go...\n");
  41.  
  42.  for(i = 0; i < PROCESS_COUNT; i++){
  43.  
  44.   /*locks the semaphore*/
  45.   sops[0].sem_num = i;
  46.   sops[0].sem_op = 1;
  47.  
  48.   if (semop(semid, sops, 1) == -1) {
  49.    perror("semop");
  50.    exit(1);
  51.   }
  52.   printf("Parent sem %d locked\n",i);
  53.  
  54.   pid = fork();
  55.   if(pid == 0){
  56.    child(i);
  57.    exit(1);
  58.   }
  59.  }
  60.  
  61.   printf("Parent sent message\n");
  62.   if (msgsnd(msqid, &mqbuf, sizeof(mqbuf), 0) == -1)
  63.    perror("msgsnd");
  64.  
  65.  for(i = 0; i < PROCESS_COUNT; i++){
  66.  
  67.   printf("Parent sem %d unlocked\n",i);
  68.   /*unlcok*/
  69.   sops[0].sem_num = i;
  70.   sops[0].sem_op = -1;
  71.   /*wait for child to finish*/
  72.   sops[1].sem_num = i;
  73.   sops[1].sem_op = 0;
  74.   if (semop(semid, sops, 2) == -1) {
  75.    perror("semop");
  76.    exit(1);
  77.   }
  78.  
  79.   /*lock*/
  80.   printf("Parent sem %d wait\n",i);
  81.  }
  82.  
  83.  wait();
  84.  
  85.  if (msgrcv(msqid, &mqbuf, sizeof(mqbuf), 0, 0) == -1) {
  86.   perror("msgrcv");
  87.   exit(1);
  88.  }
  89.  
  90.  printf("Result %d\n",mqbuf.marker);
  91.  
  92.  /* remove sem and mq*/
  93.  if (semctl(semid, 0, IPC_RMID) == -1) {
  94.   perror("semctl");
  95.   exit(1);
  96.  }
  97.  if (msgctl(msqid, IPC_RMID, NULL) == -1) {
  98.   perror("msgctl");
  99.   exit(1);
  100.  }
  101.  
  102.  return 0;
  103. }
  104.  
  105. void child(int procnum){
  106.  
  107.         /*waits for unlocking*/
  108.         sops[0].sem_num = procnum;
  109.         sops[0].sem_op = 0;
  110.         /*lock*/
  111.         sops[1].sem_num = procnum;
  112.         sops[1].sem_op = 1;
  113.         printf("Child sem %d wait\n",procnum);
  114.  
  115.         if (semop(semid, sops, 2) == -1) {
  116.           perror("semop");
  117.           exit(1);
  118.         }
  119.  
  120.         printf("Child sem locked %d\n",procnum);
  121.  
  122.         printf("Child %d receive message\n",procnum);
  123.         if (msgrcv(msqid, &mqbuf, sizeof(mqbuf), 0, 0) == -1) {
  124.          perror("msgrcv");
  125.          exit(1);
  126.         }
  127.         mqbuf.marker +=2;
  128.         printf("Child %d sent message\n",procnum);
  129.         if (msgsnd(msqid, &mqbuf, sizeof(mqbuf), 0) == -1)
  130.          perror("msgsnd");
  131.  
  132.         /*unlock*/
  133.         sops[0].sem_op = -1;
  134.  
  135.         if (semop(semid, sops, 1) == -1) {
  136.           perror("semop");
  137.           exit(1);
  138.         }
  139.  
  140.         printf("Child sem unlocked %d\n",procnum);
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement