
Chris T. answered 02/11/22
Senior Engineer @ Google | Former TA in operating systems
TLDR: This answer assumes you're writing on a POSIX system. You may want to look into the following:
- fork() to create the new child processes.
- Semaphores for communication. Semaphores will probably fit this use case very well. You can also use shared mutexes, condition variables, or even pipes if you'd like.
- open() and either getline() if on Linux or fgets() on general POSIX for file reading.
The class of problem that you're solving is producer-consumer.
Remember that once you fork(), each process gets a new private address space. They will no longer share any data, so you must make sure the synchronization primitives are in shared memory. Different operating systems do this differently, but POSIX semaphores works if you're on such a system.
A skeleton of your code will probably look something like this: