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

  1. Newsgroups: comp.unix.programmer
  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: implementation of semaphores in unix 
  5. Message-ID: <1992Aug27.115017.21484@cam.compserv.utas.edu.au>
  6. Sender: kleung@esk.compserv.utas.edu.au 
  7. Organization: University of Tasmania at Launceston
  8. References: <1992Aug21.140244.12486@walter.cray.com> 
  9. Date: Thu, 27 Aug 92 11:50:17 GMT
  10. Lines: 74
  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. Regards
  21. Keith LEUNG
  22. kleung@esk.compserv.utas.edu.au
  23.  
  24. #include <stdio.h>
  25. #include <sys/types.h>
  26. #define N              6
  27. int  s[N], me;
  28. enum status {nthinking,vhungry,neating} ;
  29. enum status p[N];
  30. main()
  31. {
  32.   int i, ;
  33.   void  PHILOSOPHER(), 
  34.         think(), eat(), hungry();
  35.  
  36.   me = 1;    /* initial open state of main semaphore */
  37.  
  38.   for (i=1;i<N;i++) /*initialise 1-5 philosophers to thinking*/
  39.       { s[i] = 0; p[i] = nthinking;}  /* private semaphore s[i]=0,close*/
  40.  
  41.   for (i=1; i<N; i++){
  42.     if (fork() == 0) PHILOSOPHER(i);
  43.   }
  44.         ..
  45.         ..
  46.   kill(0,9);
  47. }
  48. void PHILOSOPHER(i)
  49. int i;
  50. {
  51.         think(i);
  52.       -----------waitfor(me); 
  53.       !     /* see whether the sem. is open(me=1) or close(me=0)
  54.       !        if me=1, pass thru, and set the me back to 0   */ 
  55.       !        hungry(i);
  56.       !         philosopher i = hungry
  57.       !         printf("\nphil %d has gone into c.s.",i);
  58.       !         check if the lefthand and righthand philosopher 'not eating'  
  59.       !         if it is so,  set itself = eating, release private sem.
  60.       !         of itself : signalto(p[i]) -- supposed to set p[i] = 1
  61.       !         but didn't...... 
  62.       ---------    signalto(me);
  63.         waitfor(s[i]);
  64.         eat(i);
  65.       --------    waitfor(me);
  66.       !           ..
  67.       !           ..
  68.       !         activate the neigbouring philosophers (ready to eat)
  69.       !           ..
  70.       !           .. 
  71.       ----------signalto(me);
  72.                 printf("phil %d has finished eating and asking other to eat" );
  73.  
  74. waitfor(i)
  75. int i;
  76. {
  77.     if (i >= 1)   {  i--; }
  78.     else
  79.       {  waitfor(i); } 
  80.     return i;
  81. }
  82. signalto(i)
  83. int i;
  84.     {return i++;}
  85.  
  86.