热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Linux内核设计的艺术进程2的创建及执行

1、打开终端设备文件及复制文件句柄   代码路径:initmain.c 目前处于进程1的3特权级voidinit(void){intpid,i;setup((voi

1、打开标准输入设备
      代码路径:init/main.c  目前处于进程1的3特权级

void init(void)
{
int pid,i;
setup((void *) &drive_info);
(void) open("/dev/tty0",O_RDWR,0);
(void) dup(0);
(void) dup(0);
...
}      open同样调用int 0x80进入进程1的0特权级,sys_open如下:
     代码路径:fs/open.c

int sys_open(const char * filename,int flag,int mode)
{
struct m_inode * inode;
struct file * f;
int i,fd;
mode &= 0777 & ~current->umask;//暂时不考虑
for(fd=0 ; fd if (!current->filp[fd])
break;
if (fd>=NR_OPEN)
return -EINVAL;
current->close_on_exec &= ~(1< f=0+file_table;
for (i=0 ; i if (!f->f_count) break;
if (i>=NR_FILE)
return -EINVAL;
(current->filp[fd]=f)->f_count++;//将进程1的filp[20]与file_table[64]挂接,并增加引用计数,f_count为1
if ((i=open_namei(filename,flag,mode,&inode))<0) {
current->filp[fd]=NULL;
f->f_count=0;
return i;
}
...
}      代码路径:include/linux/fs.h

#define NR_OPEN 20
#define NR_FILE 64      代码路径:fs/namei.c

int open_namei(const char * pathname, int flag, int mode,
struct m_inode ** res_inode)
{
const char * basename;
int inr,dev,namelen;
struct m_inode * dir, *inode;
struct buffer_head * bh;
struct dir_entry * de;
if ((flag & O_TRUNC) && !(flag & O_ACCMODE))
flag |= O_WRONLY;
mode &= 0777 & ~current->umask;
mode |= I_REGULAR;//暂时不考虑
if (!(dir = dir_namei(pathname,&namelen,&basename)))//获取枝梢i节点,namelen为tty0的长度,basename指向tty0的第一个字母‘t‘
return -ENOENT;
if (!namelen) { /* special case: ‘/usr/‘ etc */
if (!(flag & (O_ACCMODE|O_CREAT|O_TRUNC))) {
*res_inode=dir;
return 0;
}
iput(dir);
return -EISDIR;
}
bh = find_entry(&dir,basename,namelen,&de);//此时根据dev的i节点和tty0来查找tty0的目录项
...
}      代码路径:fs/namei.c

static struct m_inode * dir_namei(const char * pathname,
int * namelen, const char ** name)
{
char c;
const char * basename;
struct m_inode * dir;
if (!(dir = get_dir(pathname)))
return NULL;
basename = pathname;
while ((c=get_fs_byte(pathname++)))
if (c==‘/‘)
basename=pathname;
*namelen = pathname-basename-1;//得到tty0名字的长度
*name = basename;//得到tty0中第一个‘t’字符的地址
return dir;
}       代码路径:fs/namei.c

static struct m_inode * get_dir(const char * pathname)
{
char c;
const char * thisname;
struct m_inode * inode;
struct buffer_head * bh;
int namelen,inr,idev;
struct dir_entry * de;
if (!current->root || !current->root->i_count)
panic("No root inode");
if (!current->pwd || !current->pwd->i_count)
panic("No cwd inode");
if ((c=get_fs_byte(pathname))==‘/‘) {
inode = current->root;//根i节点
pathname++;//指向d
} else if (c)
inode = current->pwd;
else
return NULL; /* empty name is bad */
inode->i_count++;//根i节点i_count为5
while (1) {
thisname = pathname;
if (!S_ISDIR(inode->i_mode) || !permission(inode,MAY_EXEC)) {
iput(inode);//不执行
return NULL;
}
for(namelen=0;(c=get_fs_byte(pathname++))&&(c!=‘/‘);namelen++)//如果遇到/或者字符串结尾就退出
/* nothing */ ;
if (!c)
return inode;//第二次循环返回空了,执行到此,返回dev的i节点
if (!(bh = find_entry(&inode,thisname,namelen,&de))) {//此时根据根i节点和dev来查找dev的目录项,此时thisname为dev,namelen为3
iput(inode);
return NULL;
}
inr = de->inode;//dev的i节点号
idev = inode->i_dev;//虚拟盘,0x101
brelse(bh);
iput(inode);//根i节点i_count为4
if (!(inode = iget(idev,inr)))//获取了dev的i节点(inode_table第二个结构体),i_count为1
return NULL;
}
}       代码路径:include/linux/fs.h

...
#define NAME_LEN 14
...
struct dir_entry {
unsigned short inode;
char name[NAME_LEN];
};      代码路径:include/linux/fs.h

...
struct m_inode {
unsigned short i_mode;
unsigned short i_uid;
unsigned long i_size;
unsigned long i_mtime;
unsigned char i_gid;
unsigned char i_nlinks;
unsigned short i_zone[9];
/* these are in memory also */
struct task_struct * i_wait;
unsigned long i_atime;
unsigned long i_ctime;
unsigned short i_dev;
unsigned short i_num;
unsigned short i_count;
unsigned char i_lock;
unsigned char i_dirt;
unsigned char i_pipe;
unsigned char i_mount;
unsigned char i_seek;
unsigned char i_update;
};
...


      程序执行到了open_namei,找到了tty0的目录项,接下来继续执行:

int open_namei(const char * pathname, int flag, int mode,
struct m_inode ** res_inode)
{
...
bh = find_entry(&dir,basename,namelen,&de);
...
inr = de->inode;//tty0的i节点号
dev = dir->i_dev;//0x101
brelse(bh);
iput(dir);//第二个inode_table的i_count为0
if (flag & O_EXCL)
return -EEXIST;//不执行
if (!(inode=iget(dev,inr)))//得到了tty0的i节点,(inode_table第二个结构体),i_count为1
return -EACCES;
if ((S_ISDIR(inode->i_mode) && (flag & O_ACCMODE)) ||
!permission(inode,ACC_MODE(flag))) {
iput(inode);//不执行
return -EPERM;
}
inode->i_atime = CURRENT_TIME;
if (flag & O_TRUNC)
truncate(inode);//不执行
*res_inode = inode;
return 0;
}

       返回sys_open继续执行:

...
int sys_open(const char * filename,int flag,int mode)
{
struct m_inode * inode;
struct file * f;
int i,fd;
mode &= 0777 & ~current->umask;
for(fd=0 ; fd if (!current->filp[fd])//找到进程第一个空闲的文件指针
break;
if (fd>=NR_OPEN)
return -EINVAL;
current->close_on_exec &= ~(1< f=0+file_table;
for (i=0 ; i if (!f->f_count) break;
if (i>=NR_FILE)
return -EINVAL;
(current->filp[fd]=f)->f_count++;//将进程1的filp[20]与file_table[64]挂接,并增加引用计数,f_count为1
if ((i=open_namei(filename,flag,mode,&inode))<0) {
current->filp[fd]=NULL;
f->f_count=0;
return i;
}
/* ttys are somewhat special (ttyxx major==4, tty major==5) */
if (S_ISCHR(inode->i_mode)) {//检查tty0文件的i节点属性,确定它是一个设备文件
if (MAJOR(inode->i_zone[0])==4) {
if (current->leader && current->tty<0) {
current->tty = MINOR(inode->i_zone[0]);
tty_table[current->tty].pgrp = current->pgrp;
}
} else if (MAJOR(inode->i_zone[0])==5)
if (current->tty<0) {
iput(inode);
current->filp[fd]=NULL;
f->f_count=0;
return -EPERM;
}
}
/* Likewise with block-devices: check for floppy_change */
if (S_ISBLK(inode->i_mode))//暂时不考虑
check_disk_change(inode->i_zone[0]);
f->f_mode = inode->i_mode;
f->f_flags = flag;
f->f_count = 1;
f->f_inode = inode;
f->f_pos = 0;
return (fd);//fd为0
}
...

          至此进程1的current->filp[0]存放的file_table第一个元素地址,file_table第一个元素,又存放着inode的地址,f_count为1



2、打开标准输出、标准错误输出设备

          又返回了进程1的3特权级,接着执行init()

          代码路径:init/main.c

void init(void)
{
int pid,i;
setup((void *) &drive_info);
(void) open("/dev/tty0",O_RDWR,0);
(void) dup(0);
(void) dup(0);
...
}         执行dup(0),又陷入了进程1的0特权级,开始执行sys_dup

         代码路径:fs/fcntl.c

static int dupfd(unsigned int fd, unsigned int arg)//fd为0,arg为0
{
if (fd >= NR_OPEN || !current->filp[fd])
return -EBADF;
if (arg >= NR_OPEN)
return -EINVAL;
while (arg if (current->filp[arg])
arg++;
else
break;//arg为1
if (arg >= NR_OPEN)
return -EMFILE;
current->close_on_exec &= ~(1< (current->filp[arg] = current->filp[fd])->f_count++;//0和1共同指向一个文件地址,并且f_count为2
return arg;
}           然后又返回进程1的3特权级,又一次执行dup(0),结果是current->filp[0],current->filp[1],current->filp[2]共同指向一个文件地址,并且f_count为3。

















Linux内核设计的艺术-进程2的创建及执行,布布扣,bubuko.com


推荐阅读
  • 实验六提交版
    1.21.3part2共用体与结构体类型的区别?答:共用体与结构体的区别在于它们的表示方法不同。结构体内,结构体的各成员顺序排列存储,每个成员都有自己独立的存储位置,而共用体的情况 ... [详细]
  • D-War(8.4.3)CrawlinginprocessCrawlingfailedTimeLimit:3000MS    MemoryLimit:0KB  ... [详细]
  • JS swiper轮播图完美兼容手机端
    swiper ... [详细]
  • 使用IGP和BGP的配合达到降低路由容量目的的实验与总结
    本文描述了OSPF和BGP配合来降低路由器的容量压力的实验和总结,有助于对IGP协议和BGP协议的互 ... [详细]
  • Python对象特性0x01:所有Python对象都有三个特性以及属性*身份:每一个对象都有一个唯一的身份标识自己,任何一个都可以用内建函数id()来得到。*类型:决定了可以保存什 ... [详细]
  • Forexamplewehavefollowingcode:$(el).hide()el.style.display'none'$(el).forEach((){ ... [详细]
  • RocketdecodeSimplifyDC
    https:mp.weixin.qq.coms4uWqBRrMVG6FlnBKmw8U-w介绍SimplifyDC如何简化解码逻辑。1.使用??简化从mint和maxt中查找的逻辑 ... [详细]
  • 获取鼠标的位置/坐标
    使用javascript如何获取鼠标的位置呢?获取光标的位置?获取鼠标坐标先看效果?核心方法:****返回鼠标的坐标*@parame*@returns{{x ... [详细]
  • 题目:写一个函数返回参数二进制中1的个数方法1:我自己写的,运用‘%‘和‘‘,感觉挺简单的。intcount_one_bit(intnum){unsignedintcount0;w ... [详细]
  • 【7】继承、super、this、抽象类
    1、继承定义:继承就是子类继承父类的属性和行为,使得子类对象具有与父类相同的属性、相同的行为。子类可以直接访问父类中的非私有的属性和行为。好处:1、提高代码的复用性。2、类与类之间 ... [详细]
  • 网络Cisco考试
    二、操作题(共80分)请将以下拓扑实验配置完毕,保存拓扑,建立一个文本文档,按照交换机-路由器1234的顺序,将每台设备的showrunning-config复制粘贴出来,将文本文 ... [详细]
  • 虚拟机需要关闭bcdeditsethypervisorlaunchtypeoffdocker需要开启bcdeditsethypervisorlauncht ... [详细]
  • SparkMLlib提供了一些基本的统计学的算法,下面主要说明一下:1、Summarystatistics对于RDD[Vector]类型,SparkMLlib提供了colStats ... [详细]
  • mysql在BTree上创建伪哈希索引
    构建哈希的过程select过程长字符串下,构建索引可通过自定义哈希作为索引,本人通过实验,在3百多个数据记录的下,性能效果很明显,完全不是一个等级.以下为索引前后几种情况对比在哈希 ... [详细]
  • df du命令 查看磁盘大小
    1.df命令查看文件系统使用情况。最常用的命令就是df-h其他选项:a:列出所有的文件系统,包括系统特有的/proc等系统文件 k:以KB的容量显示 m:以MB的容量显示文件系统  ... [详细]
author-avatar
你的拥吻像情歌一样凄美_207
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有