Using fork(), how can I make child process run always first?

后端 未结 6 671
星月不相逢
星月不相逢 2020-12-19 20:11

Child and parent process execution is parallel and which starts first depends on OS scheduling. But what can be done to start child always before the parent?

This is

6条回答
  •  醉梦人生
    2020-12-19 20:42

    Use a binary semaphore with initial value 0. After fork, parent should wait on the semaphore. After child starts, it can signal the semaphore (i.e., make it 1). Then, parent's wait would be over and it will progress.

提交回复
热议问题