Linux系统调用:创建和终止进程

1.进程的三种状态

1.运行。要么在被CPU执行,要么等待被执行且最终会被内核调度。

2.停止。执行被挂起且不会被调度。收到特定信号后才能继续运行。

3.终止。进程永远地停止了。可能的原因有三种:(1)收到终止进程的信号,(2)从主程序返回,(3)调用exit函数

2.终止进程

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

3.创建进程

父进程通过fork函数创建一个新的运行的子进程:(fork英文意为分岔、餐叉,这里意思应该是从一个进程中分出来了一个子进程),新创建的子进程将得到父进程几乎所有信息的一个副本,二者之间最大的区别在于他们有不同的PID。

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

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

对于系统调用出错的情况,在CSAPP中,提到了一种解决的方式,使用与原函数参数相同、名字相似的含错误处理的包装函数来代替原函数。

例如对于fork函数,使用一个名为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值返回特定的描述错误的文本串
}

下面就是fork的具体应用:

分为三个文件,alluse.h(头文件),alluse.c(包装函数的定义),fork.c(使用)

//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);
}

编译运行:

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

由输出结果可以得到下表:

进程 进程PID 父进程PPID Fork返回值
调用fork的进程 9577 24176 9578
fork创建的进程 9578 1 0

可见调用fork的进程的父进程与上一篇文章Linux系统调用:获取进程PID中得到的PPID24176相同,并且fork的返回值也是另一个进程的PID。唯一让我有些困惑的就是fork得到的进程的ppid是1而不是调用进程的pid,在网上查了下发现可以使用top命令查看进程信息,pid=1的进程是systemd,同时也惊喜地发现PID=24176进程就是之前猜测的bash。

猜你喜欢

转载自www.cnblogs.com/sgawscd/p/12732847.html