[英]Semaphore counter: program hangs
I want create a semaphore counter, with this code: 我想用以下代码创建一个信号量计数器:
union semun arg_assistant;
int max_ass = atoi(argv[1]);
printf("Num massimo di assistant %d\n", max_ass);
fflush(stdout);
if ((sem_a = semget(IPC_PRIVATE, 1, 0600)) == -1) {
perror("semget");
exit(EXIT_FAILURE);
}
arg_assistant.val = max_ass;
if (semctl(sem_a, 0, SETALL, arg_assistant) == -1) {
perror("semctl");
exit(EXIT_FAILURE);
}
When I executed my program, I have no errors but it hangs and it don't create this sem. 当我执行程序时,我没有任何错误,但是它挂起了,并且没有创建此sem。 Any suggestion about what could be the problem?
关于可能是什么问题的任何建议? Have I make some mistake with falgs?
我会误以为是错误的吗? Thaks
解冻
From documentation 从文档
SETALL
设置
Set semval for all semaphores of the set using arg.array,
使用arg.array为集合的所有信号量设置semval,
For SETALL
you need array of values 对于
SETALL
您需要一个值数组
unsigned short int sem_array[1] ;
sem_array[0] = max_ass;
arg_assistant.array = sem_array;
if (semctl(sem_a, 0, SETALL, arg_assistant) == -1) {
perror("semctl");
exit(EXIT_FAILURE);
}
sem_array[1]
becaues you create only one semaphore. sem_array[1]
您只创建了一个信号量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.