Re: [linux-audio-dev] sysv semaphores instead of pipes

New Message Reply About this list Date view Thread view Subject view Author view Other groups

Subject: Re: [linux-audio-dev] sysv semaphores instead of pipes
From: Paul Davis (pbd_AT_Op.Net)
Date: Tue Jun 26 2001 - 16:13:40 EEST


>I just wanted to make sure I understood that. (I might learn something
>here). First, I guess SysV uses semget(2), semctl(2) and semop(2) to
>handle semaphores. Does that mean that the following code would not be
>correct:
>
>
> struct sembuf g_lock_sembuf[1];
> struct sembuf g_unlock_sembuf[1];
>
> g_lock_sembuf[0].sem_num = 0;
> g_lock_sembuf[0].sem_op = -1;
> g_lock_sembuf[0].sem_flg = 0;
> g_unlock_sembuf[0].sem_num = 0;
> g_unlock_sembuf[0].sem_op = 1;
> g_unlock_sembuf[0].sem_flg = 0;
>
> ....
>
> semop(g_sem_id, g_lock_sembuf, 1); /* lock semaphore */
> len = read(fd, buf, len); /* might block */
> semop(g_sem_id, g_unlock_sembuf, 1); /* unlock semaphore */
>
>
>What could go wrong?
>
>Do the same restrictions apply with the pthread semaphores
>(semaphores(3), semaphore.h) or the pthread mutexes?

No, you're on the wrong track here. The problem I was describing was
quite different. Suppose you have:

      int fd = open ("some non-disk file", O_RDONLY);
      struct sembuf foobuf;
      
      foobuf.sem_num = 0;
      foobuf.sem_op = -1;
      foobuf.sem_flg = 0;

and now you want to put the process to sleep until *either* the
semaphore is ready (however you've defined that) or the file is ready
to read.

Under POSIX you can't do this efficiently. There is no single call
that tells the kernel to put the process to sleep until either of
these conditions is satisfied. You can wait on the semaphore *OR* you
can wait on the file condition, but you can't wait on both. This is
sad.

Nothing will go wrong in your example.

pthread semaphores as supported by linuxthreads are completely
unrelated to SysV semaphores, and should never be discussed in the
same message :)

--p


New Message Reply About this list Date view Thread view Subject view Author view Other groups

This archive was generated by hypermail 2b28 : Tue Jun 26 2001 - 16:14:11 EEST