I have a programme using sem_wait
. The Posix specification says:
The
sem_wait()
function is interruptible by the delivery of a signal.
Additionally, in the section about errors it says:
[EINTR] - A signal interrupted this function.
However, in my programme, sending a signal does not unblock the call (and return -1
as indicated in the spec).
A minimal example can be found below. This programme hangs and sem_wait
never unblocks after the signal is sent.
#include <semaphore.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
sem_t sem;
void sighandler(int sig) {
printf("Inside sighandler\n");
}
void *thread_listen(void *arg) {
signal(SIGUSR1, &sighandler);
printf("sem_wait = %d\n", sem_wait(&sem));
return NULL;
}
int main(void) {
pthread_t thread;
sem_init(&sem, 0, 0);
pthread_create(&thread, NULL, &thread_listen, NULL);
sleep(1);
raise(SIGUSR1);
pthread_join(thread, NULL);
return 0;
}
The programme outputs Inside sighandler
then hangs.
There is another question here about this, but it doesn't really provide any clarity.
Am I misunderstanding what the spec says? FYI my computer uses Ubuntu GLIBC 2.31-0ubuntu9.