home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / sys / sgi / misc / 232 < prev    next >
Encoding:
Text File  |  1993-01-11  |  6.9 KB  |  271 lines

  1. Path: sparky!uunet!cis.ohio-state.edu!rutgers!cmcl2!adm!news
  2. From: smant@nlr.nl (smant g.a.)
  3. Newsgroups: comp.sys.sgi.misc
  4. Subject: Re: 4Dgifts: Shared Memory example
  5. Message-ID: <34946@adm.brl.mil>
  6. Date: 11 Jan 93 12:45:04 GMT
  7. Sender: news@adm.brl.mil
  8. Lines: 261
  9.  
  10. According to Dave Englund:
  11. > I'm looking at the sample codes in the
  12. > /usr/people/4Dgifts/examples/unix  directory. The one problem I'm
  13. > having is with the "shmem1" and "shmem2" example. This does nothing. I
  14. > looked at the code and see that something is supposed to print
  15. > somewhere, but that's not what happens. I'm running under 4.0.4 on a
  16. > Crimson XS24. 
  17. > All of the other samples in this directory seem to work fine. Any
  18. > words of wisdom? Thanks in advance...
  19.  
  20. A few months ago I also had some problems getting the sem_op1.c & sem_op2.c
  21. to run correctly. There are some typos in the examples:
  22.  
  23. ===========================================================================
  24.  
  25. /*
  26.  *   sem_op1.c:
  27.  *
  28.  *    This set of programs (sem_op1 and sem_op2) illustrates a simple
  29.  *  implementation of semaphore operations.  To demonstrate, first 
  30.  *  execute sem_op1 (in the background ("sem_op1 &"), or from another
  31.  *  window/shell), and then execute sem_op2.
  32.  *
  33.  *  References:  INTRO(2), SEMGET(2), SEMOP(2), SEMCTL(2), SIGNAL(2)
  34.  *
  35.  *                                     George Smith - 1987
  36.  *
  37.  */
  38.  
  39. #include <stdio.h>
  40. #include <sys/signal.h>
  41. #include <sys/types.h>
  42. #include <sys/ipc.h>
  43. #include <sys/sem.h>
  44.  
  45. #define SEMKEY                10  /* Why not ? */
  46. #define Program               "sem_op1"
  47.  
  48. /*
  49.  *      This is the declaration as it appears in sem.h:
  50.  *  struct sembuf {
  51.  *      ushort  sem_num;        / semaphore # /
  52.  *      short   sem_op;         / semaphore operation /
  53.  *      short   sem_flg;        / operation flags /
  54.  *  };
  55.  */
  56.  
  57. struct sembuf sem_ar;
  58. /*
  59.  the int should be ushort here, and the 2 should be 1:
  60.  
  61. int  sig_array[2];
  62. ^^^           ^^^
  63.  */
  64. ushort sig_array[1];
  65. int i, cleanup();
  66. unsigned int count;
  67. int semid;
  68.  
  69.  
  70. main ()
  71. {
  72.  
  73. /* Open semaphore */
  74. /*
  75.  the 2 should be 1 here:
  76.         if( ( semid=semget(SEMKEY,2,0777|IPC_CREAT)) == -1) {
  77.                                  ^^^
  78.  */
  79.         if( ( semid=semget(SEMKEY,1,0777|IPC_CREAT)) == -1) {
  80.                 printf("\n Can't open semaphor ( %s ) \n",Program);
  81.                 fflush(stdout);
  82.                 exit(-1);
  83.         }
  84.  
  85. /* Catch signals */
  86. /*      for(i=1; i<21 ; i++ ) 
  87.                 signal(i , cleanup );
  88. */
  89.  
  90.         
  91. /* Set up initial value of 1 , 1 */
  92. /*
  93.  the array is now of length 1,
  94.  and should be initialized to 0 in stead of 1:
  95.  
  96.         sig_array[0] = sig_array[1] = 1 ;
  97.                      ^^^^^^^^^^^^^^^ ^^^
  98.         if( semctl(semid,2,SETALL,sig_array) == -1 ) {
  99.                         ^^^
  100.  */
  101.  
  102.         sig_array[0] = 0 ;
  103.         if( semctl(semid,1,SETALL,sig_array) == -1 ) {
  104.                 printf("\n Can't do semctl (%s)\n",Program);
  105.                 fflush(stdout);
  106.                 cleanup();
  107.         }
  108.  
  109. /* Pause until sig_array[0] increments  */
  110.         sem_ar.sem_num = 0 ;
  111.         sem_ar.sem_op = -1 ;
  112.         sem_ar.sem_flg = SEM_UNDO ;
  113.  
  114.         if(semop(semid, &sem_ar , 1 ) == -1 ) {
  115.                 printf("\n Can't do semop ( %s ) \n" , Program );
  116.                 fflush(stdout);
  117.                 cleanup() ;
  118.         }
  119.  
  120.  
  121.         printf("\n **> Was waiting on Increment , Now i am awake ( %s )\n",
  122.                                                                 Program);
  123.         fflush(stdout);
  124.  
  125. /* Remove semid from system */
  126.  
  127.         cleanup();
  128. }
  129.  
  130.  
  131. cleanup() {
  132.  
  133. /*
  134.  the number of semaphores has been reduced to 1:
  135.         semctl(semid, 2 , IPC_RMID,0 );
  136.                     ^^^^^
  137.  */
  138.         semctl(semid, 1 , IPC_RMID,0 );
  139.         exit(1) ;
  140. }        
  141.  
  142. ===========================================================================
  143.  
  144. /* 
  145.  *   sem_op2.c:
  146.  *
  147.  *     This set of programs (sem_op1 and sem_op2) illustrates a simple 
  148.  *  implementation of semaphore operations.  To demonstrate, first 
  149.  *  execute sem_op1 (in the background--i.e. "sem_op1 &"--or from another
  150.  *  window/shell, and then execute sem_op2.
  151.  *
  152.  *  References:  INTRO(2), SEMGET(2), SEMOP(2), SEMCTL(2), SIGNAL(2)
  153.  *
  154.  *                                     George Smith - 1987
  155.  */
  156.  
  157. #include <stdio.h>
  158. #include <sys/signal.h>
  159. #include <sys/types.h>
  160. #include <sys/ipc.h>
  161. #include <sys/sem.h>
  162.  
  163. #define SEMKEY                10  /* Why not ? */
  164. #define Program                "sem_op2"
  165.  
  166. /* 
  167.  *     This is the declaration as it appears in sem.h:
  168.  *
  169.  *  struct sembuf {
  170.  *      ushort  sem_num;        / semaphore # /
  171.  *      short   sem_op;         / semaphore operation /
  172.  *      short   sem_flg;        / operation flags /
  173.  *  };
  174.  *
  175.  */
  176.  
  177. struct sembuf sem_ar;
  178. /*
  179.  the int should be ushort here, and the 2 should be 1:
  180.  
  181. int  sig_array[2];
  182. ^^^           ^^^
  183.  */
  184. ushort  sig_array[1];
  185. int out_val[2];
  186. int i;
  187. unsigned int count;
  188. int semid;
  189. int val, num;
  190.  
  191.  
  192. main () {
  193.  
  194.         void cleanup();
  195. /* Open semaphore */
  196. /*
  197.  the 2 should be 1 here:
  198.         if( ( semid=semget(SEMKEY,2,0777|IPC_CREAT)) == -1) {
  199.                                  ^^^
  200.  */
  201.         if( ( semid=semget(SEMKEY,1,0777|IPC_CREAT)) == -1) {
  202.                 printf("\n Can't open semaphor ( %s ) \n",Program);
  203.                 fflush(stdout);
  204.                 exit(-1);
  205.         }
  206.  
  207. /* Catch signals */
  208.         for(i=1; i<21 ; i++ ) 
  209.                 signal(i , cleanup);
  210.  
  211.         
  212.  
  213. /* Get the value of the semaphores and print */
  214. /*
  215.  the number of semaphores has been reduced to 1:
  216.         for( i=0 ; i<2 ; i++ ) {
  217.  */
  218.         for( i=0 ; i<1 ; i++ ) {
  219.                 if( (val =semctl(semid, i , GETVAL, out_val )) == -1 ){
  220.                         printf("\n Can't read semval ( %s ) \n",Program);
  221.                         fflush(stdout);
  222.                         exit(-1);
  223.                 }
  224.                 printf("\n Semval [%d]: %d ",i,val );
  225.                 fflush(stdout);
  226.         }
  227.  
  228.         printf("\n");
  229.         fflush(stdout);
  230.  
  231. /* Incremewnt 0  */
  232.         sem_ar.sem_num = 0 ;
  233.         sem_ar.sem_op = 1;
  234.         sem_ar.sem_flg = SEM_UNDO ;
  235.         if( semop(semid, &sem_ar , 1 ) == -1 ) {
  236.                 printf("\n Can't do semop ( %s ) " ,Program );
  237.                 fflush(stdout);
  238.                 cleanup();
  239.         }
  240.         
  241. /* sleep then die */
  242.         sleep(1);
  243.         cleanup();
  244. }
  245.  
  246.  
  247. void cleanup() {
  248.  
  249. /*
  250.  the number of semaphores has been reduced to 1:
  251.         semctl(semid, 2 , IPC_RMID,0 );
  252.                     ^^^^^
  253.  */
  254.         semctl(semid, 1 , IPC_RMID,0 );
  255.         exit(1) ;
  256. }        
  257.  
  258. ===========================================================================
  259.  
  260. -- 
  261. Best Regards, Geert Albert Smant.
  262. ------------------------------------------------------------------------
  263. Ing. G.A. Smant (Informatics Division, IR-NOP)
  264. National Aerospace Laboratory (NLR)           Email:   smant@nlr.nl
  265. Voorsterweg  31,  8316 PR Marknesse           Phone: (+31)(0)5274 - 8418
  266. P.O. Box    153,  8300 AD Emmeloord                  (+31)(0)5274 - 8444
  267. The Netherlands                               Fax:   (+31)(0)5274 - 8210
  268. ------------------------------------------------------------------------
  269.