From: keskin@unisql.UUCP (Ali Keskin)
Newsgroups: comp.sys.sun.misc,comp.unix.misc
Subject: problem with semaphors
Date: 30 Jul 92 16:29:16 GMT
Reply-To: keskin@unisql.UUCP (Ali Keskin)
Followup-To: comp.sys.sun.misc
Organization: UniSQL, Inc., Austin, Texas, USA


I need to protect a shared memory segment which is shared by a parent
process and n forked child processes from mutual access and the best
candidate seemed to be semaphors. 

But, I am running into a problem with Sun's provided semaphore functions
(Sun OS 4.1.1). The problem is that on creation the semaphore value is set 
to -1 (if it was 0, there wouldn't be this problem). So, for further
semaphore operations to work properly it needs to be reinitialized to 0 or 
some other positive value. But, the system routine to do that which is 
called semctl() gives bus error or segmentation fault even tough called 
the way required. Two sets of semaphore interface routines given in Steven's 
Unix Network Programming book cause the same problem. It seems that system 
routine semctl() does not work properly (?!). The problematic line of
code is marked below:

function prototype is as follows:
     int semctl(int semid, int semnum, int cmd, union semun arg)
     union semun {
          val;
          struct semid_ds *buf;
          ushort *array;
     } arg;

-----

#define	SEMKEY	123456L	/* key value for semget() */
#define	PERMS	0666

static struct sembuf	op_lock[2] = {
	0, 0, 0,	/* wait for sem#0 to become 0 */
	0, 1, SEM_UNDO	/* then increment sem#0 by 1 */
};

static struct sembuf	op_unlock[1] = {
	0, -1, (IPC_NOWAIT | SEM_UNDO)	/* decrement sem#0 by 1 (sets it to 0) */
};

int	semid = -1;	/* semaphore id */

int
sem_create(void) {
  int id, semval;
  union semun tmp_val;

    	if ( (id = semget(SEMKEY, 1, IPC_CREAT | PERMS)) < 0) {
		printf("semget error\n");
                return -1;
        }

        tmp_val.val = 0;
	if ((semval = semctl(id, 1, GETVAL, tmp_val)) != 0) {

           /* semval == -1, initialized to -1, so try to set it to 0 */

             tmp_val.val = 0;
             if (semctl(id, 1, SETALL, tmp_val) < 0) {   <---- Bus Error/SegFlt

		printf("semget initialization error\n");
                return -1;
             }
	     semval = semctl(id, 1, GETVAL, tmp_val);
        }

  return id;
}

void
sem_wait(int id)
{
	if (semop(id, &op_lock[0], 2) < 0) {
		printf("semop lock error\n");
                return;
        }

}

void
sem_signal(int id)
{
	if (semop(id, &op_unlock[0], 1) < 0) {
		printf("semop unlock error\n");
                return;
        }

}


void
sem_rm(int id)
{
  union semun tmp_val;

        tmp_val.val = 0;
	if (semctl(id, 0, IPC_RMID, tmp_val) < 0) {
		printf("can't IPC_RMID\n");
                return;
        }
}


if you can comment on the cause or solution (or even getting around) of
the problem, or you suggest another candidate mechanism for protecting shared
memory among parent and child processing, would you please email it to 
_my personal email address_ (I don't read/follow this newsgroup). 
You may post it as well.

thanks in advance.

-ali

 ****************************************************************************
 * - ali keskin                                                             *
 *      _____                                                               *
 *      |   |               UniSQL, Inc.                                    *
 *      |   \_______        9390 Research Blvd.                             *
 *  ____|          |        Kaleido II, Suite 200                           *
 *  \  ___        /         Austin, TX 78759                                *
 *   \/   \   *  /          (512) 343-7297                                  *
 *         \_   /                                                           *
 *           \_/                                                            *
 *                                                                          *
 *   Internet: unisql!keskin@cs.utexas.edu                                  *
 *   UUCP: {uunet, cs.utexas.edu}!unisql!keskin                             *
 *                                                                          *
 ****************************************************************************
-- 
 * - ali keskin                                                             *
 *   Internet: unisql!keskin@cs.utexas.edu                                  *
 *   UUCP: {uunet, cs.utexas.edu}!unisql!keskin                             *
Newsgroups: comp.sys.sun.misc,comp.sys.sun.apps