linux的fork,exec,wait 调用关系图,linux 进程学习笔记

linux进程学习笔记

进程控制块(PCB)

在Linux中task_struct结构体即是PCB。PCB是进程的唯一标识,PCB由链表实现(为了动态插入和删除)。

进程创建时,为该进程生成一个PCB;进程终止时,回收PCB。

PCB包含信息:1、进程状态(state);2、进程标识信息(uid、gid);3、定时器(time);4、用户可见寄存器、控制状态寄存器、栈指针等(tss)

每个进程都有一个非负的唯一进程ID(PID)。虽然是唯一的,但是PID可以重用,当一个进程终止后,其他进程就可以使用它的PID了。

PID为0的进程为调度进程,该进程是内核的一部分,也称为系统进程;PID为1的进程为init进程,它是一个普通的用户进程,但是以超级用户特权运行;PID为2的进程是页守护进程,负责支持虚拟存储系统的分页操作。除了PID,每个进程还有一些其他的标识符:

#if defined __USE_XOPEN_EXTENDED || defined __USE_XOPEN2K8

/* Return the session ID of the given process. */

extern __pid_t getsid (__pid_t __pid) __THROW;

#endif

/* Get the real user ID of the calling process. */

extern __uid_t getuid (void) __THROW;

/* Get the effective user ID of the calling process. */

extern __uid_t geteuid (void) __THROW;

/* Get the real group ID of the calling process. */

extern __gid_t getgid (void) __THROW;

/* Get the effective group ID of the calling process. */

extern __gid_t getegid (void) __THROW;

五种进程之间转换关系如图:

bVbtc9a?w=429&h=133

bVbtdbn?w=652&h=466

每个进程的task_struct和系统空间堆栈存放位置如下:两个连续的物理页【《Linux内核源代码情景分析》271页】

bVbtdbv?w=542&h=224

bVbtdbz?w=552&h=382

系统堆栈空间不能动态扩展,在设计内核、驱动程序时要避免函数嵌套太深,同时不宜使用太大太多的局部变量,因为局部变量都是存在堆栈中的。

进程的创建

新进程的创建,首先在内存中为新进程创建一个task_struct结构,然后将父进程的task_struct内容复制其中,再修改部分数据。分配新的内核堆栈、新的PID、再将task_struct 这个node添加到链表中。所谓创建,实际上是“复制”。

子进程刚开始,内核并没有为它分配物理内存,而是以只读的方式共享父进程内存,只有当子进程写时,才复制。即“copy-on-write”。

fork都是由do_fork实现的,do_fork的简化流程如下图:

bVbtdbQ?w=739&h=216

fork函数

#include

pid_t fork(void) //子进程返回0,父进程返回子进程ID,出错返回-1.

fork函数时调用一次,返回两次。在父进程和子进程中各调用一次。子进程中返回值为0,父进程中返回值为子进程的PID。程序员可以根据返回值的不同让父进程和子进程执行不同的代码。

一个形象的过程:

bVbtdfE?w=605&h=390

执行下面程序:

#include

#include

#include

int main()

