0

I have a program which has 2 children (running 2 processes by execl), and one fifo. I can't use printf, and I want both children to write and read from fifo. problem is, I want only first child to make sure that everything he writes to my FIFO will be printed out to the screen. "fifoCommunication" is the name of the fifo created by father. here is the code inside the first child's process only:

int main()    {
int fd_write = open("fifoCommunication",O_WRONLY);
dup(fd_write,0);
write(fd_write,"to be printed to screen!" ,18);}

I know it's not the right syntax, but I don't know how to make sure the message is printed to the screen properly, and also preventing the other child to print messages to the screen, only to the FIFO.

Zohar Argov
  • 143
  • 1
  • 1
  • 11

1 Answers1

1

I am afraid your requirements conflict with each other.

I want only first child to make sure that everything he writes to my FIFO will be printed out to the screen.

Therefore FIFO must print to the console whatever it gets. FIFO doesn't distinguish between processes that have printed to it. It doesn't know that it is the first or the second child that called write at this time 1.

preventing the other child to print messages to the screen, only to the FIFO

Therefore this contradicts above, because printing "only to the fifo" must print to the screen too if former requirement is to be met. You can achieve what you want by printing separately to fifo and to the stdout.

1 (unless you change kernel code to for example check the first byte of message to be printed, so you would then prefix each data with '1' or '2' or whatever you choose and take appropriate action in kernel based on that - but then what is going to happen to all other uses of fifo on your machine is nothing good most likely, so don't do it)

4pie0
  • 29,204
  • 9
  • 82
  • 118