[<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 ςΕΫΕΞΙΡ

production.h:

   1     #define NWIDGETS 5
   2     #define PARTA 0
   3     #define PARTB 1
   4     #define PARTC 2
   5     #define SUB1  3
   6     #define HOLDER 0

ιϊηοτοχιτεμψ χιδφετοχ, sem_prodline.c:
   1 #include <signal.h>
   2 #include <sys/types.h>
   3 #include <unistd.h>
   4 #include <stdlib.h>
   5 #include <stdio.h>
   6 #include <sys/ipc.h>
   7 #include <sys/sem.h>
   8 #include "production.h"
   9 static int semprodn;
  10 static void endsim();
  11
  12 main(int argc, char *argv[])
  13 {
  14  int widget;
  15  char asc_prod_key[20];
  16  static struct sembuf partc_sub1[2] = {
  17      { PARTC, -1, SEM_UNDO},
  18      { SUB1, -1, SEM_UNDO}
  19  };
  20
  21 if((semprodn=semget(IPC_PRIVATE,4,IPC_CREAT|0640))==-1
  22  {
  23      printf("Can't get production line semaphore set\n
  24      exit(1);
  25  }
  26  sigset(SIGINT, endsim);
  27  sprintf(asc_prod_key, "%d", semprodn);
  28
  29
  30  if( fork() == 0) {
  31      execl("prodpart", "a", asc_prod_key, (char *)0);
  32      perror("a");
  33      exit(1);
  34  }
  35
  36  if( fork() == 0) {
  37      execl("prodpart", "b", asc_prod_key, (char *)0);
  38      perror("b");
  39      exit(1);
  40  }
  41

  42  if( fork() == 0) {
  43      execl("prodpart", "c", asc_prod_key, (char *)0);
  44      perror("c");
  45      exit(1);
  46  }
  47
  48  if( fork() == 0) {
  49      execl("prodsub1","prodsub1",asc_prod_key,(char*)0
  50      perror("prodsub1");
  51      exit(1);
  52  }
  53
  54
  55  for(widget=1; widget < NWIDGETS; widget++) {
  56      if (semop(semprodn, partc_sub1, 2) == -1){
  57          perror(argv[0]);
  58          exit(1);
  59      };
  60      printf("%s: ready to make widget #%d\n",
  61          argv[0], widget);
  62  }
  63
  64  endsim();
  65
  66 }
  67
  68 static void endsim()
  69 {
  70  semctl(semprodn, IPC_RMID, 0);
  71  kill(0, SIGTERM);
  72  exit(0);
  73 }
ιϊηοτοχιτεμψ δεταμεκ, prodpart.c:
   1 #include <sys/types.h>
   2 #include <unistd.h>
   3 #include <stdio.h>
   4 #include <stdlib.h>
   5 #include <sys/ipc.h>
   6 #include <sys/sem.h>
   7 #include "production.h"
   8
   9 main(int argc, char *argv[])
  10 {
  11      int semprodn;
  12      int unit = 0;
  13      static struct sembuf parti = {HOLDER, 1, SEM_UNDO
  14      static int prodtimeabc[3] = {2, 3, 4};
  15      ushort index;
  16
  17      semprodn = atoi( argv[1] );
  18      index = argv[0][0] - 'a'; /* argv[0] == [abc] */
  19      parti.sem_num = index;
  20      setbuf(stdout,NULL);
  21
  22      while( 1 )
  23           {
  24                sleep(prodtimeabc[index]);
  25                printf("%s: producing unit #%d\n",
  26                     argv[0], ++unit);
  27                semop(semprodn, &parti, 1);
  28           }
  29 }
ιϊηοτοχιτεμψ νοδυμρ 1, prodsub1.c:
   1 #include <sys/types.h>
   2 #include <stdio.h>
   3 #include <stdlib.h>
   4 #include <unistd.h>
   5 #include <sys/ipc.h>
   6 #include <sys/sem.h>
   7 #include <errno.h>
   8 #include "production.h"
   9
  10 main(int argc, char *argv[])
  11 {
  12     int semprodn;
  13     int unit = 0;
  14     static struct sembuf part_ab[2] = {
  15                        { PARTA, -1, SEM_UNDO },
  16                        { PARTB, -1, SEM_UNDO }
  17              };
  18
  19     static struct sembuf sub1 = { SUB1, 1, SEM_UNDO };
  20
  21     semprodn = atoi( argv[1] );
  22     setbuf(stdout,NULL);
  23
  24   while( 1 )
  25   {
  26       if(semop(semprodn, &part_ab[0], 2) == -1) {
  27            perror(argv[0]);
  28            exit(1);
  29       }
  30       printf("%s:    producing    sub-assembly #%d\n",
  31            argv[0], ++unit);
  32       if (semop(semprodn, &sub1, 1) == -1) {;
  33            fprintf(stderr, "sub1 error\n");
  34            perror(argv[0]);
  35            exit(1);
  36       }
  37   }
  38 }
χωϊοχ:
$ sem_prodline
a: producing unit #1
b: producing unit #1
c: producing unit #1
prodsub1: producing sub-assembly #1
sem_prodline: ready to make widget #1
...
$