{

pid_t pid;

char *message;

int n = 0;

pid = fork();

while(1){

if(pid < 0){

perror("fork failed\n");

exit(1);

}

else if(pid == 0){

n--;

printf("child's n is:%d\n",n);

}

else{

n++;

printf("parent's n is:%d\n",n);

}

sleep(1);

}

exit(0);

运行结果:

root@iZbp1anc6yju2dks3nw5j0Z:~/test# ./a.out

parent's n is:1

child's n is:-1

parent's n is:2

child's n is:-2

parent's n is:3

child's n is:-3

可以发现子进程和父进程之间并没有对各自的变量产生影响。

一般来说,fork之后父、子进程执行顺序是不确定的,这取决于内核调度算法。进程之间实现同步需要进行进程通信。

fork的应用

一个父进程希望子进程同时执行不同的代码段,这在网络服务器中常见——父进程等待客户端的服务请求,当请求到达时,父进程调用fork,使子进程处理此请求。

一个进程要执行一个不同的程序,一般fork之后立即调用exec

vfork函数

vfork与fork对比:

相同:

返回值相同

不同:

fork创建子进程,把父进程数据空间、堆和栈复制一份;vfork创建子进程,与父进程内存数据共享;

vfork先保证子进程先执行,当子进程调用exit()或者exec后,父进程才往下执行

为什么需要vfork?

因为用vfork时,一般都是紧接着调用exec,所以不会访问父进程数据空间,也就不需要在把数据复制上花费时间了,因此vfork就是”为了exec而生“的。

运行这样一段演示程序:

#include

#include

#include

int main()

{

pid_t pid;

char *message;

int n = 0;

int i;

pid = vfork();

for(i = 0; i < 10; i++){

if(pid < 0){

perror("fork failed\n");

exit(1);

}

else if(pid == 0){

n--;

printf("child's n is:%d\n",n);

if(i == 1)

_exit(0);

//return 0;

//exit(0);

}

else{

n++;

printf("parent's n is:%d\n",n);

}

sleep(1);

}

exit(0);

}

执行结果:

root@iZbp1anc6yju2dks3nw5j0Z:~/test# ./a.out

child's n is:-1

child's n is:-2

parent's n is:-1

parent's n is:0

parent's n is:1

parent's n is:2

parent's n is:3

parent's n is:4

可以发现子进程先被执行,exit后,父进程才被执行,同时子进程改变了父进程中的数据

子进程return 0 会发生什么?

运行结果:

root@iZbp1anc6yju2dks3nw5j0Z:~/test# ./a.out

child's n is:-1

child's n is:-2

parent's n is:32767

parent's n is:32768

parent's n is:32769

parent's n is:32770

parent's n is:32771

parent's n is:32772

从上面我们知道,结束子进程的调用是exit()而不是return,如果你在vfork中return了,那么,这就意味main()函数return了,注意因为函数栈父子进程共享,所以整个程序的栈就跪了。 如果你在子进程中return,那么基本是下面的过程: 1)子进程的main() 函数 return了,于是程序的函数栈发生了变化。 2)而main()函数return后,通常会调用 exit()或相似的函数(如:_exit(),exitgroup()) 3)这时,父进程收到子进程exit(),开始从vfork返回,但是尼玛,老子的栈都被你子进程给return干废掉了,你让我怎么执行?(注:栈会返回一个诡异一个栈地址,对于某些内核版本的实现,直接报“栈错误”就给跪了,然而,对于某些内核版本的实现,于是有可能会再次调用main(),于是进入了一个无限循环的结果,直到vfork 调用返回 error) 好了,现在再回到 return 和 exit,return会释放局部变量,并弹栈,回到上级函数执行。exit直接退掉。如果你用c++ 你就知道,return会调用局部对象的析构函数,exit不会。(注:exit不是系统调用,是glibc对系统调用 _exit()或_exitgroup()的封装) 可见,子进程调用exit() 没有修改函数栈,所以,父进程得以顺利执行。

【《vfork挂掉的一个问题》http://coolshell.cn/articles/...】

execve

可执行文件装入内核的linux_binprm结构体。

进程调用exec时,该进程执行的程序完全被替换,新的程序从main函数开始执行。因为调用exec并不创建新进程,只是替换了当前进程的代码区、数据区、堆和栈。

六种不同的exec函数:

bVbtd1d?w=668&h=272

当指定filename作为参数时:

如果filename中包含/,则将其视为路径名。

否则,就按系统的PATH环境变量,在它所指定的各个目录中搜索可执行文件。

*出于安全方面的考虑,有些人要求在搜索路径中不要包括当前目录。

在这6个函数中,只有execve是内核的系统调用。另外5个只是库函数,他们最终都要调用该系统调用,如下图所示:

bVbtd1v?w=608&h=172

execve的实现由do_execve完成,简化的实现过程如下图:

bVbtd1D?w=443&h=249

bVbtd1G?w=640&h=277

运行这样一段演示程序:

#include

#include

#include

char command[256];

void main()

{

int rtn; /*child process return value*/

while(1) {

printf( ">" );

fgets( command, 256, stdin );

command[strlen(command)-1] = 0;

if ( fork() == 0 ) {

execlp( command, NULL );

perror( command );

exit( errno );

}

else {

wait ( &rtn );

printf( " child process return %d\n", rtn );

}

}

}

a.out为打印hello,world的执行文件

运行结果:

root@iZbp1anc6yju2dks3nw5j0Z:~/test# ./test

>./a.out

hello,world

child process return 0

进程终止

正常终止(5种)

从main返回,等效于调用exit

调用exit

exit 首先调用各终止处理程序,然后按需多次调用fclose,关闭所有的打开流。

调用_exit或者_Exit

最后一个线程从其启动例程返回

最后一线程调用pthread_exit

异常终止(3种)

调用abort

接到一个信号并终止

