main()
{
int count = 0;
pid_t pid; /*此时仅有一个进程*/
pid=fork(); /*此时已经有两个进程在同时运行*/
if(pid<0) /*返回错误*/
printf("error in fork!");
else
{
if(pid==0) /*代码在子进程中执行*/
printf("I am the child process, my count is %d,my process ID is %d\n",count,getpid());
else /*代码在父进程中执行*/
printf("I am the parent process,my count is %d, my process ID is %d\n",++count,getpid());
}
}
/*test.c*/
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
main()
{
int count = 0;
pid_t pid; /*此时仅有一个进程*/
pid=fork(); /*此时已经有两个进程在同时运行*/
if(pid<0) /*返回错误*/
printf("error in fork!");
else
{
if(pid==0) /*代码在子进程中执行*/
printf("I am the child process, my count is %d,my process ID is %d\n",count,getpid());
else /*代码在父进程中执行*/
printf("I am the parent process,my count is %d, my process ID is %d\n",++count,getpid());
}
}
弄好后,在linux中键入:
$ gcc test.c -o test
$ ./test
在本次试验中
I am the parent process,my count is 1,my process ID is 3196
I am the child process, my count is 0,my process ID is 3776
view plaincopy to clipboardprint?
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
main()
{
int count = 1;
int child;
printf("Before create son, the father's count is:%d\n", count);//打印没创建进程前
if(!(child = vfork())) //创建子进程
{
printf("This is son, his pid is: %d and the count is: %d\n", getpid(), ++count);
exit(1);
} else
{
printf("After son, This is father, his pid is: %d and the count is: %d, and the child is: %d\n",
printf("Before create son, the father's count is:%d\n", count);//打印没创建进程前
if(!(child = vfork())) //创建子进程
{
printf("This is son, his pid is: %d and the count is: %d\n", getpid(), ++count);
exit(1);
} else
{
printf("After son, This is father, his pid is: %d and the count is: %d, and the child is: %d\n",
getpid(), count, child);
}
}
然后编译,执行,得到下列结果:
Before create son, the father's count is:1
This is son, his pid is: 4048 and the count is: 2
After son, This is father, his pid is: 4048 and the count is: 2, and the child is: 2748
view plaincopy to clipboardprint?
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
main()
{
int count = 1;
int child;
printf("Before create son, the father's count is:%d\n", count);
if(!(child = vfork()))
{//这里是子进程执行区
int i;
for(i = 0; i < 100; i++)
{
printf("This is son, The i is: %d\n", i);
if(i == 70)
exit(1);
}
printf("This is son, his pid is: %d and the count is: %d\n", getpid(), ++count);
exit(1);//子进程退出
}
else
{//父进程
printf("After son, This is father, his pid is: %d and the count is: %d, and the child is: %d\n",
printf("Before create son, the father's count is:%d\n", count);
if(!(child = vfork()))
{//这里是子进程执行区
int i;
for(i = 0; i < 100; i++)
{
printf("This is son, The i is: %d\n", i);
if(i == 70)
exit(1);
}
printf("This is son, his pid is: %d and the count is: %d\n", getpid(), ++count);
exit(1);//子进程退出
}
else
{//父进程
printf("After son, This is father, his pid is: %d and the count is: %d, and the child is: %d\n",
getpid(), count, child);
}
}
好,编译通过,执行。。。
Before create son, the father's count is:1
This is son, The i is: 0
...
...
This is son, The i is: 68
This is son, The i is: 69
This is son, The i is: 70
After son, This is father, his pid is: 2564 and the count is: 1, and the child is: 2736
文章评论(0条评论)
登录后参与讨论