1.熟悉linux进程控制
2.通过跟踪进程的运行轨迹,熟悉linux进程调度
linux 0.11内核,最多可有64个进程同时存在。第一个进程是“手工”创建的,其余的都是现有进程使用系统调用fork创建的新进程。内核程序使用Pid号来标示每个进程。进程由可执行的指令代码、数据和堆栈区组成。内核通过调度程序分时调度各个进程的执行。linux系统中一个进程可以在内核态(kernel mode)或用户态(user mode)下执行,并且分别使用各自内核态和用户态堆栈。内核程序通过进程表对进程进行管理,每个进程在进程表中占一项。在linux系统中,进程表项是一个task_struct 任务结构指针,也称为进程控制块PCB。其中保存着用于控制和管理进程的所有信息。主要包括进程的Pid号,父进程号,进程状态,运行时间累计值,正在使用的文件等。
linux进程信息保存在PCB的state字段中,总的来说一个在其生命周期中会经理新建(N),就绪(J),运行(R),进入阻塞(W),退出(E)。这里可以提僵死状态,指的是当进程停止运行,当其父进程还没有调用wait()询问其状态时,则该进程进程处于僵死状态。unix中一个进程在调用exit命令结束自己生命的时候,其实没有真正的被销毁,内核会释放该进程的所有资源,包括打开的文件,占用的内存。但仍然为其保留一定的信息(包括进程号,进程退出状态,运行时间等)。直到父进程通过wait/waitpid来取时才自动释放。在这过程中,进程处于僵死状态。
linux中创建新进程使用fork()系统调用。所有进程都是复制进程0而得到的,都是进程0的子进程。在创建新进程的过程中,系统首先在任务数组中找出一个还没有被任何进程使用的空项(空槽)。如果系统的64个进程在运行,则fork()系统调用会因为没有任务数组表中没有空项而出错返回。然后系统为新建的进程在主内存区中申请一页内存来存放其任务数据结构信息,并复制当前任务数据结构中的所有内容作为新进程任务数据结构的模板。随后对复制的人数数据结构进行修改。
另外,创建一个新的子进程和加载运行一个可执行程序文件是两个概念。当创建子进程时,它完全复制了父进程的代码和数据区,并会在其中执行子进程部分的代码。而执行块设备上的一个程序时,一般是在子进程运行exec()系统调用来操作的。
1.编写样本程序。生成几个进程的程序。可以先在ubuntu上测试成功后,再在修改后的linux 0.11上面运行。
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <time.h> #include <sys/times.h> #include <sys/wait.h> #include <sys/types.h> #include <errno.h> #define HZ 100 /* * 此函数按照参数占用CPU和I/O时间 * last: 函数实际占用CPU和I/O的总时间,不含在就绪队列中的时间,>=0是必须的 * cpu_time: 一次连续占用CPU的时间,>=0是必须的 * io_time: 一次I/O消耗的时间,>=0是必须的 * 如果last > cpu_time + io_time,则往复多次占用CPU和I/O * 所有时间的单位为秒 */ void cpuio_bound(int last, int cpu_time, int io_time) { struct tms start_time, current_time; clock_t utime, stime; int sleep_time; while (last > 0) { /* CPU Burst */ times(&start_time); /* 其实只有t.tms_utime才是真正的CPU时间。但我们是在模拟一个 * 只在用户状态运行的CPU大户,就像“for(;;);”。所以把t.tms_stime * 加上很合理。*/ do { times(¤t_time); utime = current_time.tms_utime - start_time.tms_utime; stime = current_time.tms_stime - start_time.tms_stime; } while ( ( (utime + stime) / HZ ) < cpu_time ); last -= cpu_time; if (last <= 0 ) break; /* IO Burst */ /* 用sleep(1)模拟1秒钟的I/O操作 */ sleep_time=0; while (sleep_time < io_time) { sleep(1); sleep_time++; } last -= sleep_time; } } void main() { pid_t c_p1; pid_t c_p2; pid_t c_p3; pid_t c_p4; if((c_p1 = fork())==0 ) { printf( "in child1 \n");cpuio_bound( 5 , 2 , 2); } else if((c_p2 = fork())==0) { printf( "in child2 \n");cpuio_bound( 5 , 4 , 0); } else if((c_p3 = fork())==0) { printf( "in child3 \n");cpuio_bound( 5,0 , 4); } else if((c_p4 = fork())==0) { printf( "in child4 \n");cpuio_bound( 4 , 2 , 2); } else if(c_p1==-1||c_p2==-1||c_p3==-1||c_p4==-1) { perror("fork"); exit(1); } else { printf("====================This is parent process====================\n"); printf("My pid is %d\n",getpid()); printf("The pid of child1 is %d\n",c_p1); printf("The pid of child2 is %d\n",c_p2); printf("The pid of child3 is %d\n",c_p3); printf("The pid of child4 is %d\n",c_p4); } wait(NULL); }
2.打开/var/process.log,修改init/main.c中的main()
move_to_user_mode(); /***************添加开始***************/ setup((void *) &drive_info); (void) open("/dev/tty0",O_RDWR,0); //建立文件描述符0和/dev/tty0的关联 (void) dup(0); //文件描述符1也和/dev/tty0关联 (void) dup(0); //文件描述符2也和/dev/tty0关联 (void) open("/var/process.log",O_CREAT|O_TRUNC|O_WRONLY,0666); /***************添加结束***************/ if (!fork()) { /* we count on this going ok */ init(); } ……
3.写log文件,修改kernel/printk.c,增加fprintk()
#include <linux/sched.h> #include <sys/stat.h> static char logbuf[1024]; int fprintk(int fd, const char *fmt, ...) { va_list args; int count; struct file * file; struct m_inode * inode; va_start(args, fmt); count=vsprintf(logbuf, fmt, args); va_end(args); if (fd < 3) /* 如果输出到stdout或stderr,直接调用sys_write即可 */ { __asm__("push %%fs\n\t" "push %%ds\n\t" "pop %%fs\n\t" "pushl %0\n\t" "pushl $logbuf\n\t" /* 注意对于Windows环境来说,是_logbuf,下同 */ "pushl %1\n\t" "call sys_write\n\t" /* 注意对于Windows环境来说,是_sys_write,下同 */ "addl $8,%%esp\n\t" "popl %0\n\t" "pop %%fs" ::"r" (count),"r" (fd):"ax","cx","dx"); } else /* 假定>=3的描述符都与文件关联。事实上,还存在很多其它情况,这里并没有考虑。*/ { if (!(file=task[0]->filp[fd])) /* 从进程0的文件描述符表中得到文件句柄 */ return 0; inode=file->f_inode; __asm__("push %%fs\n\t" "push %%ds\n\t" "pop %%fs\n\t" "pushl %0\n\t" "pushl $logbuf\n\t" "pushl %1\n\t" "pushl %2\n\t" "call file_write\n\t" "addl $12,%%esp\n\t" "popl %0\n\t" "pop %%fs" ::"r" (count),"r" (file),"r" (inode):"ax","cx","dx"); } return count; }
4.寻找状态切换点,跟踪进程运行轨迹
修改 fork.c
#include <errno.h> #include <linux/sched.h> #include <linux/kernel.h> #include <asm/segment.h> #include <asm/system.h> extern void write_verify(unsigned long address); long last_pid=0; void verify_area(void * addr,int size) { ... } int copy_mem(int nr,struct task_struct * p) { ... } int copy_process(int nr,long ebp,long edi,long esi,long gs,long none, long ebx,long ecx,long edx, long fs,long es,long ds, long eip,long cs,long eflags,long esp,long ss) { p->start_time = jiffies; /*在上面一行初始化了进程的开始时间所以赶快输出一条进程创建的Log*/ fprintk(3,"%ld\t%c\t%ld\n",last_pid,‘N‘,jiffies); ... p->state = TASK_RUNNING; /* do this last, just in case */ /*在上面一行改变了进程的状态这里输出一个进入就绪队列的Log*/ /*进程中Running表示的是可以运行,并不是正在运行*/ fprintk(3,"%ld\t%c\t%ld\n",last_pid,‘J‘,jiffies); return last_pid; } int find_empty_process(void) { ... }
修改exit.c
#include <errno.h> #include <signal.h> #include <sys/wait.h> #include <linux/sched.h> #include <linux/kernel.h> #include <linux/tty.h> #include <asm/segment.h> int sys_pause(void); int sys_close(int fd); void release(struct task_struct * p) { ... } static inline int send_sig(long sig,struct task_struct * p,int priv) { ... } static void kill_session(void) { ... } int sys_kill(int pid,int sig) { ... } static void tell_father(int pid) { ... } int do_exit(long code) { ... } int sys_exit(int error_code) { ... } int sys_waitpid(pid_t pid,unsigned long * stat_addr, int options) { int flag, code; struct task_struct ** p; verify_area(stat_addr,4); repeat: flag=0; for(p = &LAST_TASK ; p > &FIRST_TASK ; --p) { if (!*p || *p == current) continue; if ((*p)->father != current->pid) continue; if (pid>0) { if ((*p)->pid != pid) continue; } else if (!pid) { if ((*p)->pgrp != current->pgrp) continue; } else if (pid != -1) { if ((*p)->pgrp != -pid) continue; } switch ((*p)->state) { case TASK_STOPPED: if (!(options & WUNTRACED)) continue; put_fs_long(0x7f,stat_addr); return (*p)->pid; case TASK_ZOMBIE: current->cutime += (*p)->utime; current->cstime += (*p)->stime; flag = (*p)->pid; code = (*p)->exit_code; /*输出一条进程退出的Log*/ /*TASK_STOPED状态只是将当前进程转入睡眠状态,收到SIG_CONT信号时会被唤醒*/ /*TASK_ZOMBIE状态则是当前进程被KILL,并发送信号给父进程*/ fprintk(3,"%ld\t%c\t%ld\n",flag,‘E‘,jiffies); release(*p); put_fs_long(code,stat_addr); return flag; default: flag=1; continue; } } if (flag) { if (options & WNOHANG) return 0; current->state=TASK_INTERRUPTIBLE; /*输出一条等待的Log*/ /*这里要注意一下输出wait的时候要判断一下 pid 是不是等于0 如果等于0 就不输出Log*/ /*0号进程是守护进程,cpu空闲的时候一直在waiting,输出它的话是不会通过脚本检查的哦*/ if (current->pid!=0) fprintk(3,"%ld\t%c\t%ld\n",current->pid,‘W‘,jiffies); schedule(); if (!(current->signal &= ~(1<<(SIGCHLD-1)))) goto repeat; else return -EINTR; } return -ECHILD; }
sched.c的修改
#include <linux/sched.h> #include <linux/kernel.h> #include <linux/sys.h> #include <linux/fdreg.h> #include <asm/system.h> #include <asm/io.h> #include <asm/segment.h> #include <signal.h> #define _S(nr) (1<<((nr)-1)) #define _BLOCKABLE (~(_S(SIGKILL) | _S(SIGSTOP))) void show_task(int nr,struct task_struct * p) { ... } void show_stat(void) { ... } #define LATCH (1193180/HZ) extern void mem_use(void); extern int timer_interrupt(void); extern int system_call(void); union task_union { struct task_struct task; char stack[PAGE_SIZE]; }; static union task_union init_task = {INIT_TASK,}; long volatile jiffies=0; long startup_time=0; struct task_struct *current = &(init_task.task); struct task_struct *last_task_used_math = NULL; struct task_struct * task[NR_TASKS] = {&(init_task.task), }; long user_stack [ PAGE_SIZE>>2 ] ; struct { long * a; short b; } stack_start = { & user_stack [PAGE_SIZE>>2] , 0x10 }; void math_state_restore() { ... } void schedule(void) { int i,next,c; struct task_struct ** p; /* check alarm, wake up any interruptible tasks that have got a signal */ for(p = &LAST_TASK ; p > &FIRST_TASK ; --p) if (*p) { if ((*p)->alarm && (*p)->alarm < jiffies) { (*p)->signal |= (1<<(SIGALRM-1)); (*p)->alarm = 0; } if (((*p)->signal & ~(_BLOCKABLE & (*p)->blocked)) && (*p)->state==TASK_INTERRUPTIBLE) { (*p)->state=TASK_RUNNING; /*输出就绪的Log*/ fprintk(3,"%ld\t%c\t%ld\n",(*p)->pid,‘J‘,jiffies); } } /* this is the scheduler proper: */ while (1) { c = -1; next = 0; i = NR_TASKS; p = &task[NR_TASKS]; while (--i) { if (!*--p) continue; if ((*p)->state == TASK_RUNNING && (*p)->counter > c) c = (*p)->counter, next = i; } if (c) break; for(p = &LAST_TASK ; p > &FIRST_TASK ; --p) if (*p) (*p)->counter = ((*p)->counter >> 1) + (*p)->priority; } if(current->state == TASK_RUNNING && current != task[next]) /*输出就绪的Log*/ fprintk(3,"%ld\t%c\t%ld\n",current->pid,‘J‘,jiffies); if(current != task[next]) /*输出可运行的Log*/ fprintk(3,"%ld\t%c\t%ld\n",task[next]->pid,‘R‘,jiffies); switch_to(next); } int sys_pause(void) { current->state = TASK_INTERRUPTIBLE; /*检查并输出等待的Log*/ if (current->pid != 0) fprintk(3,"%ld\t%c\t%ld\n",current->pid,‘W‘,jiffies); schedule(); return 0; } void sleep_on(struct task_struct **p) { struct task_struct *tmp; if (!p) return; if (current == &(init_task.task)) panic("task[0] trying to sleep"); tmp = *p; *p = current; current->state = TASK_UNINTERRUPTIBLE; /*检查并输出等待的Log*/ if (current->pid != 0) fprintk(3,"%ld\t%c\t%ld\n",current->pid,‘W‘,jiffies); schedule(); *p = tmp; if (tmp) { tmp->state=TASK_RUNNING; /*输出就绪的Log*/ fprintk(3,"%ld\t%c\t%ld\n",tmp->pid,‘J‘,jiffies); } } void interruptible_sleep_on(struct task_struct **p) { struct task_struct *tmp; if (!p) return; if (current == &(init_task.task)) panic("task[0] trying to sleep"); tmp=*p; *p=current; repeat: current->state = TASK_INTERRUPTIBLE; /*检查并输出等待的Log*/ if (current->pid != 0) fprintk(3,"%ld\t%c\t%ld\n",current->pid,‘W‘,jiffies); schedule(); if (*p && *p != current) { (**p).state=TASK_RUNNING; /*输出就绪的Log*/ fprintk(3,"%ld\t%c\t%ld\n",(**p).pid,‘J‘,jiffies); goto repeat; } *p=tmp; if (tmp) { tmp->state=TASK_RUNNING; /*输出就绪的Log*/ fprintk(3,"%ld\t%c\t%ld\n",tmp->pid,‘J‘,jiffies); } } void wake_up(struct task_struct **p) { if (p && *p) { if((**p).state != TASK_RUNNING){ /*输出就绪的Log*/ fprintk(3,"%ld\t%c\t%ld\n",(**p).pid,‘J‘,jiffies); (**p).state=TASK_RUNNING; } } } ...
原文:http://www.cnblogs.com/wxquare/p/5140748.html