《linux 内核完全剖析》 exit.c 代码分析笔记

2024-06-06 10:08

本文主要是介绍《linux 内核完全剖析》 exit.c 代码分析笔记,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 

  exit.c 代码分析笔记

 release

          释放进程的函数release() 主要根据指定进程的任务数据结构指针,在任务数组中删除指定的进程指针,释放相关内存页,并立刻让内核重新调度进程的运行。



void release(struct task_struct * p) //释放p指向的进程
{int i;if (!p) //常规检测p是否为0return;if (p == current) { //不能把自己给释放了printk("task releasing itself\n\r");return;}for (i=1 ; i<NR_TASKS ; i++) //扫描所有的进程if (task[i]==p) { //找出p进程task[i]=NULL; //把p置空/* Update links */if (p->p_osptr) //调整链表p->p_osptr->p_ysptr = p->p_ysptr;if (p->p_ysptr)p->p_ysptr->p_osptr = p->p_osptr;elsep->p_pptr->p_cptr = p->p_osptr;free_page((long)p); //释放p进程占用的内存页schedule();//任务调度return;}panic("trying to release non-existent task");
}

bad_task_ptr 和audit_ptree


#ifdef DEBUG_PROC_TREE //下面这部分代码是调试用的
/** Check to see if a task_struct pointer is present in the task[] array* Return 0 if found, and 1 if not found.*/
int bad_task_ptr(struct task_struct *p)
{int     i;if (!p)return 0;for (i=0 ; i<NR_TASKS ; i++)if (task[i] == p)return 0;return 1; //如果p不在task数组里面,那这个指针就有问题!
}/** This routine scans the pid tree and make sure the rep invarient still* holds.  Used for debugging only, since it's very slow....** It looks a lot scarier than it really is.... we're doing ænothing more* than verifying the doubly-linked list foundæin p_ysptr and p_osptr,* and checking it corresponds with the process tree defined by p_cptr and* p_pptr;*/
void audit_ptree() //其实不难,就是打印进程树的相关信息,方便调试用
{int    i;for (i=1 ; i<NR_TASKS ; i++) {if (!task[i])continue;if (bad_task_ptr(task[i]->p_pptr))printk("Warning, pid %d's parent link is bad\n",task[i]->pid);if (bad_task_ptr(task[i]->p_cptr))printk("Warning, pid %d's child link is bad\n",task[i]->pid);if (bad_task_ptr(task[i]->p_ysptr))printk("Warning, pid %d's ys link is bad\n",task[i]->pid);if (bad_task_ptr(task[i]->p_osptr))printk("Warning, pid %d's os link is bad\n",task[i]->pid);if (task[i]->p_pptr == task[i])printk("Warning, pid %d parent link points to self\n");if (task[i]->p_cptr == task[i])printk("Warning, pid %d child link points to self\n");if (task[i]->p_ysptr == task[i])printk("Warning, pid %d ys link points to self\n");if (task[i]->p_osptr == task[i])printk("Warning, pid %d os link points to self\n");if (task[i]->p_osptr) {if (task[i]->p_pptr != task[i]->p_osptr->p_pptr)printk("Warning, pid %d older sibling %d parent is %d\n",task[i]->pid, task[i]->p_osptr->pid,task[i]->p_osptr->p_pptr->pid);if (task[i]->p_osptr->p_ysptr != task[i])printk("Warning, pid %d older sibling %d has mismatched ys link\n",task[i]->pid, task[i]->p_osptr->pid);}if (task[i]->p_ysptr) {if (task[i]->p_pptr != task[i]->p_ysptr->p_pptr)printk("Warning, pid %d younger sibling %d parent is %d\n",task[i]->pid, task[i]->p_osptr->pid,task[i]->p_osptr->p_pptr->pid);if (task[i]->p_ysptr->p_osptr != task[i])printk("Warning, pid %d younger sibling %d has mismatched os link\n",task[i]->pid, task[i]->p_ysptr->pid);}if (task[i]->p_cptr) {if (task[i]->p_cptr->p_pptr != task[i])printk("Warning, pid %d youngest child %d has mismatched parent link\n",task[i]->pid, task[i]->p_cptr->pid);if (task[i]->p_cptr->p_ysptr)printk("Warning, pid %d youngest child %d has non-NULL ys link\n",task[i]->pid, task[i]->p_cptr->pid);}}
}
#endif /* DEBUG_PROC_TREE */


send_sig

