我有一个非常简单的程序来测试命名信号量:
#include <stdio.h>
#include <stdlib.h>
#include <semaphore.h>
#include <sys/ipc.h>
#include <unistd.h>
#include <sys/shm.h>
#include <fcntl.h>
sem_t thing;
void processOne() {
sleep(1);
int l;
sem_getvalue(&thing, &l);
printf("processOneThing: %d\n", l);
}
void processTwo(){
sem_wait(&thing);
printf("done");
}
int main(int argc, char **argv) {
int pidOne, pidTwo;
thing = *sem_open("/testest", O_CREAT|O_EXCL,0777,1);
pidTwo=fork();
if(pidTwo==0){
processTwo();
}
else{
pidOne=fork()
if(pidOne==0){
processOne();
}
else{
}
wait(&pidTwo);
wait(&pidOne);
}
sem_unlink("/testest");
sem_close(&thing);
}
输出为:
doneprocessOneThing: 1
这意味着第二个进程减少了信号量,但是在第一个子进程中它的值仍然是1。
我不知道我在做什么错,我查看了已命名的信号量文档,却找不到很多可以帮助我解决此问题的信息。
我正在使用编译:
gcc test.c -pthread
任何和所有帮助将不胜感激。
首先,代码缺少; 在此行的末尾:
else{
pidOne=fork() <------ here
if(pidOne==0){
processOne();
}
而且它无法为wait()原型#include <sys / wait.h>。但是更正这些问题,我得到的输出与您相同。那到底是怎么回事?问题在于您如何创建信号量:
sem_t thing;
...
thing = *sem_open("/testest", O_CREAT|O_EXCL,0777,1);
sem_open的返回值是创建的信号量的地址。但是,您正在取消引用该地址,并将内容的副本放入您的可变内容中。这意味着实际上不是您创建的信号量,而仅仅是保存状态信息的结构的副本。结果是与代码中的事物进行交互实际上并没有与您的意图进行交互。(实际上,如果您检查sem_wait()和sem_getvalue()的返回值,您可能会得到一些表明不正确的提示。)
相反,如果将事物作为指向信号量的指针并与其进行交互,则会得到以下输出:
done
processOneThing: 0
我认为这是您期望看到的。您可以在此处尝试更正的代码:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句