How to create multiple ordered processes using fork() and execvp() in C?
I am trying to create multiple processes using fork() and execvp() calls, but so far I've been unsuccessful. Here is what I am trying to do:
Processes A开发者_运维技巧, B, C should run at the same time. When they are finished, process D should run. When it is done, processes E and F should run.
I can call execvp() successfully by passing the program name and its arguments, but I don't know how many times I should call fork() and where. I also know how to call wait(), but again I am not sure where it should be called.
How would I do this?
I would approach this in the following manner:
- Main Program starts -> fork() Process A, wait() for completion
- Process A starts -> fork() Process B, run code, wait() for Process B completion
- Process B starts -> fork() Process C, run code, wait() for Process C completion
- Process C starts -> runs code, returns to Process B
- Process B returns to Process A
- Process A returns to Main Program
- Main Program continues to run, calls Process D routines (without forking)
- Main Program -> fork() Process E, wait() for completion
- Process E starts -> fork() Process F, run code, wait() for Process F completion
- Process F starts -> runs code, returns to Process E
- Process E returns to Main Program
精彩评论