gpt4 book ai didi

c - semop : Bad file descriptor

转载 作者:行者123 更新时间:2023-11-30 17:18:21 26 4
gpt4 key购买 nike

#define MAX 2

int main(){

int mutex = semget(ftok("/usr",'P'),1,IPC_CREAT|0666);
int wrt = semget(ftok("/usr",'Q'),1,IPC_CREAT|0666);

if(mutex <= 0) perror("mutex");
if(wrt <= 0) perror("wrt");

if(semctl(mutex,0,SETVAL,1) <= 0) perror("mutex semctl");
if(semctl(wrt,0,SETVAL,1) <= 0) perror("wrt semctl");

struct sembuf sops;
int pid1,pid2,readcount = 0,var=0;
FILE* fp = fopen("buffer.txt","w");
fprintf(fp,"%d",var);
fclose(fp);
printf("File created. VAR is %d\n",var);
pid1 = fork();

if(!pid1){

while(1){
sleep(1);
sops.sem_num = 0; sops.sem_op = -1; sops.sem_flg = 0;
semop(mutex,&sops,1);

// if(readcount == 0)
if(readcount == 0){
// wait(wrt)
sops.sem_num = 0; sops.sem_op = -1; sops.sem_flg = 0;
if(semop(wrt,&sops,1) <= 0) perror("wrt in reader 1");
}
// readcount++
readcount++;
// signal(mutex)
sops.sem_num = 0; sops.sem_op = 1; sops.sem_flg = 0;
semop(mutex,&sops,1);

// read()
sleep(1.5);
printf("First reader enters \n");
fp = fopen("buffer.txt","r");
fscanf(fp,"%d",&var);
printf("Reader (%d) reads the value %d\n",getpid(),var);
fclose(fp);
printf("First reader leaves\n");
sleep(1.5);


// wait(mutex)
sops.sem_num = 0; sops.sem_op = -1; sops.sem_flg = 0;
semop(mutex,&sops,1);
// readcount--;
readcount--;
// if(readcount == 0)
if(readcount == 0){
// signal(wrt)
sops.sem_num = 0; sops.sem_op = 1; sops.sem_flg = 0;
semop(wrt,&sops,1);
}
// signal(mutex)
sops.sem_num = 0; sops.sem_op = 1; sops.sem_flg = 0;
semop(mutex,&sops,1);
}


}
else{
pid2 = fork();
if(!pid2){

while(1){
sleep(1);
// wait(mutex)
sops.sem_num = 0; sops.sem_op = -1; sops.sem_flg = 0;
semop(mutex,&sops,1);
// if(readcount == 0)
if(readcount == 0){
// wait(wrt)
sops.sem_num = 0; sops.sem_op = -1; sops.sem_flg = 0;
if(semop(wrt,&sops,1) <= 0) perror("wrt in reader 1");
}
// readcount++
readcount++;
// signal(mutex)
sops.sem_num = 0; sops.sem_op = 1; sops.sem_flg = 0;
semop(mutex,&sops,1);


// read()
sleep(1);
printf("Second reader enters\n");
sleep(1);
fp = fopen("buffer.txt","r");
fscanf(fp,"%d",&var);
printf("Reader (%d) reads the value %d\n",getpid(),var);
sleep(1);
fclose(fp);
printf("Second reader leaves\n");
sleep(1);


// wait(mutex)
sops.sem_num = 0; sops.sem_op = -1; sops.sem_flg = 0;
semop(mutex,&sops,1);
// readcount--;
readcount--;
// if(readcount == 0)
if(readcount == 0){
// signal(wrt)
sops.sem_num = 0; sops.sem_op = 1; sops.sem_flg = 0;
semop(wrt,&sops,1);
}
// signal(mutex)
sops.sem_num = 0; sops.sem_op = 1; sops.sem_flg = 0;
semop(mutex,&sops,1);
}
}
else{
//struct sembuf sop;

while(1){

// wait(wrt)
sops.sem_num = 0; sops.sem_op = -1; sops.sem_flg = 0;
semop(wrt,&sops,1);

// write
sleep(1);
//printf("%d Writer enters\n",time(0)%100);
fp = fopen("buffer.txt","w");
fscanf(fp,"%d",&var);
var++;
sleep(1);
printf("Writer (%d) writes the value %d\n",getpid(),var);
fprintf(fp,"%d",&var);
fclose(fp);
sleep(1);
printf("Writer leaves wrt %d\n",semctl(wrt,0,GETVAL,0));

// signal(wrt)
sops.sem_num = 0; sops.sem_op = 1; sops.sem_flg = 0;
printf("wrt just before semop %d and op is %d\n",semctl(wrt,0,GETVAL,0),sops.sem_op);
if(semop(wrt,&sops,1) <= 0) perror("wrt signal");
printf("wrt %d\n",semctl(wrt,0,GETVAL,0));
}
}

}
}

以下代码与读者-作者问题略有不同。在这个程序中,我尝试创建两个子进程(两个读者)。有一个父进程(编写器)。读取器只是从名为 buffer.txt 的文件中读取值,而写入器则在该文件中写入值。

但是在编写器代码中,使用 semop()//signal(wrt) 代码收到错误错误文件描述符。因此,wrt 的值不会增加。请帮忙。

最佳答案

经过10个小时的艰苦努力,我想出了一个完美运行的解决方案,没有任何错误死锁。

更改 1:在writer的代码中,首先以读模式打开文件,读取值,然后以写模式打开文件,然后写入递​​增的值。

这还不够,还可能出现死锁问题。

更改 2:我们还需要为 readcount 创建一个信号量,因为它们必须由两个子进程使用。

关于c - semop : Bad file descriptor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29197222/

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