第二部分, (void) open("/dev/tty0",O_RDWR,0);
参考 [github这个博主的 厉害][ https://github.com/sunym1993/flash-linux0.11-talk ]
它会触发一个 int 0x80
中断,会找的 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 ; fdfilp[fd])
break;
if (fd>=NR_OPEN)
return -EINVAL;
current->close_on_exec &= ~(1<f_count) break;
if (i>=NR_FILE)
return -EINVAL;
(current->filp[fd]=f)->f_count++;
if ((i=open_namei(filename,flag,mode,&inode))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))
if (MAJOR(inode->i_zone[0])==4) {
if (current->leader && current->ttytty = MINOR(inode->i_zone[0]);
tty_table[current->tty].pgrp = current->pgrp;
}
} else if (MAJOR(inode->i_zone[0])==5)
if (current->ttyfilp[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);
}
一部分一部分看吧
- 第一部分
#define NR_OPEN 20
struct task_struct {
.....
struct file * filp[NR_OPEN];
unsigned long close_on_exec;
......
struct tss_struct tss;
};
{
......
mode &= 0777 & ~current->umask;
for(fd=0 ; fdfilp[fd])
break;
.....
在寻找当前线程filp数组空闲的位置,current就是结构体 task_struct
的实例,空位置的索引为 fd
* 第二部分
#define NR_FILE 64
{
...
current->close_on_exec &= ~(1<f_count) break;
(current->filp[fd]=f)->f_count++;
...
}
寻找 file_table
的 f_count的空闲为,记录了f和i,把当前线程的找到的空闲位置 fd赋值为这个 f
* 第三部分
f->f_mode = inode->i_mode;
f->f_flags = flag;
f->f_count = 1;
f->f_inode = inode;
f->f_pos = 0;
return (fd);
把中间那部分省略了,这部分很简单了,就是在给f这个文件填充值了,相关信息的赋值
Original: https://www.cnblogs.com/shuisanya/p/16593933.html
Author: 水三丫
Title: linux-0.11分析:进程初始化函数init(),第二部分(void) open(“/dev/tty0”,O_RDWR,0),第十三篇随笔
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/581464/
转载文章受原作者版权保护。转载请注明原作者出处!