fork()函数的学习
2008-12-18 19:52
225 查看
转的一篇文章:[/b]
fork[/b]函数[/b]的学习。
先看这段范例代码:
#include <unistd.h>;
#include <sys/types.h>;
main ()
{
pid_t pid;
pid=fork[/b]();
if (pid < 0)
printf("error in fork[/b]!");
else if (pid == 0)
printf("i am the child process, my process id is %dn",getpid());
else
printf("i am the parent process, my process id is %dn",getpid());
}
这段代码写了一个使用fork[/b]函数[/b]创建子进程,父子进程同时运行而产生交错的,不一样的运行结果。
运行结果如下:
[root@localhost c]# ./a.out
i am the child process, my process id is 4286
i am the parent process, my process id is 4285
fork[/b]在英文中[/b]是叉子,分叉的意思,在函数[/b]fork[/b]中[/b],取后面的意思。很形象的表示程序从这里分叉,fork[/b]函数[/b]创建了子进程,子进程和父进程同时(其实是cpu分时处理)开始运行分叉之后的程序。
我把程序改写了一下:
#include <unistd.h>
#include <sys/types.h>
main()
{
pid_t pid;
printf("/n[%d]not fork[/b] pid=%d/n",getpid(),pid);
pid=fork[/b]();
printf("/n[%d]forked pid=%d/n",getpid(),pid);
if(pid<0)
{
printf("error in fork[/b]!/n");
getchar();
exit(1);
}
else if(pid==0)
printf("/n[%d]in child process,p_id=%d/n",getpid(),getpid());
else
{
printf("/n[%d]in parent process,my pid=%d/n",getpid(),pid);
printf("/n[%d]in parent process,my getpid=%d/n",getpid(),getpid());
}
}
程序运行结果如下:
[hardy@localhost fork[/b]]$ ./fork[/b]
[3819]not fork[/b]
[3820]forked pid=0
[3820]in child process,p_id=3820
[3819]forked pid=3820
[3819]in parent process,my pid=3820
[3819]in parent process,my getpid=3819
可以清楚的看到 not fork[/b]只打印了一次,其中[3819]是父进程的进程号,创建fork[/b]以后,fork[/b]函数[/b]返回给父进程的值pid是子进程的进程号[3820],而在子进程中[/b],pid值为零。也就是说子进程中[/b],pid被置零。
引用网上一位网友的解释“其实就相当于链表,进程形成了链表,父进程pid(p 意味point)指向子进程的进程id, 因为子进程没有子进程,所以其pid为0. ”
下面有一个很有意思的程序:
#include <sys/types.h>
#include <unistd.h>
int main()
{
int i;
for( i= 0; i< 3; i++)
{
int pid= fork[/b]();
if(pid== 0)
{
printf("son/n");
}
else
{
printf("father/n");
}
}
return 0;
}
大家想想看最后将出现几个son 几个father呢?
。
。
。
。
。
。
。
对一下答案吧:
[hardy@localhost fork[/b]]$ ./fork[/b]
father
son
son
son
father
father
son
father
son
son
father
father
son
father
总共7个son7个father。你答对了么?
这道题需要在纸上画画才好理解
for i=0 1 2
father father father
son
son father
son
son father father
son
son father
son
其中每一行分别代表一个进程的运行打印结果。
当产生子进程的时刻,子进程打印son,当子进程调用fork[/b]的生成子子进程,他就提升为father。
总结来说,father永远打印father,son在fork[/b]之前是son,fork[/b]之后就为father,同时生成新的son。
这个比喻就像真正的父子,孩子长大了生了小孩,孩子就成了父亲。而父亲永远是父亲。
fork[/b]函数[/b]的学习。
先看这段范例代码:
#include <unistd.h>;
#include <sys/types.h>;
main ()
{
pid_t pid;
pid=fork[/b]();
if (pid < 0)
printf("error in fork[/b]!");
else if (pid == 0)
printf("i am the child process, my process id is %dn",getpid());
else
printf("i am the parent process, my process id is %dn",getpid());
}
这段代码写了一个使用fork[/b]函数[/b]创建子进程,父子进程同时运行而产生交错的,不一样的运行结果。
运行结果如下:
[root@localhost c]# ./a.out
i am the child process, my process id is 4286
i am the parent process, my process id is 4285
fork[/b]在英文中[/b]是叉子,分叉的意思,在函数[/b]fork[/b]中[/b],取后面的意思。很形象的表示程序从这里分叉,fork[/b]函数[/b]创建了子进程,子进程和父进程同时(其实是cpu分时处理)开始运行分叉之后的程序。
我把程序改写了一下:
#include <unistd.h>
#include <sys/types.h>
main()
{
pid_t pid;
printf("/n[%d]not fork[/b] pid=%d/n",getpid(),pid);
pid=fork[/b]();
printf("/n[%d]forked pid=%d/n",getpid(),pid);
if(pid<0)
{
printf("error in fork[/b]!/n");
getchar();
exit(1);
}
else if(pid==0)
printf("/n[%d]in child process,p_id=%d/n",getpid(),getpid());
else
{
printf("/n[%d]in parent process,my pid=%d/n",getpid(),pid);
printf("/n[%d]in parent process,my getpid=%d/n",getpid(),getpid());
}
}
程序运行结果如下:
[hardy@localhost fork[/b]]$ ./fork[/b]
[3819]not fork[/b]
[3820]forked pid=0
[3820]in child process,p_id=3820
[3819]forked pid=3820
[3819]in parent process,my pid=3820
[3819]in parent process,my getpid=3819
可以清楚的看到 not fork[/b]只打印了一次,其中[3819]是父进程的进程号,创建fork[/b]以后,fork[/b]函数[/b]返回给父进程的值pid是子进程的进程号[3820],而在子进程中[/b],pid值为零。也就是说子进程中[/b],pid被置零。
引用网上一位网友的解释“其实就相当于链表,进程形成了链表,父进程pid(p 意味point)指向子进程的进程id, 因为子进程没有子进程,所以其pid为0. ”
下面有一个很有意思的程序:
#include <sys/types.h>
#include <unistd.h>
int main()
{
int i;
for( i= 0; i< 3; i++)
{
int pid= fork[/b]();
if(pid== 0)
{
printf("son/n");
}
else
{
printf("father/n");
}
}
return 0;
}
大家想想看最后将出现几个son 几个father呢?
。
。
。
。
。
。
。
对一下答案吧:
[hardy@localhost fork[/b]]$ ./fork[/b]
father
son
son
son
father
father
son
father
son
son
father
father
son
father
总共7个son7个father。你答对了么?
这道题需要在纸上画画才好理解
for i=0 1 2
father father father
son
son father
son
son father father
son
son father
son
其中每一行分别代表一个进程的运行打印结果。
当产生子进程的时刻,子进程打印son,当子进程调用fork[/b]的生成子子进程,他就提升为father。
总结来说,father永远打印father,son在fork[/b]之前是son,fork[/b]之后就为father,同时生成新的son。
这个比喻就像真正的父子,孩子长大了生了小孩,孩子就成了父亲。而父亲永远是父亲。
相关文章推荐
- 零基础学习linux中的fork()函数
- Linux 进程学习1 --fork()函数
- UNIX环境编程学习笔记(19)——进程管理之fork 函数的深入学习
- linux学习之十---vfork()&fork()函数
- 一次调用,两次返回--fork函数学习总结
- 第一课关于 fork 和 exec 函数的学习
- 守护进程学习————两次fork细节、deamon()函数
- UNIX环境编程学习笔记(19)——进程管理之fork 函数的深入学习
- UNIX再学习 -- 函数 fork 和 vfork
- Linux基础学习系列:对于fork()函数的学习,及进程创建相关知识
- 【操作系统】fork()、exec()函数学习笔记
- linux内核学习-3 fork()函数(关注新浪微博:寂寞侵蚀的岁月(4000多篇技术分享))
- 进程学习(一)——fork()和vfork()函数学习过程
- Linux基础学习系列:对于fork()函数的学习,及进程创建相关知识
- fork函数的使用【学习笔记】
- UNIX环境编程学习笔记(19)——进程管理之fork 函数的深入学习
- Linux C学习--getline()函数
- 函数指针学习笔记
- C语言学习笔记:10_函数_高级
- python 学习(十七)之函数