挑战与挑战使用System V信号量时的奇怪体验

时间:2014-07-04 03:24:17

标签: ipc semaphore fifo atomicity

系统:

Linux ubuntu 3.2.0-57-generic-pae i686 GNU / Linux

问题:

我想利用信号量来实现流程原子性

下面的程序只是简单地将一些数据写入FIFO,而其他一些进程也尝试写入同一个FIFO,但下面的进程首先得到信号量,必须完成其写入并释放其他人的密钥

header.h

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
//#include<sys/ipc.h>
//#include<sys/sem.h>
#include<fcntl.h>
#define RES_FIFO 1234
#include<linux/sem.h>
typedef struct
{
pid_t   pid;
int     op1;
int     op2;
char    oper;
}MATH;
int result;
int ret;

过程:

#include"header.h"
int
main(void)
{
 MATH request;
 int fd,KK;
 if(access("TEMP",F_OK)== -1)
    mkfifo("TEMP",0666);
 fd=open("TEMP",O_WRONLY,0666);
 int i=0;
 union semun a;
 struct sembuf sops[2]={{0,-1,SEM_UNDO},{0,1,SEM_UNDO}};
 //1.Semaphore Creation
 KK=semget(RES_FIFO,1,IPC_CREAT);//semaphore set having one semaphore is created and kernel key will be returned
 if(KK==-1)
 {
 perror("Semget Error in P1\n");
 exit(1);
 }
 //2.Semaphore Control
 a.val=1;
 ret= semctl(KK,0,a);
 if(ret==-1)
 {
  perror("SemCtl Error in P1\n");
  exit(1);
 }
 //3.Semaphore Operations
 ret=semop(KK,&sops[0],1);
 if(ret==-1)
 {
  perror("Semop Error in decrementing at P1\n");
  exit(1);
 }


 for(i=0;i<3;i++)
 {
  request.op1=i;
  request.op2=i;
  request.oper='+';
  request.pid=getpid();
  write(fd,&request,sizeof(request));
 // sleep(1);
 }

 ret=semop(KK,&sops[1],2);
 if(ret==-1)
 {
  perror("Semop Error in decrementing at P1\n");
  exit(1);
 }
return 1;
}

但问题是在信号量操作semop之后,我被困住了,进程空闲无所事事并进入睡眠状态。那么我的代码有什么问题吗?

最新编辑1

有关建议我已更新代码行

ret= semctl(KK,0,SETVAL,a);//command updated as SETVAL

并且还取消注释了该行

sleep(1);

所以我的process1,process2,process3有相同的代码,在这里我忘了提及读取器代码从fifo读取并在控制台上打印

所以我在控制台上运行这样的脚本

./reader&
./p1&
./p2&
./p3&

reader.c

#include"header.h"
void
main(void)
{
 MATH request;
 int fd;
 int ret;
 int KK;
 if(access("TEMP",F_OK)== -1)
         mkfifo("TEMP",0666);
 fd=open("TEMP",O_RDONLY,0666);
sleep(3);
printf("Server Read Started\n");
 while(1)
 {
 if( (ret=read(fd,&request,sizeof(request))) == -1)
 {
  perror("Read in Server Failed\n");
  exit(1);
 }
 else if(ret==0)
  continue;
 printf("Read from PID : %d about %d %c %d \n",request.pid,request.op1,request.oper,request.op2);

 }
printf("Read Complete\n");
}

根据我的目标,我希望输出像这样

Server Read Started
Read from PID : 3673 about 0 + 0 
Read from PID : 3675 about 1 + 1 
Read from PID : 3674 about 2 + 2 
Read from PID : 3673 about 0 - 0 
Read from PID : 3675 about 1 - 1 
Read from PID : 3674 about 2 - 2 
Read from PID : 3673 about 0 * 0 
Read from PID : 3675 about 1 * 1 
Read from PID : 3674 about 2 * 2 

但实际观察到的输出是这样的

Server Read Started
Read from PID : 3673 about 0 + 0 
Read from PID : 3675 about 0 * 0 
Read from PID : 3674 about 0 - 0 
Read from PID : 3673 about 1 + 1 
Read from PID : 3675 about 1 * 1 
Read from PID : 3674 about 1 - 1 
Read from PID : 3673 about 2 + 2 
Read from PID : 3675 about 2 * 2 
Read from PID : 3674 about 2 - 2 

我认为我成功地清楚地解释了我的问题。任何进一步的编辑表示赞赏

2 个答案:

答案 0 :(得分:2)

Bug位于代码

//2.Semaphore Control
 a.val=1;
 ret= semctl(KK,0,a);
 if(ret==-1)
 {
  perror("SemCtl Error in P1\n");
  exit(1);
 }

我把它放在它们彼此复制的所有三个进程中,当包含相同代码的process2开始执行由process1递减的信号量时,又被重新初始化,这打破了3个进程的原子性

答案 1 :(得分:1)

我需要对要运行的代码进行3次更正:

1)您可能不需要这个,因为您可能拥有读取队列的进程。但是因为我只有你的&#34; process.c&#34;我需要调整一下:

FD =开放(&#34; TEMP&#34;,O_WRONLY,0666); - &GT; fd = open(&#34; TEMP&#34;,O_WRONLY | O_NONBLOCK ,0666);

2)这条线似乎有错误,需要调整。要将信号量设置为初始值,您需要使用SETVAL:

进行设置

ret = semctl(KK,0,a); - &GT; ret = semctl(KK,0, SETVAL, a);

3)这条线似乎有误。最后一个参数是要抓取的操作数。它必须是1:

RET =执行semop(KK,&安培;标准操作程序[1],2); - &GT; RET =执行semop(KK,&安培;标准操作程序[1],的 1 );