0
点赞
收藏
分享

微信扫一扫

进程的概念与exec的用法

星巢文化 2022-03-12 阅读 70

一.进程概念

1.进程与程序的差异?
未编译的程序 ----------可执行程序
程序: 一堆待执行的程序 gcc hello.c -o hello (程序是一个静态数据) 剧本

进程: 只有程序被加载到CPU中占用资源时,根据代码每行做出反应,才能形成一个真正动态的进程
(进程是一个动态的过程) 拍戏的过程

2.可执行程序 project --> ./project --> 开启一个进程

以下的几个区域的数据会直接从程序中拷贝到内存中

.init初始化段
.data和.bss数据段
.rodata常量区

3.当程序执行时,除了会分配空间之外,系统还会为你分配一个结构体目录,读取一个目录项时就会返回一个结构体指针,这个结构体用于描述这个目录项 (描述:类型,结构体大小,索引号,偏移量,文件名)

    ./project   开启一个进程  
      -->  返回一个结构体  
      struct task_struct{};

在这里插入图片描述

这个结构体用于描述这个进程 (描述:进程ID号,信号,文件,资源…)这个结构体在哪里?

/usr/src/linux-headers-3.5.0-23/include/linux/sched.h

4. 关于进程的命令

1) 查看整个Linux系统的命令 --> pstree

2) 查看进程PID号 --> ps -ef (静态)
在这里插入图片描述

3)查看进程CPU占用率 --> top (动态)

CPU实时使用率在这里插入图片描述

5. 进程的状态

所谓CPU资源: 指的就是 struct task_struct{};

二. 进程函数接口

1.如何创建新的进程? --> fork() --> man 2 fork

NAME
fork - create a child process //在一个进程创建子进程,父子进程就会一起执行

====================================================
#include <stdio.h>
#include <unistd.h>

int main()
{
	//1.程序开始执行时,只有一个进程
	printf("hello!\n");
	
	//2.产生一个子进程
	fork();
	
	//3.父子进程同时执行这条代码,至于谁先谁后
	printf("world!\n");
	
	return 0;
}
========================================================

总结:
1)fork()之后,父子进程并发进行
2)父子进程是随机先后执行
3)PID号没有负数

2.查看自身的PID号/查看父进程的PID号

 getpid()         getppid()
//头文件
   #include <sys/types.h>
   #include <unistd.h>

//函数原型
   pid_t getpid(void);  --> 查看自身的ID号
   pid_t getppid(void); --> 查看当前进程的父进程ID号

返回值:
	成功:  相应的PID号
	失败:  These functions are always successful.

三. 创建前后,父子进程资源差异?

#include <stdio.h>
#include <unistd.h>

int main()
{
	int a = 100;
	
	pid_t x = fork();
	if(x > 0)
	{
		a = 50;
		printf("parent a = %d\n",a); //50
	}
	
	if(x == 0)
	{
		printf("child a = %d\n",a); //100
	}
	
	return 0;
}

四. 关于子进程中,资源回收问题。

为什么要进行资源回收? --> 释放CPU资源

2.解决僵尸问题:

两种情况:
1. 父进程需要持续一段时间,再进行回收,子进程很快就结束自己的任务退出了。子进程退出时,子进程会变成僵尸态,等待自己的父进程结束任务后,再帮子进程回收资源,子进程变成死亡态

#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>

int main()
{
	
	pid_t x = fork();
	if(x > 0)
	{
		int i;
		printf("hello,I am parent!\n");
		//2. 由于父进程还来不及监听,子进程在2到20秒这段时间内,一直都是僵尸态
		//3. 直到20秒时,父进程主动调用wait函数回收子进程资源
		for(i=0;i<20;i++)
		{
			printf("hello!\n");
			sleep(1);
		}
		
		int state;
		//wait(NULL); //父进程不关注子进程的退出状态
		wait(&state); //阻塞等待到子进程退出为止
		printf("state = %d\n",state);
	}
	
	if(x == 0)
	{
		sleep(2);
		printf("I am child!\n");
		//1.子进程马上退出,自身变成僵尸态
	}
	
	return 0;
}

2.父进程在子进程退出之前就已经在监听子进程的状态,回收子进程资源,让子进程变成死亡态。子进程退出后,变成僵尸态,马上就会被父进程回收,进入死亡态。

#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>

int main()
{
	pid_t x = fork();
	if(x > 0)
	{
		int state;
		printf("hello,I am parent!\n");
		//wait(NULL); //父进程不关注子进程的退出状态
		wait(&state); //阻塞等待到子进程退出为止
		printf("state = %d\n",state);
	}
	
	if(x == 0)
	{
		int i;
		for(i=0;i<5;i++)
		{
			printf("hello!\n");
			sleep(1);
		}
		exit(1);
	}
	
	return 0;
}
  1. 回收子进程资源的接口 – wait() / waitpid()
wait for process to change state

功能: 监听子进程的状态

#include <sys/types.h>
#include <sys/wait.h>

   pid_t wait(int *status);

waitpid()是针对wait()函数的封装 --> man 2 waitpid

#include <sys/types.h>
#include <sys/wait.h>

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

The call wait(&status) is equivalent to: waitpid(-1, &status, 0);

4.进程的退出

#include <stdlib.h>

void exit(int status);   ---> 清洗缓冲区后,再退出

#include <unistd.h>

void _exit(int status);   ---> 不清理缓冲区,直接退出

五. exec族

NAME
execl, execlp, execle, execv, execvp, execvpe - execute a file

SYNOPSIS

#include <unistd.h>
int execl(const char *path, const char *arg,...);
int execlp(const char *file, const char *arg,...);
int execle(const char *path, const char *arg,..., char * const envp[]);
int execv(const char *path, char *const argv[]);
int execvp(const char *file, char *const argv[]);
int execvpe(const char *file, char *const argv[],char *const envp[]);

只要进程被exec族替换掉,在exec函数之后的代码都不会执行在这里插入图片描述

举报

相关推荐

0 条评论