I have a file
names.txt Pop,Jack and a file
surnames.txt Ioan,Gry. I need to obtain the following output,
person.txt, Pop Ioan, Jack Gry. I must create two programs:
- First read from file names.txt and write in person.txt.
- Second read from surnames.txt and write in person.txt.
I need to use pipes for synchronization. How I can do it?
Best How To :
Each program will open a named pipe for reading and one for writing, person.txt for append (or just write to stdout), and the appropriate input file (or read from stdin). One program, probably designated by a command line arg, will start by immediately reading one line and writing it to the output, and then write a single byte into one pipe and then block on a read from the other pipe. The other program waits for the byte on the first pipe, then writes its text and a newline before writing a byte into the other pipe. So you're just passing a token (the byte written to the pipes) to synchronize the writes. It might be nice to simply pass the names of the pipes to the programs as command line args, and then kick off the whole process by writing a single byte into one of the pipes, but the programs are not identical since one has to write a newline. Your best bet is to write a single program but invoke it with different command line args. (I don't know if that qualifies as "two programs".)