home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / sys / sun / misc / 3401 < prev    next >
Encoding:
Internet Message Format  |  1992-07-30  |  4.5 KB

  1. Xref: sparky comp.sys.sun.misc:3401 comp.unix.misc:3063
  2. Path: sparky!uunet!cs.utexas.edu!unisql!keskin
  3. From: keskin@unisql.UUCP (Ali Keskin)
  4. Newsgroups: comp.sys.sun.misc,comp.unix.misc
  5. Subject: problem with semaphors
  6. Message-ID: <3356@unisql.UUCP>
  7. Date: 30 Jul 92 16:29:16 GMT
  8. Reply-To: keskin@unisql.UUCP (Ali Keskin)
  9. Followup-To: comp.sys.sun.misc
  10. Organization: UniSQL, Inc., Austin, Texas, USA
  11. Lines: 130
  12.  
  13.  
  14. I need to protect a shared memory segment which is shared by a parent
  15. process and n forked child processes from mutual access and the best
  16. candidate seemed to be semaphors. 
  17.  
  18. But, I am running into a problem with Sun's provided semaphore functions
  19. (Sun OS 4.1.1). The problem is that on creation the semaphore value is set 
  20. to -1 (if it was 0, there wouldn't be this problem). So, for further
  21. semaphore operations to work properly it needs to be reinitialized to 0 or 
  22. some other positive value. But, the system routine to do that which is 
  23. called semctl() gives bus error or segmentation fault even tough called 
  24. the way required. Two sets of semaphore interface routines given in Steven's 
  25. Unix Network Programming book cause the same problem. It seems that system 
  26. routine semctl() does not work properly (?!). The problematic line of
  27. code is marked below:
  28.  
  29. function prototype is as follows:
  30.      int semctl(int semid, int semnum, int cmd, union semun arg)
  31.      union semun {
  32.           val;
  33.           struct semid_ds *buf;
  34.           ushort *array;
  35.      } arg;
  36.  
  37. -----
  38.  
  39. #define    SEMKEY    123456L    /* key value for semget() */
  40. #define    PERMS    0666
  41.  
  42. static struct sembuf    op_lock[2] = {
  43.     0, 0, 0,    /* wait for sem#0 to become 0 */
  44.     0, 1, SEM_UNDO    /* then increment sem#0 by 1 */
  45. };
  46.  
  47. static struct sembuf    op_unlock[1] = {
  48.     0, -1, (IPC_NOWAIT | SEM_UNDO)    /* decrement sem#0 by 1 (sets it to 0) */
  49. };
  50.  
  51. int    semid = -1;    /* semaphore id */
  52.  
  53. int
  54. sem_create(void) {
  55.   int id, semval;
  56.   union semun tmp_val;
  57.  
  58.         if ( (id = semget(SEMKEY, 1, IPC_CREAT | PERMS)) < 0) {
  59.         printf("semget error\n");
  60.                 return -1;
  61.         }
  62.  
  63.         tmp_val.val = 0;
  64.     if ((semval = semctl(id, 1, GETVAL, tmp_val)) != 0) {
  65.  
  66.            /* semval == -1, initialized to -1, so try to set it to 0 */
  67.  
  68.              tmp_val.val = 0;
  69.              if (semctl(id, 1, SETALL, tmp_val) < 0) {   <---- Bus Error/SegFlt
  70.  
  71.         printf("semget initialization error\n");
  72.                 return -1;
  73.              }
  74.          semval = semctl(id, 1, GETVAL, tmp_val);
  75.         }
  76.  
  77.   return id;
  78. }
  79.  
  80. void
  81. sem_wait(int id)
  82. {
  83.     if (semop(id, &op_lock[0], 2) < 0) {
  84.         printf("semop lock error\n");
  85.                 return;
  86.         }
  87.  
  88. }
  89.  
  90. void
  91. sem_signal(int id)
  92. {
  93.     if (semop(id, &op_unlock[0], 1) < 0) {
  94.         printf("semop unlock error\n");
  95.                 return;
  96.         }
  97.  
  98. }
  99.  
  100.  
  101. void
  102. sem_rm(int id)
  103. {
  104.   union semun tmp_val;
  105.  
  106.         tmp_val.val = 0;
  107.     if (semctl(id, 0, IPC_RMID, tmp_val) < 0) {
  108.         printf("can't IPC_RMID\n");
  109.                 return;
  110.         }
  111. }
  112.  
  113.  
  114. if you can comment on the cause or solution (or even getting around) of
  115. the problem, or you suggest another candidate mechanism for protecting shared
  116. memory among parent and child processing, would you please email it to 
  117. _my personal email address_ (I don't read/follow this newsgroup). 
  118. You may post it as well.
  119.  
  120. thanks in advance.
  121.  
  122. -ali
  123.  
  124.  ****************************************************************************
  125.  * - ali keskin                                                             *
  126.  *      _____                                                               *
  127.  *      |   |               UniSQL, Inc.                                    *
  128.  *      |   \_______        9390 Research Blvd.                             *
  129.  *  ____|          |        Kaleido II, Suite 200                           *
  130.  *  \  ___        /         Austin, TX 78759                                *
  131.  *   \/   \   *  /          (512) 343-7297                                  *
  132.  *         \_   /                                                           *
  133.  *           \_/                                                            *
  134.  *                                                                          *
  135.  *   Internet: unisql!keskin@cs.utexas.edu                                  *
  136.  *   UUCP: {uunet, cs.utexas.edu}!unisql!keskin                             *
  137.  *                                                                          *
  138.  ****************************************************************************
  139. -- 
  140.  * - ali keskin                                                             *
  141.  *   Internet: unisql!keskin@cs.utexas.edu                                  *
  142.  *   UUCP: {uunet, cs.utexas.edu}!unisql!keskin                             *
  143.