linux下C的fork函数应用实例
2012-01-02 17:59
246 查看
最近在复习考试,顺便把一些心得写下来吧。
题目:写出一段程序,创建4个子进程,每个子进程都打印“Hello”后立刻终止,父进程等待4个子进程都终止后,打印“Bye”,然后终止。
答:这里有两种结构的程序,各位看哪种顺眼就看哪种吧~
本文出自 “标题不能为空” 博客,请务必保留此出处http://hector.blog.51cto.com/4229131/756378
题目:写出一段程序,创建4个子进程,每个子进程都打印“Hello”后立刻终止,父进程等待4个子进程都终止后,打印“Bye”,然后终止。
答:这里有两种结构的程序,各位看哪种顺眼就看哪种吧~
#include<stdio.h> #include<stdlib.h> #include<sys/types.h> #include<sys/wait.h> int main() { char *msg = "Hello"; pid_t pid; int count = 0; for(count = 0;count<4;count++) { pid = fork(); if (0 != pid) //父进程执行 wait(NULL); else //子进程执行 { printf("Child %d: ", count); puts("Hello"); exit(0); } } printf("Father : "); puts("Bye"); exit(0); } /* fork1: pid = fork(); if (0 != pid) //父进程执行 { wait(NULL); if (count < 3) { count++; goto fork1; } else { printf("Father : "); puts("Bye"); } } else //子进程执行 { printf("Child %d: ", count); puts("Hello"); } exit(0); }*/
本文出自 “标题不能为空” 博客,请务必保留此出处http://hector.blog.51cto.com/4229131/756378
相关文章推荐
- linux中fork()函数详解(原创!!实例讲解)
- Linux C read、write、creat、open等函数应用实例
- linux中fork()函数详解(实例讲解)
- linux中fork()函数详解(原创!!实例讲解)--写的太好了转过来
- Linux下的C开发之fork()函数的趣味应用
- Linux中使用C语言的fork()函数创建子进程的实例教程
- linux的list常用函数用法速查及应用实例
- linux中fork()函数详解(原创!!实例讲解) - jason的专栏 - 博客频道 - CSDN.NET
- linux中fork()函数详解(原创!!实例讲解)
- Linux 中的 fork() 函数实例解析
- linux中fork()函数详解(原创!!实例讲解)
- [置顶] linux中fork()函数详解(原创!!实例讲解)
- linux中fork()函数具体解释(原创!!实例解说)
- linux中fork()函数详解(原创!!实例讲解)
- linux中fork()函数详解(原创!!实例讲解) (转载)
- linux中fork()函数详解(原创!!实例讲解)
- Linux中fork()函数实例讲解
- [置顶] linux中fork()函数详解(原创!!实例讲解)
- linux中fork()函数详解(原创!!实例讲解)
- 使用C语言的fork()函数在Linux中创建进程的实例讲解