大良o2o网站建设,百度手机卫士下载安装,网页游戏制作工具,wordpress 新建数据库linux系统调用fork()创建一个和当前进程完全相同的拷贝进程#xff0c;其中父进程和子进程的代码段#xff0c;堆栈段#xff0c;数据段均独立 进程必须的4要点#xff1a; a.要有一段程序供该进程运行 b.进程专用的系统堆栈空间。 c.进程控制块#xff0c;在linux中具体实… linux系统调用fork()创建一个和当前进程完全相同的拷贝进程其中父进程和子进程的代码段堆栈段数据段均独立 进程必须的4要点 a.要有一段程序供该进程运行 b.进程专用的系统堆栈空间。 c.进程控制块在linux中具体实现是task_struct d.有独立的存储空间。 当一个进程缺少其中一个条件时候我们称其为线程。 1.先看fork()的原型 #includesys/types.h /* 提供类型pid_t的定义 */ #includeunistd.h /* 提供函数的定义 */ pid_t fork(void); 返回 父进程中执行则返回子进程PID子进程中执行返回0 现在进行一个例子创建一个子进程然后根据返回的 PID进行识别 [cpp] view plaincopy /*test.c*/ #include sys/types.h #include sys/stat.h #include unistd.h main() { int count 0; pid_t pid; /*此时仅有一个进程*/ pidfork(); /*此时已经有两个进程在同时运行*/ if(pid0) /*返回错误*/ printf(error in fork!); else { if(pid0) /*代码在子进程中执行*/ 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 从代码里面可以看出2者的pid不同内存资源count是值得复制父进程改变了count的值而子进程中的count没有被改变。有人认为这样大批量的复制会导致执行效率过低。其实在复制过程中子进程复制了父进程的task_struct系统堆栈空间和页面表这意味着上面的程序我们没有执行count前其实子进程和父进程的count指向的是同一块内存。而当子进程改变了父进程的变量时候会通过copy_on_write的手段为所涉及的页面建立一个新的副本。所以当我们执行count后这时候子进程才新建了一个页面复制原来页面的内容基本资源的复制是必须的而且是高效的。整体看上去就像是父进程的独立存储空间也复制了一遍。 在fork中父子进程是独立开来的 并没有影响 2.vfork函数 vfork创建出来的不是真正意义上的进程而是一个线程因为它缺少了上面提到的进程的四要素的第4项独立的内存资源我们编一个程序练习 [cpp] view plaincopy #include sys/types.h #include sys/stat.h #include unistd.h main() { int count 1; int child; printf(Before create son, the fathers 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 fathers 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 从这里我们看到子进程和父进程是共享count的也就是说内存区是共享的 另外由vfork创造出来的子进程还会导致父进程挂起除非子进程exit或者execve才会唤起父进程看下面程序 [cpp] view plaincopy #include sys/types.h #include sys/stat.h #include unistd.h main() { int count 1; int child; printf(Before create son, the fathers 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 fathers 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 可以看到父进程总是等子进程执行完毕后才开始继续执行