1

I have two progress, p1 and p2, and a named pipe var for ipc between 2 progress.
I want to lock var's rw for p2 when p1 writes, and unlock var when p1 finished write.

ps:

I using select for nonblocking, so p2 will get readable when p1 put anything to var.Can I let var become readable when p1 finish write?

KaMeTang
  • 43
  • 5

1 Answers1

1

You could use signals (e.g. SIGUSR1). The writer makes it pipe non-blocking (so it won't block when the pipe becomes full), writes until it can't write anymore, then send the signal to the other process. The reading process reads all (from its non-blocking pipe), then sends a signal to the writer who then continues to write.

However, this is really not needed. The writer can just write, and the reader just read. If the pipe becomes full the writer will block until it can write more. And the same for the reader, it will block if there's nothing to read. Then when the writer has written all data, it will simply close its end of the pipe, which the reader will detect with a read call that returns zero bytes read.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • File lock give me some sense. I will test fcntl later. – KaMeTang Dec 04 '13 at 09:54
  • I think I need to use two pipes, one for signal, the other for data. I just try to lock, but I cant lock. Maybe cant lock `mkfifo`'s file? – KaMeTang Dec 04 '13 at 12:32
  • @KaMeTang I suspected it was not possible to lock the FIFO file. And yes, one pipe for data in one direction, and one pipe for commands (or signals) in the other. – Some programmer dude Dec 04 '13 at 12:39