fork() 是一个系统调用函数,可以从父主进程生成子进程。使用某些条件,我们可以根据需要生成任意多个子进程。
null
我们给了n,我们必须创造 N -来自同一父进程(主进程)的子进程。
例如:
Input :3 Output :[son] pid 25332 from [parent] pid 25329 [son] pid 25331 from [parent] pid 25329 [son] pid 25330 from [parent] pid 25329 here 25332, 25331,25330 are child processes from same parent process with process id 25329 Input :5 Output :[son] pid 28519 from [parent] pid 28518 [son] pid 28523 from [parent] pid 28518 [son] pid 28520 from [parent] pid 28518 [son] pid 28521 from [parent] pid 28518 [son] pid 28522 from [parent] pid 28518 here 28519, 28519,28520,28521,28522 are child processes from same parent process with process id 28518
#include<stdio.h> int main() { for ( int i=0;i<5;i++) // loop will run n times (n=5) { if (fork() == 0) { printf ( "[son] pid %d from [parent] pid %d" ,getpid(),getppid()); exit (0); } } for ( int i=0;i<5;i++) // loop will run n times (n=5) wait(NULL); } |
输出:
[son] pid 28519 from [parent] pid 28518 [son] pid 28523 from [parent] pid 28518 [son] pid 28520 from [parent] pid 28518 [son] pid 28521 from [parent] pid 28518 [son] pid 28522 from [parent] pid 28518
本文由 迪比恩杜·罗伊·乔杜里 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END