Reputation: 87
I have 2 independent processes. I send 2 values to the second process to calculate their amount. This works, but now I need to return the value of the sum to the first process and this does not work anymore because of the lock. The second process is waiting for someone to do READ and it unlocks him. What are some ideas to return the value of the sum to the first process? The code is below:
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;
}
Upvotes: 0
Views: 57