SYSV IPC Semaphore Problem on Linux

SYSV IPC Semaphore Problem on Linux

Post by Lutz Wohlr » Fri, 06 Sep 1996 04:00:00



Hi,

I got a problem trying to organize simple mutual exclusion for an arbitrary
number of processes using a semaphore, on HPUX and SunOS/Solaris it works fine.

The first process opens a file gets a semaphore using semget, initializes it
semctl operation SETVAL to 1, and forks 10 children (as a test), which write
into that file.

Before writing, the children perform operation -1 on the semaphore without
UNDO or NOWAIT. After writing, they perform op 1 (without UNDO again) and
exit.

To my knowledge, this should ensure that only one process at a time writes to
that file.

Under Linux, I get the contents of the test file (fopened with mode a+) messed
up. Any idea why it works under HPUX, SunOS, Solaris and not under Linux, too?


Thanks a lot in advance,
                          Lutz.
--
-------------------------------------------

 
 
 

SYSV IPC Semaphore Problem on Linux

Post by James Youngm » Sat, 07 Sep 1996 04:00:00




>Hi,

>I got a problem trying to organize simple mutual exclusion for an arbitrary
>number of processes using a semaphore, on HPUX and SunOS/Solaris it works
fine.

>The first process opens a file gets a semaphore using semget, initializes it
>semctl operation SETVAL to 1, and forks 10 children (as a test), which write
>into that file.

>Before writing, the children perform operation -1 on the semaphore without
>UNDO or NOWAIT. After writing, they perform op 1 (without UNDO again) and
>exit.

>To my knowledge, this should ensure that only one process at a time writes to
>that file.

>Under Linux, I get the contents of the test file (fopened with mode a+) messed
>up. Any idea why it works under HPUX, SunOS, Solaris and not under Linux, too?


>Thanks a lot in advance,

Are you flushing the file's stdio buffer before you release the semaphore?

(BTW, why not just use normal file locking?)

--
James Youngman       VG Gas Analysis Systems |The trouble with the rat-race
 Before sending advertising material, read   |is, even if you win, you're
http://www.law.cornell.edu/uscode/47/227.html|still a rat.

 
 
 

SYSV IPC Semaphore Problem on Linux

Post by Lutz Wohlr » Mon, 09 Sep 1996 04:00:00


Quote:>>I got a problem trying to organize simple mutual exclusion for an arbitrary
>>number of processes using a semaphore, on HPUX and SunOS/Solaris it works
>fine.
>>To my knowledge, this should ensure that only one process at a time writes to
>>that file.
>Are you flushing the file's stdio buffer before you release the semaphore?
>That was it, the other System don't buffer, it seems.

A fflush(file) did the job.
Quote:

>(BTW, why not just use normal file locking?)

Aren't they only advisory?
                               Lutz.