static inline int send_sig(long sig,struct task_struct * p,int priv) //给进程p,发送信号。priv 是强制发送信号的标识
{if (!p) //常规的p非空检测return -EINVAL;if (!priv && (current->euid!=p->euid) && !suser())//如果不具有超级用户权限,又不是当前session里面的进程,且没有强制发送信号,进入if,returnreturn -EPERM;if ((sig == SIGKILL) || (sig == SIGCONT)) { //如果要发送的信号是SIGKILL或者SIGCONTif (p->state == TASK_STOPPED) //如果当前进程处于stop状态,则将其置于TASK_RUNNING状态p->state = TASK_RUNNING;p->exit_code = 0;p->signal &= ~( (1<<(SIGSTOP-1)) | (1<<(SIGTSTP-1)) |(1<<(SIGTTIN-1)) | (1<<(SIGTTOU-1)) ); //消除SIGSTOP SIGTSTP SIGTTIN SIGTTOU}/* If the signal will be ignored, don't even post it */if ((int) p->sigaction[sig-1].sa_handler == 1) //如果handler是 ignore 就不要送信号鸟。。return 0;/* Depends on order SIGSTOP, SIGTSTP, SIGTTIN, SIGTTOU */if ((sig >= SIGSTOP) && (sig <= SIGTTOU))//如果信号含有 SIGSTOP, SIGTSTP, SIGTTIN, SIGTTOU其中任何信号,那么就消除SIGCONT信号p->signal &= ~(1<<(SIGCONT-1));/* Actually deliver the signal */p->signal |= (1<<(sig-1)); //最后这里才把信号写入信号变量return 0;
}


session_of_pgrp

int session_of_pgrp(int pgrp) //获取process group的session ID ,没有找到返回-1
{struct task_struct **p;for (p = &LAST_TASK ; p > &FIRST_TASK ; --p)if ((*p)->pgrp == pgrp)return((*p)->session);return -1;
}


kill_pg

int kill_pg(int pgrp, int sig, int priv) //给指定的进程组发送信号
{struct task_struct **p;int err,retval = -ESRCH; //指定的进程不存在int found = 0;if (sig<1 || sig>32 || pgrp<=0)return -EINVAL;for (p = &LAST_TASK ; p > &FIRST_TASK ; --p)if ((*p)->pgrp == pgrp) { //对每一个属于process group的进程发送信号sigif (sig && (err = send_sig(sig,*p,priv)))retval = err;elsefound++;}return(found ? 0 : retval);
}


kill_proc

int kill_proc(int pid, int sig, int priv) //给指定进程发送信号
{struct task_struct **p;if (sig<1 || sig>32)return -EINVAL;for (p = &LAST_TASK ; p > &FIRST_TASK ; --p)if ((*p)->pid == pid)return(sig ? send_sig(sig,*p,priv) : 0);return(-ESRCH);
}

sys_kill


        系统调用sys_kill 用于项进程发送任何指定的信号,根据参数pid不同的数值,该系统调用会向不同的进或进程组发送信号。

/** POSIX specifies that kill(-1,sig) is unspecified, but what we have* is probably wrong.  Should make it like BSD or SYSV.*/
int sys_kill(int pid,int sig)
{struct task_struct **p = NR_TASKS + task;int err, retval = 0;if (!pid) //如果是进程0 init ,用最高权限发送信号,将信号发送给当前进程所处进程组的所有进程return(kill_pg(current->pid,sig,0));if (pid == -1) { //如果 pid是-1,把信号发送到除了init进程外的所有进程!while (--p > &FIRST_TASK)if (err = send_sig(sig,*p,0))retval = err;return(retval);}if (pid < 0) //如果pid<0,发送到|pid| 所处进程组的所有进程return(kill_pg(-pid,sig,0));/* Normal kill */return(kill_proc(pid,sig,0)); //否则,发送到进程pid
}


is_orphaned_pgrp

关于孤儿进程组的探讨

