Article View: comp.unix.programmer
Article #3702problem with gcc and semctl(2)
From: Jorge Gautier
Date: Tue, 15 Oct 1991 08:55
Date: Tue, 15 Oct 1991 08:55
169 lines
6387 bytes
6387 bytes
I'm running SunOS 4.1.1 and gcc 1.40. I'm using the semaphore code
from Stevens's book, semaph.c. When I call sem_create I'm get a
segfault in line 143,
if (semctl(id, 0, SETVAL, semctl_arg) < 0)
when semctl_arg.val is 0, and a buserror when it is 1. The code works
when compiled with cc. Any hints for fixing this would be much
appreciated. Here is the code from semaph.c:
/*
* Provide an simpler and easier to understand interface to the System V
* semaphore system calls. There are 7 routines available to the user:
*
* id = sem_create(key, initval); # create with initial value or open
* id = sem_open(key); # open (must already exist)
* sem_wait(id); # wait = P = down by 1
* sem_signal(id); # signal = V = up by 1
* sem_op(id, amount); # wait if (amount < 0)
* # signal if (amount > 0)
* sem_close(id); # close
* sem_rm(id); # remove (delete)
*
* We create and use a 3-member set for the requested semaphore.
* The first member, [0], is the actual semaphore value, and the second
* member, [1], is a counter used to know when all processes have finished
* with the semaphore. The counter is initialized to a large number,
* decremented on every create or open and incremented on every close.
* This way we can use the "adjust" feature provided by System V so that
* any process that exit's without calling sem_close() is accounted
* for. It doesn't help us if the last process does this (as we have
* no way of getting control to remove the semaphore) but it will
* work if any process other than the last does an exit (intentional
* or unintentional).
* The third member, [2], of the semaphore set is used as a lock variable
* to avoid any race conditions in the sem_create() and sem_close()
* functions.
*/
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <errno.h>
extern int errno;
#define BIGCOUNT 10000 /* initial value of process counter */
/*
* Define the semaphore operation arrays for the semop() calls.
*/
static struct sembuf op_lock[2] = {
2, 0, 0, /* wait for [2] (lock) to equal 0 */
2, 1, SEM_UNDO /* then increment [2] to 1 - this locks it */
/* UNDO to release the lock if processes exits
before explicitly unlocking */
};
static struct sembuf op_endcreate[2] = {
1, -1, SEM_UNDO,/* decrement [1] (proc counter) with undo on exit */
/* UNDO to adjust proc counter if process exits
before explicitly calling sem_close() */
2, -1, SEM_UNDO /* then decrement [2] (lock) back to 0 */
};
static struct sembuf op_open[1] = {
1, -1, SEM_UNDO /* decrement [1] (proc counter) with undo on exit */
};
static struct sembuf op_close[3] = {
2, 0, 0, /* wait for [2] (lock) to equal 0 */
2, 1, SEM_UNDO, /* then increment [2] to 1 - this locks it */
1, 1, SEM_UNDO /* then increment [1] (proc counter) */
};
static struct sembuf op_unlock[1] = {
2, -1, SEM_UNDO /* decrement [2] (lock) back to 0 */
};
static struct sembuf op_op[1] = {
0, 99, SEM_UNDO /* decrement or increment [0] with undo on exit */
/* the 99 is set to the actual amount to add
or subtract (positive or negative) */
};
/****************************************************************************
* Create a semaphore with a specified initial value.
* If the semaphore already exists, we don't initialize it (of course).
* We return the semaphore ID if all OK, else -1.
*/
int
sem_create(key, initval)
key_t key;
int initval; /* used if we create the semaphore */
{
register int id, semval;
union semun {
int val;
struct semid_ds *buf;
ushort *array;
} semctl_arg;
if (key == IPC_PRIVATE)
return(-1); /* not intended for private semaphores */
else if (key == (key_t) -1)
return(-1); /* probably an ftok() error by caller */
again:
if ( (id = semget(key, 3, 0666 | IPC_CREAT)) < 0)
return(-1); /* permission problem or tables full */
/*
* When the semaphore is created, we know that the value of all
* 3 members is 0.
* Get a lock on the semaphore by waiting for [2] to equal 0,
* then increment it.
*
* There is a race condition here. There is a possibility that
* between the semget() above and the semop() below, another
* process can call our sem_close() function which can remove
* the semaphore if that process is the last one using it.
* Therefore, we handle the error condition of an invalid
* semaphore ID specially below, and if it does happen, we just
* go back and create it again.
*/
if (semop(id, &op_lock[0], 2) < 0) {
if (errno == EINVAL)
goto again;
err_sys("can't lock");
}
/*
* Get the value of the process counter. If it equals 0,
* then no one has initialized the semaphore yet.
*/
if ( (semval = semctl(id, 1, GETVAL, 0)) < 0)
err_sys("can't GETVAL");
if (semval == 0) {
/*
* We could initialize by doing a SETALL, but that
* would clear the adjust value that we set when we
* locked the semaphore above. Instead, we'll do 2
* system calls to initialize [0] and [1].
*/
semctl_arg.val = initval;
if (semctl(id, 0, SETVAL, semctl_arg) < 0) /* bombs right here */
err_sys("can SETVAL[0]");
semctl_arg.val = BIGCOUNT;
if (semctl(id, 1, SETVAL, semctl_arg) < 0)
err_sys("can SETVAL[1]");
}
/*
* Decrement the process counter and then release the lock.
*/
if (semop(id, &op_endcreate[0], 2) < 0)
err_sys("can't end create");
return(id);
}
Technical Details
Message-ID:
<JGAUTIER.91Oct15095509@vangogh.ads.com>Path:
arc1-ca.pugleaf.net!gmdzi!unido!mcsun!uunet!decwrl!ads.com!saturn!jgautierShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 11318c,22407d957bfa9556,start X-Google-Attributes: gid11318c,public X-Google-Thread: f8c9b,22407d957bfa9556,start X-Google-Attributes: gidf8c9b,public X-Google-Thread: fc89c,22407d957bfa9556,start X-Google-Attributes: gidfc89c,public X-Google-ArrivalTime: 1991-10-15 16:31:22 PST Path: arc1-ca.pugleaf.net!gmdzi!unido!mcsun!uunet!decwrl!ads.com!saturn!jgautier From: jgautier@vangogh.ads.com (Jorge Gautier) Newsgroups: comp.unix.programmer,comp.unix.questions,gnu.gcc.help Subject: problem with gcc and semctl(2) Message-ID: <JGAUTIER.91Oct15095509@vangogh.ads.com> Date: 15 Oct 91 08:55:09 GMT Sender: usenet@ads.com (USENET News) Organization: Advanced Decision Systems, Mountain View, CA 94043, +1 (415) 960-7300 Lines: 169 Nntp-Posting-Host: vangogh.ads.com