关于sem_trywait的一个问题
代码如下:
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <semaphore.h>
int main(int argc, char *argv[])
{
sem_t *sem = NULL;
sem = sem_open(argv[1], O_RDWR);
if (sem == SEM_FAILED)
{
fprintf(stderr, "%s\n", "Can not open a semaphore");
exit(EXIT_FAILURE);
}
int sem_value = 0;
int ret = sem_getvalue(sem, &sem_value);
printf("%d\n", sem_value);
if (ret == -1)
{
perror("Can not get the value of a semaphore");
}
/* Why don't retuen a error number of EAGAIN(11) */
ret = sem_trywait(sem);
printf("%d\n", ret);
if (ret == EAGAIN)
{
fprintf(stdout, "%s\n", "can not get resource");
}
return 0;
}