/** Determine if a process group is "orphaned", according to the POSIX* definition in 2.2.2.52.  Orphaned process groups are not to be affected* by terminal-generated stop signals.  Newly orphaned process groups are* to receive a SIGHUP and a SIGCONT.** "I ask you, have you ever known what it is to be an orphan?"*/
int is_orphaned_pgrp(int pgrp) //判断是否为一个孤儿进程组
{struct task_struct **p;for (p = &LAST_TASK ; p > &FIRST_TASK ; --p) {if (!(*p) || //如果进程不存在,下一个((*p)->pgrp != pgrp) ||  // 如果进程所处的进程组不是pgrp,下一个((*p)->state == TASK_ZOMBIE) || // 进程的状态是zombie,下一个((*p)->p_pptr->pid == 1)) //进程parent是init ,下一个continue;if (((*p)->p_pptr->pgrp != pgrp) && //如果父进程所在的组不在pgrp,但是父进程所在的session存在((*p)->p_pptr->session == (*p)->session))return 0;}return(1);    /* (sighing) "Often!" */
}

has_stopped_jobs

static int has_stopped_jobs(int pgrp) //判断进程组内是否有进程处于TASK_STOPPED状态
{struct task_struct ** p;for (p = &LAST_TASK ; p > &FIRST_TASK ; --p) {if ((*p)->pgrp != pgrp)continue;if ((*p)->state == TASK_STOPPED)return(1);}return(0);
}


do_exit

volatile void do_exit(long code)
{struct task_struct *p;int i;free_page_tables(get_base(current->ldt[1]),get_limit(0x0f));free_page_tables(get_base(current->ldt[2]),get_limit(0x17));/*这里采用的两个参数0x0f即1111B,因此他指向的段优先级为3,存储在LDT表中,索引为1。也就是当前任务段的代码段描述符。另外一个参数0x17即10111B,因此他指向的段优先级为3,存储在LDT表中,索引为2.也就是当前任务的数及堆栈段描述符。这样,通过get_limit(0x0f)和get_limit(0x17)就得到了当前任务代码段和数据堆栈段的长度。因此,
free_page_tables(get_base(current->ldt[1]),get_limit(0x0f));
free_page_tables(get_base(current->ldt[2]),get_limit(0x17));就释放了当前任务的代码段和数据堆栈段。*/for (i=0 ; i<NR_OPEN ; i++)if (current->filp[i]) //filp文件结构指针表,filp[i]就是文件i被打开的IDsys_close(i); //关闭进程当前打开的文件iput(current->pwd); //和fs inode有关系current->pwd = NULL; //当前进程的工作目录置为NULLiput(current->root);//和fs inode有关系current->root = NULL;//当前进程的根目录置为NULLiput(current->executable);//和fs inode有关系current->executable = NULL;//当前进程的执行程序文件的i节点置为NULLiput(current->library);//和fs inode有关系current->library = NULL;//当前进程的库文件置为NULLcurrent->state = TASK_ZOMBIE; //状态置为TASK_ZOMBIEcurrent->exit_code = code; //设置进程退出码/** Check to see if any process groups have become orphaned* as a result of our exiting, and if they have any stopped* jobs, send them a SIGUP and then a SIGCONT.  (POSIX 3.2.2.2)** Case i: Our father is in a different pgrp than we are* and we were the only connection outside, so our pgrp* is about to become orphaned.*/if ((current->p_pptr->pgrp != current->pgrp) &&(current->p_pptr->session == current->session) &&is_orphaned_pgrp(current->pgrp) &&has_stopped_jobs(current->pgrp)) { //如果当前进程组是个orphaned groupkill_pg(current->pgrp,SIGHUP,1);//用于只是这些进程和当前session 断开联系,我这里不是很明白为什么要发送SIGHUP 和 SIGCONTkill_pg(current->pgrp,SIGCONT,1);}/* Let father know we died */current->p_pptr->signal |= (1<<(SIGCHLD-1)); // 告诉parent process 当前进程要挂掉鸟/** This loop does two things:** A.  Make init inherit all the child processes* B.  Check to see if any process groups have become orphaned*    as a result of our exiting, and if they have any stopped*    jons, send them a SIGUP and then a SIGCONT.  (POSIX 3.2.2.2)*/if (p = current->p_cptr) { //如果当前进程的child process 非空,进入ifwhile (1) {p->p_pptr = task[1]; //让init进程领养current 进程的child 进程if (p->state == TASK_ZOMBIE) //p进程沦为zombietask[1]->signal |= (1<<(SIGCHLD-1)); //向init进程发送SIGCHLD信号/** process group orphan check* Case ii: Our child is in a different pgrp* than we are, and it was the only connection* outside, so the child pgrp is now orphaned.*/if ((p->pgrp != current->pgrp) &&(p->session == current->session) &&is_orphaned_pgrp(p->pgrp) &&has_stopped_jobs(p->pgrp)) {//判断p进程所在进程组是否是orphaned group,如果是。发送SIGHUP和SIGCONT信号kill_pg(p->pgrp,SIGHUP,1);kill_pg(p->pgrp,SIGCONT,1);}if (p->p_osptr) { //如果p_osptr非空,则把p->p_osptr赋值给pp = p->p_osptr;continue; //下一轮循环,知道p_osptr为NULL}/** This is it; link everything into init's children* and leave*/p->p_osptr = task[1]->p_cptr;task[1]->p_cptr->p_ysptr = p;task[1]->p_cptr = current->p_cptr; //这三句把这个child process —— p 更新为 init 进程的 最年轻的child processcurrent->p_cptr = 0; //处理完所有的child process ,把 当前进程的p_cptr置为NULLbreak; //处理完了跳出while}}if (current->leader) { //如果当前进程是session leaderstruct task_struct **p;struct tty_struct *tty;if (current->tty >= 0) { //切断当前进程和terminal 的联系tty = TTY_TABLE(current->tty); //tty 还没看布吉岛。。。if (tty->pgrp>0)kill_pg(tty->pgrp, SIGHUP, 1);tty->pgrp = 0;tty->session = 0;}for (p = &LAST_TASK ; p > &FIRST_TASK ; --p) //把所有和当前进程同一个session的进程的tty都置为-1if ((*p)->session == current->session)(*p)->tty = -1;}if (last_task_used_math == current)//数字协处理器木有看。。。布吉岛,//但是语句的意思是上次用过协处理器的进程是当前进程的话酒吧last_task_used_math 置为NULLlast_task_used_math = NULL;
#ifdef DEBUG_PROC_TREEaudit_ptree();
#endifschedule(); //进程调度
}


sys_exit

int sys_exit(int error_code) //系统调用sys_exit函数,通过调用do_exit 函数来实现
{do_exit((error_code&0xff)<<8);//error_code是用户程序提供的退出状态信息,只有低字节有效//把error_code 左移八位是空出第八位供wait()和waitpid()函数使用
}

sys_waitpid


             用于挂起当前进程,知道pid指定的进程退出或者收到要求该进程终止的信号,或者是需要调用一个signal handler。如果pid 所指的进程早已退出(zombie)则本调用立刻返回。子进程的所有资源将被释放。

int sys_waitpid(pid_t pid,unsigned long * stat_addr, int options)//其实我一般都木有怎么用stat_addr 这边选项,一般我都NULL了 <—_<—
//waitpid居然放在。。。exit.c 里,木有别的,只有惊奇
{int flag;struct task_struct *p;unsigned long oldblocked;verify_area(stat_addr,4); //验证stat_addr 地址处4byte 可写
repeat:flag=0;for (p = current->p_cptr ; p ; p = p->p_osptr) {if (pid>0) {if (p->pid != pid) //找到pid描述的child进程的指针continue;} else if (!pid) { //如果pid等于0, 找到pid 所在的进程组if (p->pgrp != current->pgrp)continue;} else if (pid != -1) {   //如果pid < -1 ,找到|pid|所在的进程组if (p->pgrp != -pid)continue;}// 如果以上判断田间都没进if去,pid = -1 ,这里不是很明白,看完fork之后回头再updateswitch (p->state) {case TASK_STOPPED: //如果进程p处于TASK_STOPPED状态if (!(options & WUNTRACED) ||  //如果option的WUNTRACED 没有置位或者 exit_code 等于0 ,对下一个older child进行检测!p->exit_code)continue;put_fs_long((p->exit_code << 8) | 0x7f,stat_addr);//把第一个参数写入第二个参数指向的地址p->exit_code = 0; //正常退出return p->pid; //返回结束child 的pidcase TASK_ZOMBIE:current->cutime += p->utime;current->cstime += p->stime; //更新时间参数flag = p->pid;put_fs_long(p->exit_code, stat_addr); //把p->exit_code 写入stat_addrrelease(p); //释放进程p占用的内存页
#ifdef DEBUG_PROC_TREEaudit_ptree();
#endifreturn flag;default:flag=1;continue;}}if (flag) { //存在不处于TASK_STOPPED 和TASK_ZOMBIE状态的进程if (options & WNOHANG) //如果WNOHANG(表示若没有子进程处于退出或者终止状态就返回)在options中置位,就立刻返回0;return 0; //一般我是不会用WNOHANG的。。。坦白的说我都没用过current->state=TASK_INTERRUPTIBLE; //更新进程状态,置为TASK_INTERRUPTIBLEoldblocked = current->blocked; //当前进程阻塞的信号储存于oldblocked中current->blocked &= ~(1<<(SIGCHLD-1)); //消除当前进程中的SIGCHLD信号schedule();//进程调度current->blocked = oldblocked; //回复原来的信号图if (current->signal & ~(current->blocked | (1<<(SIGCHLD-1))))//如果收到了除了SIGCHLD之外的其他信号,return 挂掉return -ERESTARTSYS;elsegoto repeat; //否则goto到repeat 继续重复,直到return 出去。。。。。原来是这样waitpid的啊!}return -ECHILD; //没有找到pid对应的child process,返回错误码
}




这篇关于《linux 内核完全剖析》 exit.c 代码分析笔记的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1035804

相关文章

linux-基础知识3

打包和压缩 zip 安装zip软件包 yum -y install zip unzip 压缩打包命令: zip -q -r -d -u 压缩包文件名 目录和文件名列表 -q:不显示命令执行过程-r:递归处理,打包各级子目录和文件-u:把文件增加/替换到压缩包中-d:从压缩包中删除指定的文件 解压:unzip 压缩包名 打包文件 把压缩包从服务器下载到本地 把压缩包上传到服务器(zip

性能分析之MySQL索引实战案例

文章目录 一、前言二、准备三、MySQL索引优化四、MySQL 索引知识回顾五、总结 一、前言 在上一讲性能工具之 JProfiler 简单登录案例分析实战中已经发现SQL没有建立索引问题,本文将一起从代码层去分析为什么没有建立索引? 开源ERP项目地址:https://gitee.com/jishenghua/JSH_ERP 二、准备 打开IDEA找到登录请求资源路径位置

活用c4d官方开发文档查询代码

当你问AI助手比如豆包,如何用python禁止掉xpresso标签时候,它会提示到 这时候要用到两个东西。https://developers.maxon.net/论坛搜索和开发文档 比如这里我就在官方找到正确的id描述 然后我就把参数标签换过来

Linux 网络编程 --- 应用层

一、自定义协议和序列化反序列化 代码: 序列化反序列化实现网络版本计算器 二、HTTP协议 1、谈两个简单的预备知识 https://www.baidu.com/ --- 域名 --- 域名解析 --- IP地址 http的端口号为80端口,https的端口号为443 url为统一资源定位符。CSDNhttps://mp.csdn.net/mp_blog/creation/editor

【Python编程】Linux创建虚拟环境并配置与notebook相连接

1.创建 使用 venv 创建虚拟环境。例如,在当前目录下创建一个名为 myenv 的虚拟环境: python3 -m venv myenv 2.激活 激活虚拟环境使其成为当前终端会话的活动环境。运行: source myenv/bin/activate 3.与notebook连接 在虚拟环境中,使用 pip 安装 Jupyter 和 ipykernel: pip instal

内核启动时减少log的方式

内核引导选项 内核引导选项大体上可以分为两类:一类与设备无关、另一类与设备有关。与设备有关的引导选项多如牛毛,需要你自己阅读内核中的相应驱动程序源码以获取其能够接受的引导选项。比如,如果你想知道可以向 AHA1542 SCSI 驱动程序传递哪些引导选项,那么就查看 drivers/scsi/aha1542.c 文件,一般在前面 100 行注释里就可以找到所接受的引导选项说明。大多数选项是通过"_

poj 1258 Agri-Net(最小生成树模板代码)

感觉用这题来当模板更适合。 题意就是给你邻接矩阵求最小生成树啦。~ prim代码:效率很高。172k...0ms。 #include<stdio.h>#include<algorithm>using namespace std;const int MaxN = 101;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int n

【学习笔记】 陈强-机器学习-Python-Ch15 人工神经网络(1)sklearn

系列文章目录 监督学习:参数方法 【学习笔记】 陈强-机器学习-Python-Ch4 线性回归 【学习笔记】 陈强-机器学习-Python-Ch5 逻辑回归 【课后题练习】 陈强-机器学习-Python-Ch5 逻辑回归(SAheart.csv) 【学习笔记】 陈强-机器学习-Python-Ch6 多项逻辑回归 【学习笔记 及 课后题练习】 陈强-机器学习-Python-Ch7 判别分析 【学

系统架构师考试学习笔记第三篇——架构设计高级知识(20)通信系统架构设计理论与实践

本章知识考点:         第20课时主要学习通信系统架构设计的理论和工作中的实践。根据新版考试大纲,本课时知识点会涉及案例分析题(25分),而在历年考试中,案例题对该部分内容的考查并不多,虽在综合知识选择题目中经常考查,但分值也不高。本课时内容侧重于对知识点的记忆和理解,按照以往的出题规律,通信系统架构设计基础知识点多来源于教材内的基础网络设备、网络架构和教材外最新时事热点技术。本课时知识