你可以从一个
semop()
通过接收信号(如由
alarm()
.
未测试示例:
#include <signal.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
volatile int alarm_triggered = 0;
void alarm_handler(int sig)
{
alarm_triggered = 1;
}
int main(int argc, char **argv)
{
int rc;
/* set up signal handler */
signal(SIGALRM, alarm_handler);
/* ... */
alarm(30); /* 30 second timeout */
rc = semop(...);
if (rc == -1 && errno == EINTR)
{
if (alarm_triggered)
{
/* timed out! */
}
}
alarm(0); /* disable alarm */
/* ... */
}