进程(process)

1. 进程

进程的描述

a.进程是一个正在运行的程序

b.每个进程都有一个进程控制块,PCB

c.进程进程控制块是用一个结构体struct task_struct 来实现

一个进程,包括代码、数据和分配给进程的资源。fork()函数通过系统调用创建一个与原来进程几乎完全相同的进程,也就是两个进程可以做完全相同的事,但如果初始参数或者传入的变量不同,两个进程也可以做不同的事。

    一个进程调用fork()函数后,系统先给新的进程分配资源,例如存储数据和代码的空间。然后把原来的进程的所有值都复制到新的新进程中,只有少数值与原来的进程的值不同。相当于克隆了一个自己。

fork 创建进程,返回值为0是子进程,返回值大于0为父进程(返回值的数值是子进程id),返回值为负数表示fork失败。

用fork创建进程的例子如下:

[root@localhost linux]# vim fork_process.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>

int main ()
{
        char * process_name = NULL;
        int n, i;
        pid_t id = fork();
        if (id == 0)
        {
                process_name = "child ";
                n = 2;
        }
        else
        {
                process_name = "parent ";
                n = 5;
        }

        for ( i = 0; i < n; i++)
        {
                printf("process=%s pid=%d ppid=%d n=%d \n", process_name,getpid(),getppid(),n);
                sleep(2);
        }
}

编译:gcc -o fork_process fork_process.c

运行结果:

[root@localhost linux]# ./fork_process
process=parent  pid=25934 ppid=1312 n=5
process=child  pid=25935 ppid=25934 n=2
process=parent  pid=25934 ppid=1312 n=5
process=child  pid=25935 ppid=25934 n=2
process=parent  pid=25934 ppid=1312 n=5
process=parent  pid=25934 ppid=1312 n=5
process=parent  pid=25934 ppid=1312 n=5

如上结果可知:

当fork 返回值为0时,执行子进程,n 的值为2输出两次,执行父进程时n的值为5执行5次。

从pid和ppid 可以看出父进程的id和子进程的id

僵尸进程的产生:

子进程先于父进程结束,父进程没有获取子进程的退出码,此时子进程变成僵尸进程。

————————

1. Process

Description of the process

a. A process is a running program

b. Each process has a process control block, PCB

c. The process control block uses a struct task_ Struct

A process, including code, data, and resources allocated to the process. The fork () function creates a process almost identical to the original process through system call, that is, the two processes can do exactly the same thing, but if the initial parameters or passed in variables are different, the two processes can also do different things.

After a process calls the fork () function, the system first allocates resources to the new process, such as space for storing data and code. Then all the values of the original process are copied to the new process, and only a few values are different from those of the original process. It’s like cloning yourself.

Fork creates a process. If the return value is 0, it is the child process. If the return value is greater than 0, it is the parent process (the value of the return value is the child process ID). If the return value is negative, it indicates that fork failed.

An example of creating a process with fork is as follows:

[root@localhost linux]# vim fork_process.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>

int main ()
{
        char * process_name = NULL;
        int n, i;
        pid_t id = fork();
        if (id == 0)
        {
                process_name = "child ";
                n = 2;
        }
        else
        {
                process_name = "parent ";
                n = 5;
        }

        for ( i = 0; i < n; i++)
        {
                printf("process=%s pid=%d ppid=%d n=%d \n", process_name,getpid(),getppid(),n);
                sleep(2);
        }
}

编译:gcc -o fork_process fork_process.c

Operation results:

[root@localhost linux]# ./fork_process
process=parent  pid=25934 ppid=1312 n=5
process=child  pid=25935 ppid=25934 n=2
process=parent  pid=25934 ppid=1312 n=5
process=child  pid=25935 ppid=25934 n=2
process=parent  pid=25934 ppid=1312 n=5
process=parent  pid=25934 ppid=1312 n=5
process=parent  pid=25934 ppid=1312 n=5

According to the above results:

When the return value of fork is 0, the child process is executed. The value of n is 2 and output twice. When the parent process is executed, the value of n is 5 and executed five times.

From PID and PPID, you can see the ID of the parent process and the ID of the child process

Generation of zombie process:

The child process ends before the parent process. The parent process does not get the exit code of the child process. At this time, the child process becomes a zombie process.