最后一个线程对取消请求作出响应

wait 和 waitpid 函数

wait用于使父进程阻塞,等待子进程退出;waitpid有若干选项,如可以提供一个非阻塞版本的wait,也能实现和wait相同的功能,实际上,linux中wait的实现也是通过调用waitpid实现的。

waitpid返回值:正常返回子进程号;使用WNOHANG且没有子进程退出返回0;调用出错返回-1;

运行如下演示程序

#include

#include

#include

#include

int main()

{

pid_t pid0,pid1;

pid0 = fork();

if(pid0 < 0){

perror("fork");

exit(1);

}

else if(pid0 == 0){

sleep(5);

exit(0);//child

}

else{

do{

pid1 = waitpid(pid0,NULL,WNOHANG);

if(pid1 == 0){

printf("the child process has not exited.\n");

sleep(1);

}

}while(pid1 == 0);

if(pid1 == pid0){

printf("get child pid:%d",pid1);

exit(0);

}

else{

exit(1);

}

}

return 0;

}

当把第三个参数WNOHANG改为0时,就不会有上面五个显示语句了,说明父进程阻塞了。

a.out 的代码如下:

#include

void main()

{

printf("hello WYJ\n");

}

process.c的代码如下:

#include

#include

#include

#include

#include

#include

int main()

{

pid_t pid_1,pid_2,pid_wait;

pid_1 = fork();

pid_2 = fork();

if(pid_1 < 0){

perror("fork1 failed\n");

exit(1);

}else if(pid_1 == 0 && pid_2 != 0){//do not allow child 2 to excute this process.

if(execlp("./a.out", NULL) < 0){

perror("exec failed\n");

}//child;

exit(0);

}

if(pid_2 < 0){

perror("fork2 failded\n");

exit(1);

}else if(pid_2 == 0){

sleep(10);

}

if(pid_2 > 0){//parent

do{

pid_wait = waitpid(pid_2, NULL, WNOHANG);//no hang

sleep(2);

printf("child 2 has not exited\n");

}while(pid_wait == 0);

if(pid_wait == pid_2){

printf("child 2 has exited\n");

exit(0);

}else{

// printf("pid_2:%d\n",pid_2);

perror("waitpid error\n");

exit(1);

}

}

exit(0);

}

bVbtd76?w=568&h=475

运行结果:

root@iZbp1anc6yju2dks3nw5j0Z:~/test# ./proess

hello,world

child 2 has not exited

child 2 has not exited

child 2 has not exited

child 2 has not exited

child 2 has not exited

child 2 has not exited

child 2 has exited

WNOHANG 改为0运行结果:

root@iZbp1anc6yju2dks3nw5j0Z:~/test# ./proess

hello,world

child 2 has not exited

child 2 has exited

编写一个多进程程序:该实验有 3 个进程,其中一个为父进程,其余两个是该父进程创建的子进程,其中一个子进程运行“ls -l”指令,另一个子进程在暂停 5s 之后异常退出,父进程并不阻塞自己,并等待子进程的退出信息,待收集到该信息,父进程就返回。

bVbteeA?w=386&h=407

#include

#include

#include

#include

#include

#include

#include

int main()

{

pid_t child1,child2,child;

if((child1 = fork()) < 0){

perror("failed in fork 1");

exit(1);

}

if((child2 = fork()) < 0){

perror("failed in fork 2");

exit(1);

}

if(child1 == 0){

//run ls -l

if(child2 == 0){

printf("in grandson\n");

}

else if(execlp("ls", "ls", "-l", NULL) < 0){

perror("child1 execlp");

}

}

else if(child2 == 0){

sleep(5);

exit(0);

}

else{

do{

sleep(1);

printf("child2 not exits\n");

child = waitpid(child2, NULL, WNOHANG);

}while(child == 0);

if(child == child2){

printf("get child2\n");

}

else{

printf("Error occured\n");

}

}

}

运行结果:

bVbtefH?w=492&h=332

init进程成为所有僵尸进程(孤儿进程)的父进程

在unix/linux中,正常情况下,子进程是通过父进程创建的,子进程在创建新的进程。子进程的结束和父进程的运行是一个异步过程,即父进程永远无法预测子进程 到底什么时候结束。 当一个 进程完成它的工作终止之后,它的父进程需要调用wait()或者waitpid()系统调用取得子进程的终止状态。

孤儿进程:

一个父进程退出,而它的一个或多个子进程还在运行,那么那些子进程将成为孤儿进程。孤儿进程将被init进程(进程号为1)所收养,并由init进程对它们完成状态收集工作。

