current position:Home>[Operating System] Process Creation and Destruction

[Operating System] Process Creation and Destruction

2022-08-06 18:29:1451CTO

进程

程序和进程

程序就是一堆指令和数据的集合,This collection is reflected in a static executable and associated configuration files, etc.

操作系统可以运行多个程序.实际上,CPUExecution is fast,And there are many programs to be run,Well in order for the operating system to run multiple programs,CPUIt will divide its execution time into many segments,Such as each paragraph is0.1秒,那么就可以这样A程序运行0.1秒,然后B程序运行0.1,然后C程序运行0.2秒,Because this switch is fast,So we feel that the programs are running at the same time.

从操作系统上来看,A running program is a process,Because there is a switch,So the process manages a lot of resources,比如:打开的文件、挂起的信号、进程状态、memory address space, etc,That is, the process is involvedCPU的调度,All resources are managed.

更详细的来说,现代计算机的CPU执行速度非常快,When using a process to participate in scheduling,frequently fromCPUIt is time-consuming to save the running state and corresponding information of the registers and process stack,所以现代CPUSomething that treats a process as just a resource,And introduce the thread asCPU调用的基本单位,Multiple threads can share all the resources of the same process.

要注意的是,程序并不是进程,实际上两个或多个进程不仅有可能执行同一程序,而且还有可能共享地址空间等资源.

Linux内核通过一个被称为进程描述符的task_struct结构体来管理进程,这个结构体包含了一个进程所需的所有信息.它定义在/include/linux/sched.h中.


创建进程

fork()

      
      
#include <unistd.h>
#include <stdio.h>
int main()
{
pid_t fpid;//fpid接收fork函数返回的值
int count=0;
fpid=fork();
if (fpid < 0)
printf("error in fork!");
else if (fpid == 0) {
printf("i am the child process, my process id is %d\n",getpid());
printf("I’m children\n");
count +=2;
}
else {
printf("i am the parent process, my process id is %d/n",getpid());
printf("I’m parent.\n");
count++;
}
printf("统计结果是: %d\n",count);
return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.

【操作系统】进程的创建与销毁_子进程

说明:

调用fork后,会创建一个子进程,父子进程都从fork处开始执行,fork函数有两个返回值,父进程会返回子进程的pid>0,子进程pid=0.

传统的linux操作系统以统一的方式对待所有的进程,子进程复制父进程所拥有的所有资源,This approach makes the creation process very slow,因为子进程需要拷贝父进程的所有的地址空间.

现代的操作系统,The following three methods are mainly used for processing:

  • 写时复制(需要不同的值).
  • 轻量级进程允许父子进程共享每进程在内核的很多数据结构,比如地址空间、打开文件表和信号处理.
  • vfork系统调用创建的进程能共享其父进程的内存地址空间,为了防止父进程重写子进程需要的数据,阻塞父进程的执行,一直到子进程退出为止.

销毁进程

exit()

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

int main()
{
pid_t fpid;//fpid接收fork函数返回的值
int count=0;
int status = 0;

fpid=fork();
if (fpid < 0)
printf("error in fork!\n");
else if (fpid == 0) {
printf("i am the child process, my process id is %d\n",getpid());
printf("I’m children\n");
count +=2;
exit(-10);
}
else {
printf("i am the parent process, my process id is %d\n",getpid());
printf("I’m parent.\n");
count++;
}
printf("统计结果是: %d\n",count);
//父进程捕捉子进程的状态
wait(&status);
printf("parent: status: %d\n", WEXITSTATUS(status));
return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.

【操作系统】进程的创建与销毁_子进程_02

copyright notice
author[51CTO],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/218/202208061818215830.html

Random recommended