home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / unix / question / 10426 < prev    next >
Encoding:
Text File  |  1992-08-27  |  2.4 KB  |  84 lines

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!munnari.oz.au!newsroom.utas.edu.au!cam!esk!kleung
  3. From: kleung@esk.compserv.utas.edu.au (Kam  Leung)
  4. Subject: implement semaphores in unix 
  5. Message-ID: <1992Aug27.115307.21550@cam.compserv.utas.edu.au>
  6. Sender: kleung@esk.compserv.utas.edu.au 
  7. Organization: University of Tasmania at Launceston
  8. References: <92234.182251FFAAC09@cc1.kuleuven.ac.be> <ac#n06l.bosak@netcom.com> <ceYpFNG00VoxBKON4O@andrew.cmu.edu>
  9. Date: Thu, 27 Aug 92 11:53:07 GMT
  10. Lines: 72
  11.  
  12.  
  13. Dear guys,
  14.       I have a difficulty in enforcing the mutual exclusion between
  15. 5 philosophers because I couldn't effectively change the global 
  16. variables such as the major semaphore (me), and the private semaphore
  17. (s[i]).  Could anybody suggest an answer for me or comment on my logic
  18. below.  Your kind attention is much appreciated.  Thanks...
  19.  
  20.   kleung@esk.compserv.utas.edu.au
  21.  
  22. #include <stdio.h>
  23. #include <sys/types.h>
  24. #define N              6
  25. int  s[N], me;
  26. enum status {nthinking,vhungry,neating} ;
  27. enum status p[N];
  28. main()
  29. {
  30.   int i, ;
  31.   void  PHILOSOPHER(), 
  32.         think(), eat(), hungry();
  33.  
  34.   me = 1;    /* initial open state of main semaphore */
  35.  
  36.   for (i=1;i<N;i++) /*initialise 1-5 philosophers to thinking*/
  37.       { s[i] = 0; p[i] = nthinking;}  /* private semaphore s[i]=0,close*/
  38.  
  39.   for (i=1; i<N; i++){
  40.     if (fork() == 0) PHILOSOPHER(i);
  41.   }
  42.         ..
  43.         ..
  44.   kill(0,9);
  45. }
  46. void PHILOSOPHER(i)
  47. int i;
  48. {
  49.         think(i);
  50.       -----------waitfor(me); 
  51.       !     /* see whether the sem. is open(me=1) or close(me=0)
  52.       !        if me=1, pass thru, and set the me back to 0   */ 
  53.       !        hungry(i);
  54.       !         philosopher i = hungry
  55.       !         printf("\nphil %d has gone into c.s.",i);
  56.       !         check if the lefthand and righthand philosopher 'not eating'  
  57.       !         if it is so,  set itself = eating, release private sem.
  58.       !         of itself : signalto(p[i]) -- supposed to set p[i] = 1
  59.       !         but didn't...... 
  60.       ---------    signalto(me);
  61.         waitfor(s[i]);
  62.         eat(i);
  63.       --------    waitfor(me);
  64.       !           ..
  65.       !           ..
  66.       !         activate the neigbouring philosophers (ready to eat)
  67.       !           ..
  68.       !           .. 
  69.       ----------signalto(me);
  70.                 printf("phil %d has finished eating and asking other to eat" );
  71.  
  72. waitfor(i)
  73. int i;
  74. {
  75.     if (i >= 1)   {  i--; }
  76.     else
  77.       {  waitfor(i); } 
  78.     return i;
  79. }
  80. signalto(i)
  81. int i;
  82.     {return i++;}
  83.  
  84.