计算机系统平台第4章.pptVIP

  • 10
  • 0
  • 约3.35万字
  • 约 204页
  • 2018-04-23 发布于河南
  • 举报
计算机系统平台第4章

第4章 操作系统的内部实现机制 主要内容 进程的管理 内存的管理 信息存储的管理 外设的管理 struct task_struct struct task_struct { struct files_struct* files; //文件描述符 struct signal_struct* sig; //信号控制signal handler struct mm_struct* mm;? ?? ???//内存管理模块 long state;? ?? ?? ?? ?? ?? ?? ?? ?? ?//进程状态 struct list_head runlist;? ?? ?? ?? ?? ?? ?? ?//用于链接RUN队列 long priority;? ?? ?? ?? ???//基本优先权 long counter;? ?? ?? ?? ?? ?//动态优先权 char comm[];? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //命令名 struct thread_struct tss;? ? //上下文保存领域 ... }; state有下面几种状态: 状态? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?说明 TASK_RUNNING? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? 执行可能状态 TASK_INTERRUPTIBLE? ?? ?? ?? ?? ?? ?? ?? ?等待状态。可接受信号 TASK_UNINTERRUPTIBLE? ?? ?? ?? ?? ?? ???等待状态。不能接受信号 TASK_ZOMBIE? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? 僵尸状态。exit后的状态 TASK_STOPPED? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???暂停状态 getpid getpid的作用很简单,就是返回当前进程的进程ID; 格式: #includesys/types.h /* 提供类型pid_t的定义 */ #includeunistd.h /* 提供函数的定义 */ pid_t getpid(void); 例:输出当前进程的PID /* getpid_test.c */ #includesys/types.h #includeunistd.h main() { printf(The current process ID is %d\n,getpid()); } $gcc getpid_test.c -o getpid_test $./getpid_test The current process ID is 1980 在2.4.4版内核中,fork是第2号系统调用,其在Linux函数库中的原型是: #include sys/types.h /* 提供类型pid_t的定义 */ #include unistd.h /* 提供函数的定义 */ pid_t fork(void); 例:fork系统调用创建进程,创建后父进程得到子进程的标识符,子进程得到值0。 #include stdio.h #include sys/types.h #include unistd.h int main() { int pid; printf(“Now this is process 1.\n”); printf(“System calling fork() will start.\n”); pid=fork(); if (pid==0) printf(“This is child process.\n”); else if (pid0) printf(“This is process 1(parent process).\n”); else printf(“fork failed.\n”); exit(0); } 说明 pid=0时的分支是子进程运行部分; pid0时的部分是原进程(即父进程)运行部分。 通常情况是: if(pid==0) call subprocess_program;/*调用子进程处理程序*/ else if (pid0) complete parent_process_program;/*完成父进程处理程序*/ 注意:父进程和子进程是并发执行。由于父、子进程具有相同的优先级,所以父、子进程运行的顺序是随机的。 例. fork 应用 int i=1;

文档评论(0)

1亿VIP精品文档

相关文档