gpt4 book ai didi

c - 以 super 用户身份运行时信号量工作,否则无法工作

转载 作者:行者123 更新时间:2023-11-30 19:14:08 24 4
gpt4 key购买 nike

我有一个简单的代码,我在其中初始化了一个系统 V 信号量,以便我的 2 个进程打印以下代码 10 次:“abcd”。第一个进程打印“ab”字符串,而另一个进程打印“cd\n”字符串。因此,当我以 super 用户身份执行它时,一切正常,但是当我不使用 sudo 命令来执行它时,我会得到意外的输出。

这是代码:

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/sem.h>
#include<sys/ipc.h>
#include<sys/types.h>
#include<string.h>
#include<errno.h>
#include<sys/wait.h>
#include "display.h"

int main(int argc,char *argv[]){

key_t key1 = 12345;
int semid;
unsigned short semval;
int i;
int childpid;

struct sembuf wit,signal;

wit.sem_num = 0;
wit.sem_op = -1;
wit.sem_flg = SEM_UNDO;

signal.sem_num = 0;
signal.sem_op = 1;
signal.sem_flg = SEM_UNDO;

semid = semget(key1,1,IPC_CREAT);
// printf("Allocating the semaphore: %s\n",strerror(errno));

semval = 1;
semctl(semid,0,SETVAL,semval);
// printf("Setting semaphore value to %d: %s\n",semval,strerror(errno));

semval = semctl(semid,0,GETVAL);
// printf("Initialized Semaphore value to %d: %s\n",semval,strerror(errno));
sleep(1);
childpid = fork();

if(childpid==0){ /*process 2 */
for(i=0;i<10;i++){
semop(semid,&wit,1);
semctl(semid,0,GETVAL,&semval);
display("cd\n");
semop(semid,&signal,1);
}
semctl(semid,0,GETVAL,&semval);
return 0;
}

for(i=0;i<10;i++){
semop(semid,&wit,1); /* process 1*/
semctl(semid,0,GETVAL,&semval);
display("ab");
semop(semid,&signal,1);
}
semctl(semid,0,GETVAL,&semval);
sleep(1);
semctl(semid,0,IPC_RMID);
// printf("\nSemaphore removed from the System = %s\n",strerror(errno));
return 0;
}

因此,当我使用“sudo ./mycodehere”命令执行代码时,一切都会顺利并且输出正确:

abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd

但是如果我正常执行,“./mycodehere”结果不一样:

acbda
bcadb
acbda
bcadb
acbda
bcadb
acbd
cd
cd
cd

有人可以解释一下为什么会发生这种情况吗?如果可能的话,我应该在哪里解决它?提前非常感谢!差点忘了。display.c 文件:

#include <stdio.h>
#include <unistd.h>
#include "display.h"

void display(char *str)
{
char *p;
for (p=str; *p; p++)
{
write(1, p, 1);
usleep(100);
}
}

最佳答案

这一行

semid = semget(key1,1,IPC_CREAT);

无法正确设置信号量权限。权限全部为零。

根据 semget specification :

The low-order 9 bits of sem_perm.mode shall be set equal to the low-order 9 bits of semflg.

根据Linux man page :

Upon creation, the least significant 9 bits of the argument semflg define the permissions (for owner, group and others) for the semaphore set. These bits have the same format, and the same meaning, as the mode argument of open(2) (though the execute permissions are not meaningful for semaphores, and write permissions mean permission to alter semaphore values).

你的代码需要是这样的:

semid = semget(key1,1,IPC_CREAT | 0660 );

但是现在您的信号量可能已经存在,但权限错误。

关于c - 以 super 用户身份运行时信号量工作,否则无法工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34619450/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com