正文开始——
【Linux系统】第八节—进程概念(上)—冯诺依曼体系结构+操作系统+进程及进程状态+僵尸进程(结合画板一起看,理解效果更好)
我们常⻅的计算机,如笔记本。我们不常⻅的计算机,如服务器,⼤部分都遵守冯诺依曼体系。
截⾄⽬前,我们所认识的计算机,都是由⼀个个的硬件组件组成。
关于冯诺依曼,必须强调⼏点:
注意——对冯诺依曼的理解,不能停留在概念上,要深⼊到对软件数据流理解上,请解释:(可以看画图板上面的解释)
任何计算机系统都包含⼀个基本的程序集合,称为操作系统(OS)。
笼统的理解,操作系统包括:
在整个计算机软硬件架构中,操作系统的定位是:⼀款纯正的“搞管理”的软件。
总结 计算机管理硬件
承上启下
那在还没有学习进程之前,就问⼤家,操作系统是怎么管理进⾏进程管理的呢?很简单,先把进程描述起来,再把进程组织起来!
基本概念
进程信息被放在⼀个叫做进程控制块的数据结构中,可以理解为进程属性的集合。 • 课本上称之为PCB(process control block),Linux操作系统下的PCB是: task_struct。
task_struct—PCB的⼀种
内容分类
组织进程
可以在内核源代码⾥找到它。所有运⾏在系统⾥的进程都以task_struct链表的形式存在内核⾥。
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
printf("pid: %d\n", getpid());
printf("ppid: %d\n", getppid());
return 0;
}
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int ret = fork();
printf("hello proc : %d!, ret: %d\n", getpid(), ret);
sleep(1);
return 0;
}
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int ret = fork();
if(ret < 0){
perror("fork");
return 1;
}
else if(ret == 0)
{
//child
printf("I am child : %d!, ret: %d\n", getpid(), ret);
}else{
//father
printf("I am father : %d!, ret: %d\n", getpid(), ret);
}
sleep(1);
return 0;
}
注意
为了弄明⽩正在运⾏的进程是什么意思,我们需要知道进程的不同状态。⼀个进程可以有⼏个状态(在Linux内核⾥,进程有时候也叫做任务)。
进程状态其实就是task_struct结构体内的一个整型变量,在操作系统里面#define宏定义R,S等定义为不同的整型值0,1等, 根据进程的不同状态将task_struct里面的整型变量的值改为对应的状态,这就是状态,未来操作系统根据task_struct里面的整型值来选择如何控制进程(调度,挂起。。。)
下⾯的状态在kernel源代码⾥定义:
/*
*The task state array is a strange "bitmap" of
*reasons to sleep. Thus "running" is zero, and
*you can test for combinations of others with
*simple bit tests.
*/
static const char *const task_state_array[] = {
"R (running)", /*0 */
"S (sleeping)", /*1 */
"D (disk sleep)", /*2 */
"T (stopped)", /*4 */
"t (tracing stop)", /*8 */
"X (dead)", /*16 */
"Z (zombie)", /*32 */
};
ps aux / ps axj 命令
来⼀个创建维持30秒的僵死进程例⼦:
#include <stdio.h>
#include <stdlib.h>
int main()
{
pid_t id = fork();
if(id < 0){
perror("fork");
return 1;
}
else if(id > 0){ //parent
printf("parent[%d] is sleeping...\n", getpid());
sleep(30);
}else{
printf("child[%d] is begin Z...\n", getpid());
sleep(5);
exit(EXIT_SUCCESS);
}
return 0;
}
⾄此,值得关注的进程状态全部讲解完成,下一节来认识另⼀种进程。