僵尸进程

在进程调用了exit之后,该进程并非马上就消失掉,而是留下了一个成为僵尸进程的数据结构,记载该进程的退出状态等信息供其他进程收集,除此之外,僵尸进程不再占有任何内存空间。

一个进程使用fork创建子进程,如果子进程退出,而父进程并没有调用wait或waitpid获取子进程的状态信息,那么子进程的进程描述符仍然保存在系统中。这种进程称之为僵尸进程。

子进程结束之后为什么会进入僵尸状态? 因为父进程可能会取得子进程的退出状态信息。

如何查看僵尸进程?

linux中命令ps,标记为Z的进程就是僵尸进程。

执行下面一段程序:

#include

#include

#include

#include

int main()

{

pid_t pid;

pid = fork();

if (pid < 0) {

printf("error occurred\n");

}

else if (pid == 0) {

exit(0);

}

else {

sleep(60);

wait(null);

}

}

运行结果:

bVbtehC?w=412&h=59

ps -ef|grep defunc可以找出僵尸进程

ps -l 可以得到更详细的进程信息

root@iZbp1anc6yju2dks3nw5j0Z:~/test# ps

PID TTY TIME CMD

6701 pts/8 00:00:00 bash

9756 pts/8 00:00:00 ps

其中S表示状态:

O:进程正在处理器运行

S:休眠状态

R:等待运行

I:空闲状态

Z:僵尸状态

T:跟踪状态

B:进程正在等待更多的内存分页

C:cpu利用率的估算值

收集僵尸进程的信息,并终结这些僵尸进程,需要我们在父进程中使用waitpid和wait,这两个函数能够手机僵尸进程留下的信息并使进程彻底消失。

守护进程

是linux的后台服务进程。它是一个生存周期较长的进程,没有控制终端,输出无处显示。用户层守护进程的父进程是init进程。

守护进程创建步骤:

1、创建子进程,父进程退出,子进程被init自动收养;fork exit

2、调用setsid创建新会话,成为新会话的首进程,成为新进程组的组长进程,摆脱父进程继承过来的会话、进程组等;setsid

3、改变当前目录为根目录,保证工作的文件目录不被删除;chdir(“/”)

4、重设文件权限掩码,给子进程更大的权限;umask(0)

5、关闭不用的文件描述符,因为会消耗资源;close

#include

#include

#include

#include

#include

#define MAXFILE 65535

int main()

{

int fd, len, i;

pid_t pid;

char* buf = "tick\n";

len = strlen(buf);

if ((pid = fork()) < 0) {

perror("fork failed");

exit(1);

}

else if (pid > 0) {

exit(0);

}

setsid();

if (chdir("/") < 0) {

perror("chdir failed");

exit(1);

}

umask(0);

for (i = 0; i < MAXFILE; i++) {

close(i);

}

while (1) {

if ((fd = open("/tmp/dameon.log", O_CREAT | O_WRONLY | O_APPEND, 0600)) < 0) {

perror("open log failed");

exit(1);

}

write(fd, buf, len + 1);

close(fd);

sleep(10);

}

}

#include

#include

#include

#include

#include

#include

#define MAXFILE 65535

int main()

{

int fd,len,i;

pid_t pid,child;

char *buf = "tick\n";

len = strlen(buf);

if((pid = fork()) < 0){

perror("fork failed");

exit(1);

}

else if(pid > 0){

exit(0);

}

openlog("Jack", LOG_PID, LOG_DAEMON);

if(setsid() < 0){

syslog(LOG_ERR, "%s\n", "setsid");

exit(1);

}

if(chdir("/") < 0){

syslog(LOG_ERR, "%s\n", "chdir");

exit(1);

}

umask(0);

for(i = 0; i < MAXFILE; i++){

close(i);

}

if((child = fork()) < 0){

syslog(LOG_ERR, "%s\n", "fork");

exit(1);

}

if(child == 0){

//printf("in child\n");//can not use terminal from now on.

syslog(LOG_INFO, "in child");

sleep(10);

exit(0);

}

else{

waitpid(child, NULL, 0);

//printf("child exits\n");//can not use terminal from now on.

syslog(LOG_INFO, "child exits");

closelog();

while(1){

sleep(10);

}

}

}

真正编写调试的时候会发现需要杀死守护进程。

如何杀死守护进程?

ps -aux 找到对应PID

kill -9 PID

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值