我有2个独立的流程。我将2个值发送到第二个过程以计算其数量。这行得通,但是现在我需要将总和的值返回到第一个进程,并且由于锁定而不再起作用。第二个过程是等待某人进行读取,然后将其解锁。有什么想法可以将总和的值返回到第一个过程? 代码如下:
test.c:
int main(int argc, char *argv[])
{
int f1,f2;
int a = 4;
int b = 3;
int summ = 0;
unlink("mk.fifo");
if (mkfifo("mk.fifo", S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == -1 && errno != EEXIST)
error(0, errno, "mkfifo() error");
f1 = open("mk.fifo", O_WRONLY);
perror("Opening ");
write(f1, &a, sizeof(int));
write(f1, &b, sizeof(int));
perror("Writing ");
close(f1);
f2 = open("mk.fifoSumm", O_RDONLY);
perror("Opening ");
read(f2, &summ, sizeof(int));
perror("Reading ");
close(f2);
printf("Received string '%d'\n", summ);
return 0;
}
test2.c:
int main(int argc, char *argv[])
{
int f2,f1;
int a = 0;
int b = 0;
int summ = 0;
f2 = open("mk.fifo", O_RDONLY);
perror("Opening ");
read(f2, &a, sizeof(int));
read(f2, &b, sizeof(int));
perror("Reading ");
close(f2);
printf("Received string '%d'\n", a);
printf("Received string '%d'\n", b);
summ = a + b;
unlink("mk.fifoSumm");
if (mkfifo("mk.fifoSumm", S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == -1 && errno != EEXIST)
error(0, errno, "mkfifo() error");
f1 = open("mk.fifoSumm", O_WRONLY);
perror("Opening ");
write(f1, &summ, sizeof(int));
perror("Writing ");
close(f1);
return 0;
}