in my program, I use in main function fork to create 2 processes. Child process do something and parent process is forked again and his child calls another function. Both functions writes to 1 file and all works fine.
What I need is to write something to the end of file, after both functions and all processes (both functions create processes) finish.
I tried to write fprintf command everywhere in main and it allways writes somewhere in the middle of file, so I think that the main propably runs parallelly with the 2 functions.
I tried to use semaphore
s = sem_open(s1, o_CREATE, 0666, 0);
in this way: In the end of each function I wrote sem_post(s)
and in main I put sem_wait(s); sem_wait(s);
and after this i wrote fprintf command, but it also didn't work.
Is there some way how to solve this?
Thanks

- 303
- 1
- 3
- 12
-
using `fork` is not *parallel processing* - that is incorrect terminology. – t0mm13b Apr 25 '17 at 19:10
1 Answers
I think you're looking for the wait
function. See this stack overflow question: wait(NULL)
will wait for all children to finish wait for a child process to finish (thanks Jonathan Leffler). Call wait
in a loop to wait for all children processes to finish. Just use that function right before you write to the file in your parent process.
You can also read about the waitpid
function if you want to wait for a specific process instead of for all the processes.
Edit:
Alternatively, you can actually use semaphores across processes, but it takes a little more work. See this stack overflow answer. The basic idea is to use the function sem_open
with the O_CREAT
constant. sem_open
has 2 function signatures:
sem_t *sem_open(const char *name, int oflag);
sem_t *sem_open(const char *name, int oflag, mode_t mode, unsigned int value);
From the sem_open man page:
If O_CREAT is specified in oflag, then two additional arguments must be supplied. The mode argument specifies the permissions to be placed on the new semaphore, as for open(2). (Symbolic definitions for the permissions bits can be obtained by including <sys/stat.h>.) The permissions settings are masked against the process umask. Both read and write permission should be granted to each class of user that will access the semaphore. The value argument specifies the initial value for the new semaphore. If O_CREAT is specified, and a semaphore with the given name already exists, then mode and value are ignored.
In your parent process, call sem_open
with the mode and value parameters, giving it the permissions you need. In the child process(es), call sem_open("YOUR_SEMAPHORE_NAME", 0)
to open that semaphore for use.
-
Might help to give a brief summary of what each of answers convey rather than dumping links. – t0mm13b Apr 25 '17 at 19:15
-
I am not sure, but I think that I have it exactly as you wrote it. I have it like: main: sem_t *s; s = sem_open(s1, O_CREAT, 0666, 0); sem_close(s); s = sem_open(s1, O_RDWR); function 1 and two: sem_t *s; s = sem_open(s1, O_RDWR); "s1" Is a macro. – Erik Apr 25 '17 at 20:05
-
1Note that [`wait()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/wait.html) waits for one child to die. You have to use it in a loop to wait for all children to die. `int corpse; int status; while ((corpse = wait(&status)) != -1) printf("PID %d exited with status 0x%.4X\n", corpse, status);`. There's still room for angst over signal handling and the like; you can make that more sophisticated if you like. – Jonathan Leffler Apr 25 '17 at 23:42