新建网站seo优化怎么做,网站建设整体策划,专做火影黄图的网站,苏州网站建设哪家好#include unistd.h
pid_t fork(void);
作用#xff1a;创建一个子进程。
到目前为止#xff0c;我们可以直到两种创建进程的方法#xff1a;1. 通过执行二进制文件来创建一个进程#xff0c;如#xff1a;./a.out /bin/ls#xff1b;2.通过fork函数来创建一个…#include unistd.h
pid_t fork(void);
作用创建一个子进程。
到目前为止我们可以直到两种创建进程的方法1. 通过执行二进制文件来创建一个进程如./a.out /bin/ls2.通过fork函数来创建一个子进程。
该函数没有形参返回值类型为pid_t为无符号整型。而进程的id为正整数。如果失败则返回-1如果成功则由于父进程调用fork函数的进程创建了一个子进程因此父进程与子进程都会有一个fork函数一个fork函数变为了两个fork函数从而就会有两个返回值父进程的fork返回子进程的id其值大于0子进程的fork返回0表示进程创建成功因此可以根据返回值来判断哪个是父进程哪个是子进程。
注意注意返回值不是fork函数能返回两个值而是fork后fork函数变为两个父子需各自返回一个。 #include sys/types.h
#include unistd.h
pid_t getpid(void);
pid_t getppid(void)
作用getpid返回当前进程调用这一函数的进程的IDgetppid返回当前进程的父进程的ID。都无形参。 这两个函数总是成功因此返回值大于0。
//代码示例
#include stdio.h
#include stdlib.h
#include unistd.hint main(void)
{pid_t pid; //id为pid_t类型printf(xxxxxxxxxxx\n); //这部分只是父进程执行子进程虽然有这部分的代码但是不执行而是从创建处开始向下执行。pid fork(); //此处产生了一个子进程if(pid -1){perror(fork);exit(1);}else if(pid 0) //如果是子进程printf(This is child process, and the pid %u, the ppid %u\n,getpid( ),getppid( ));else //如果是父进程{printf(This is parent process, and the pid %u, the ppid %u\n,getpid( ),getppid( ));sleep(1); // 保证子进程比父进程先结束}printf(yyyyyyyyyyyy, the pid %u\n,getpid( )); //父子进程都要执行这段代码return 0;
}
[rootlocalhost fork]# ./fork_test1
xxxxxxxxxxx //只是父进程执行
This is parent process, and the pid 14652, the ppid 4199 //父进程也有父进程
yyyyyyyyyyyy, the pid 14652 //父进程执行
This is child process, and the pid 14653, the ppid 14652 //子进程
yyyyyyyyyyyy, the pid 14653 //子进程执行
由上可以看出fork后的代码父子进程都执行fork前的代码只是父进程执行虽然子进程也包含这段代码但其只是从创建处开始执行。
[rootlocalhost fork]# ps aux | grep 4199 //查看父进程的父进程
root 4199 0.0 0.1 116352 3160 pts/0 Ss Mar24 0:01 -bash
root 14722 0.0 0.0 112644 972 pts/0 S 00:18 0:00 grep --colorauto 4199
可以看到父进程的父进程中断为bash解释器即终端进程其在执行二进制文件时也通过fork函数创建一个子进程来完成该二进制文件./fork_test1的执行其原理与父进程创建子进程原理一样。