Linux:进程(一)


前言

  前面我们学习了gdb的一些基础命令,今天来看看进程这个概念。


一、进程是什么

  进程 = 对应的代码和数据 + 进程对应的 PCB 结构体
  那么问题来了,PCB是什么?
  难道是高中生物?错辣!那是PCR
  PCB全称Process Control Block,即 进程管理块

二、描述进程–PCB

  进程信息被放在一个叫做进程控制块的数据结构中,可以理解为进程属性的集合。
  Linux操作系统下的PCB是: task_struct
  task_struct是Linux内核的一种数据结构,它会被装载到RAM(内存)里并且包含着进程的信息。

三、查看进程

  进程的信息可以通过 /proc 系统文件夹查看

[wkj@VM-4-13-centos lesson8]$ ls /proc/1
ls: cannot read symbolic link /proc/1/cwd: Permission denied
ls: cannot read symbolic link /proc/1/root: Permission denied
ls: cannot read symbolic link /proc/1/exe: Permission denied
attr       cgroup      comm             cwd      fd       io        map_files  mountinfo   net        oom_adj        pagemap      projid_map  schedstat  smaps  statm    task     wchan
autogroup  clear_refs  coredump_filter  environ  fdinfo   limits    maps       mounts      ns         oom_score      patch_state  root        sessionid  stack  status   timers
auxv       cmdline     cpuset           exe      gid_map  loginuid  mem        mountstats  numa_maps  oom_score_adj  personality  sched       setgroups  stat   syscall  uid_map
[wkj@VM-4-13-centos lesson8]$ ls /proc
1      1199   14040  20     24     259    27737  294  414  6411  7     925        cmdline    execdomains  kallsyms    locks    pagetypeinfo  stat           uptime
10     12     147    203    24085  26     27884  36   47   646   7301  990        consoles   fb           kcore       mdstat   partitions    swaps          version
10753  1217   16     21     25     260    28     37   49   65    8     991        cpuinfo    filesystems  keys        meminfo  sched_debug   sys            vmallocinfo
10968  13     1621   22     25007  26695  28826  38   50   650   8234  993        crypto     fs           key-users   misc     schedstat     sysrq-trigger  vmstat
11     13814  18     22268  25089  27     28850  389  51   654   8236  acpi       devices    interrupts   kmsg        modules  scsi          sysvipc        xpmem
1162   1389   19     22269  251    27052  289    39   52   657   8349  buddyinfo  diskstats  iomem        kpagecount  mounts   self          timer_list     zoneinfo
1166   1390   19897  22270  257    271    29     4    557  658   8350  bus        dma        ioports      kpageflags  mtrr     slabinfo      timer_stats
1175   14     2      23     258    277    293    410  6    659   9     cgroups    driver     irq          loadavg     net      softirqs      tty

四、通过系统调用获取进程标示符

  1. 进程id :pid
  2. 父进程id : ppid
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
printf("pid: %d\n", getpid());
printf("ppid: %d\n", getppid());
return 0;
}

五、通过系统调用创建进程-fork初识

  1. fork有俩个返回值
  2. 父子进程代码共享,数据各自开辟空间,私有一份(采用写时拷贝)
#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;
}

六、进程状态

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 */
};
  1. R运行状态(running) : 并不意味着进程一定在运行中,它表明进程要么是在运行中要么在运行队列里。
  2. S睡眠状态(sleeping): 意味着进程在等待事件完成(这里的睡眠有时候也叫做可中断睡眠。
  3. D磁盘休眠状态(Disk sleep)有时候也叫不可中断睡眠状(uninterruptible sleep),在这个状态的进程通常会等待IO的结束。
  4. T停止状态(stopped): 可以通过发送 SIGSTOP 信号给进程来停止(T)进程。这个被暂停的进程可以通过发送 SIGCONT 信号让进程继续运行。
  5. X死亡状态(dead):这个状态只是一个返回状态,你不会在任务列表里看到这个状态。
    在这里插入图片描述
      当服务器压力过大的时候,操作系统(OS)会通过一定的手段,杀掉一些进程,以求节省空间的作用。

七、进程状态查看

  ps aux / ps axj 命令

八、僵尸进程(Z:zombie)

1.是什么

  一个进程已经退出,但是还不允许被操作系统(OS)释放,处于一个被检测的状态,这个状态就是僵尸状态。

2.为什么

  维持该状态,为了让父进程和OS来进行回收。
  我是一个进程,如果我退出了,没有人来读我的数据,我就是僵尸!
在这里插入图片描述

3.怎么避免

  1. 让僵尸进程的父进程来回收,父进程每隔一段时间来查询子进程是否结束并回收,调用wait()或者waitpid(),通知内核释放僵尸进程 。
  2. 采用信号SIGCHLD通知处理,并在信号处理程序中调用wait函数 。
  3. 让僵尸进程变成孤儿进程,就是让他的父亲先死。
  4. 如果父进程很忙,那么可以用signal函数为SIGCHLD安装handler,因为子进程结束后, 父进程会收到该信号,可以在handler中调用wait回收。

九、孤儿进程

  父进程退出,子进程还在,子进程就叫做孤儿进程。孤儿进程会被领养,被1号进程领养(init,系统本身),这样就可以进行回收。

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
	pid_t id = fork();
	if(id < 0){
	perror("fork");
	return 1;
}
else if(id == 0){//child
	printf("I am child, pid : %d\n", getpid());
	sleep(10);
}else{//parent
	printf("I am parent, pid: %d\n", getpid());
	sleep(3);
	exit(0);
}
	return 0;
}

十、进程优先级

  1. cpu资源分配的先后顺序,就是指进程的优先权(priority)。
  2. 优先权高的进程有优先执行权利。配置进程优先权对多任务环境的linux很有用,可以改善系统性能。
  3. 可以把进程运行到指定的CPU上,这样一来,把不重要的进程安排到某个CPU,可以大大改善系统整体性能。

十一、其他概念

  1. 竞争性: 系统进程数目众多,而CPU资源只有少量,甚至1个,所以进程之间是具有竞争属性的。为了高效完成任务,更合理竞争相关资源,便具有了优先级
  2. 独立性: 多进程运行,需要独享各种资源,多进程运行期间互不干扰
  3. 并行:多个进程在多个CPU下分别,同时进行运行,这称之为并行
  4. 并发: 多个进程在一个CPU下采用进程切换的方式,在一段时间之内,让多个进程都得以推进,称之为并发

总结

以上就是今天要讲的内容啦

  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

二球悬铃木丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值