简体   繁体   English

从semop获取无效的参数

[英]Getting invalid argument from semop

I've set(semget + init to 1) a semaphore in process A. Forked A, and got B. 我在进程A中将信号量(semget + init设置为1)。分叉了A,得到了B。

Forked B and got C (code of processes B,C is at another .c file, so I am passing the semid as a global integer with extern int semid). 分叉B并得到C(进程B,C的代码在另一个.c文件中,因此我将semid作为带有extern int semid的全局整数传递)。

Into the process C code, I try to apply down(semid) and getting "invalid argument" error. 在进程C代码中,我尝试应用down(semid)并得到“无效参数”错误。
What I am doing in the code for down function is this: 我在down函数的代码中所做的是这样的:

struct sembuf sem_d;
sem_d.sem_num = 0;
sem_d.sem_op = -1;
sem_d.sem_flg = 0;
if ( semop(semid, &sem_d, 1) == -1 )
{
    perror("error with down function");
    return -1;
}

What am I doing wrong? 我究竟做错了什么?

I've also have reassured that the semid from when the semaphore is initialized is the same before semop. 我还向您保证,初始化信号量时的Semid在semop之前是相同的。
Also, in process A,BI am using wait(-1). 同样,在进程A,BI中,我正在使用wait(-1)。

I'm not sure if you are allowed to use semget() over forks - it's a different process space after all. 我不确定是否允许您在fork上使用semget() -毕竟这是一个不同的处理空间。

semget() is part of the old System V semaphores anyway. 无论如何, semget()是旧的System V信号量的一部分。

I would recommend to switch over to the POSIX semaphores - sem_open() , sem_wait() and friends and use named semaphores. 我建议您切换到POSIX信号量 sem_open()sem_wait()和朋友,并使用命名信号量。 Then open the same semaphore names in each process. 然后在每个进程中打开相同的信号灯名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM