开发者

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:

  1. Main Program starts -> fork() Process A, wait() for completion
  2. Process A starts -> fork() Process B, run code, wait() for Process B completion
  3. Process B starts -> fork() Process C, run code, wait() for Process C completion
  4. Process C starts -> runs code, returns to Process B
  5. Process B returns to Process A
  6. Process A returns to Main Program
  7. Main Program continues to run, calls Process D routines (without forking)
  8. Main Program -> fork() Process E, wait() for completion
  9. Process E starts -> fork() Process F, run code, wait() for Process F completion
  10. Process F starts -> runs code, returns to Process E
  11. Process E returns to Main Program
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