栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

【Linux学习笔记】09-进程控制

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【Linux学习笔记】09-进程控制


目录

二、为什么要写时拷贝?

三、进程终止

四、进程等待

为什么要等待?

进程等待方法

1、wait

2、waitpid


​​​​​一、进程创建方式

    命令行启动命令(程序、指令等)通过程序自身fork创建子进程

PCB:task_struct    进程地址空间:mm_struct


二、为什么要写时拷贝?


三、进程终止
int main() 
{
    cout << "hello world" << end;

    return 0;
}

其中return 0叫做返回进程退出码,通过进程退出码系统可以知道进程退出了且可以判断进程运行是否正确。可以通过echo $?在命令行中查看上一次进程运行的退出码:

[xupeng@VM-4-13-centos lesson12]$ cat test.c
#include "stdio.h"


int main() {
    printf("hello worldn");
    return 11;
}
[xupeng@VM-4-13-centos lesson12]$ ./test 
hello world
[xupeng@VM-4-13-centos lesson12]$ echo $?
11
[xupeng@VM-4-13-centos lesson12]$ 

通过strerror函数查看系统退出码列表:

  1 #include "stdio.h"
  2 #include "string.h"
  4 
  5 int main()
  6 {
  7     int i = 0;
  8     while (i < 100) {
  9          printf("%d : %sn", i, strerror(i));                                                                       
 10          i++;
 11     }
 12 
 13     return 0;
 19 }

四、进程等待

为什么要等待?

进程等待方法

1、wait

等待任意一个子进程,子进程退出时,wait就可以返回

NAME
       wait, waitpid, waitid - wait for process to change state

SYNOPSIS
       #include 
       #include 

       pid_t wait(int *status);

       pid_t waitpid(pid_t pid, int *status, int options);

       int waitid(idtype_t idtype, id_t id, siginfo_t *infop, int options);

进程等待demo:

  1 #include 
  2 #include 
  3 #include "stdio.h"
  4 #include "unistd.h"
  5 #include "stdlib.h"
  6 
  7 int main() {
  8     pid_t id = fork();
  9     if (id < 0) {
 10         perror("fork");
 11         return 1;
 12     }
 13     if (id == 0) {
 14         int count = 5;
 15         while (count) {
 16             printf("child(pid is : %d, ppid is : %d) is running: %dn", getpid(), getppid(), count--);
 17             sleep(1);
 18         }
 19         printf("child quit...n");
 20         exit(0);
 21     } else {
 22         printf("father is waiting...n");
 23         sleep(10);
 24         pid_t ret = wait(NULL);
 25         sleep(3);
 26         printf("father is wait done, ret : %dn", ret);
 27     }
 28     return 0;
 29 } 

以上代码可以看到僵尸进程状态(Z状态),然后被父进程回收,僵尸进程消失

while :; do echo "######"; ps ajx | grep proc | grep -v grep; echo "######"; sleep 1; done

2、waitpid
NAME
       wait, waitpid, waitid - wait for process to change state

SYNOPSIS
       #include 
       #include 

       pid_t wait(int *status);

       pid_t waitpid(pid_t pid, int *status, int options);

       int waitid(idtype_t idtype, id_t id, siginfo_t *infop, int options);

pid:pid=-1表示等待任何一个子进程与wait等同;pid>0表示等待进程id与PID相等的子进程 。

  1 #include 
  2 #include 
  3 #include "stdio.h"
  4 #include "unistd.h"
  5 #include "stdlib.h"
  6 
  7 int main() {
  8 
  9     pid_t id = fork();
 10     if (id == 0) {
 11         int count = 5;
 12         while (count) {
 13             printf("child is runnig : %d, ppid : %d, pid : %dn", count--, getppid(), getpid());                     
 14             sleep(1);
 15         }
 16         printf("child quit...n");
 17         exit(123);
 18     }
 19 
 20     int status = 0;
 21     pid_t ret = waitpid(id, &status, 0);
 22     printf("father wait done, ret : %d, exit code : %dn", ret, (status >> 8)&0xff);
 23     return 0;
}
五、进程替换

创建子进程的目的;
1、执行父进程的部分代码
2、执行其他程序的代码(进程的程序替换)

进程替换原理:

 

 前面是路径(要执行的程序在哪儿),后面是程序名(要执行的命令),再后面是可变参数列表(选项)也就是 -xxx -yyy,可变参数列表以NULL结尾:execl("/usr/bin/ls", "ls", "-l", "-a", NULL)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/733445.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号