Linux system calls: create and terminate processes

1. Three states of the process

1. Run . Either it is being executed by the CPU, or it is waiting to be executed and will eventually be scheduled by the kernel.

2. Stop . Execution is suspended and will not be scheduled. Only after receiving a specific signal can it continue to run.

3. Termination . The process stopped forever. There are three possible causes: (1) receiving a signal to terminate the process, (2) returning from the main program, (3) calling the exit function

2. Terminate the process

#include<stdlib.h>
void exit(int status);//这个大家都很熟悉

3. Create process

The parent process creates a new running child process through the fork function: (fork in English means fork and fork, which means that a child process should be separated from a process), the newly created child process will get the parent process A copy of almost all information, the biggest difference between the two is that they have different PIDs.

#include<sys/types.h>
#inlcude<unistd.h>

pid_t fork(void);
//子进程返回值为0,父进程返回子进程的pid,如果出错,则返回-1

For the case of an error in the system call, in CSAPP , a solution is mentioned, using a wrapped function with error handling with the same name and the same name as the original function instead of the original function.

For example, for the fork function, use a wrapper function called Fork:

pid_t Fork(void)
{
    pit_t pid;
    if((pit = fork())<0)//系统调用出错
        unix_error("Fork error");
    return pid;
}//可见Fork函数的参数类型、返回类型均与fork相同,故调用方式也是一样的

//unix_error的定义
void unix_error(char *msg)
{
    fprintf(stderr,"%s: %s\n",msg,stderror(errno));//errno是一个系统级的全局变量,需包含<errno.h>
    //stderror函数需包含头文件<string.h>,作用是根据errno值返回特定的描述错误的文本串
}

The following is the specific application of fork:

Divided into three files, alluse.h (header file), alluse.c (definition of wrapper function), fork.c (use)

//alluse.h
#ifndef ALLUSE_H
#define ALLUSE_H
#include<sys/types.h>
#include<unistd.h>
void unix_error(char *msg);
pid_t Fork(void);
#endif
//alluse.c
#include"alluse.h"
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<errno.h>

void unix_error(char *msg)
{
    fprintf(stderr,"%s: %s\n",msg,strerror(errno));
    exit(0);
}

pid_t Fork(void)
{
    pid_t pid;
    if((pid = fork())<0)
        unix_error("Fork error");
    return pid;
}
//fork.c
#include"alluse.h"
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>

int main()
{
    pid_t pid;
    int x = 1;
    pid = Fork();
    printf("return value of function Fork() = %d\n",pid);
    if(pid==0){
        printf("child : x=%d\n",++x);
        printf("pid: %d\n",getpid());
        printf("ppid: %d\n",getppid());
        exit(0);
    }
    printf("parent: x=%d\n",--x);
    printf("pid: %d\n",getpid());
    printf("ppid: %d\n",getppid());
    exit(0);
}

Compile and run:

linux> gcc -o forktry fork.c alluse.c
linux> ./forktry
#结果如下
linux> ./forktry 
return value of function Fork() = 9578
parent: x=0
pid: 9577
ppid: 24176
linux> return value of function Fork() = 0
child : x=2
pid: 9578
ppid: 1

The following table can be obtained from the output:

process Process PID PPID of the parent process Fork return value
The process calling fork 9577 24176 9578
Process created by fork 9578 1 0

It can be seen that the parent process of the process that calls fork is the same as the previous article Linux system call: Get PPID24176 in the process PID , and the return value of fork is also the PID of another process. The only thing that confuses me is that the ppid of the process obtained by the fork is 1 instead of the pid of the calling process. After checking on the Internet, I can use the top command to view the process information. The process with pid = 1 is systemd, and I was also surprised to find the PID. = 24176 The process is the previously guessed bash.

Guess you like

Origin www.cnblogs.com/sgawscd/p/12732847.html