美文网首页
11.进程操作

11.进程操作

作者: 陈忠俊 | 来源:发表于2020-04-12 15:17 被阅读0次

1.创建子进程

#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#define E_MSG(str, value) do{perror(str); return (value);} while(0)
int main(void){
    printf("getpid: %d\n", getpid());
    pid_t pid;
    pid = fork();
    if(pid == -1) E_MSG("fork", -1);
    if(pid == 0){ //子进程运行的代码
        printf("create child process OK\n pid = %d\n", getpid());
        printf("get parent pid %d\n", getppid());
    }
    else{ //父进程运行的代码
        printf("Run parent process code\n");
        printf("getpid: %d\n", getpid());
    }
    printf("Both run here\n");
    return 0;
}

运行结果:

zhongjun@eclipse:~/projects$ ./pr
getpid: 2345
Run parent process code
getpid: 2345
Both run here
create child process OK
 pid = 2346
get parent pid 2345
Both run here

2.进程终止

exit(int status)
------
echo $? //查看退出码

3.遗言函数,注册多次,运行多次。注册顺序和调用顺序相反。子进程继承父进程的遗言函数。

#include<stdio.h>
#include<stdlib.h>
void last(void){
   printf("last function");
   return;
}
int main(void){
    atexit(last);
    getchar();
    return 0;
}

on_exit调用方法

#include<stdio.h>
#include<stdlib.h>

void bye(int n, void *arg){
    printf("bye...%d...%s\n", n, (char *)arg);
    return;
}

int main(void){
    on_exit(bye, "goodbye...");
    getchar();
    return 0;
}
  1. 进程同步wait调用及信号。在该方法中,可以再bash终端运行kill -signal pid去测试
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<wait.h>
#include<stdlib.h>

#define E_MSG(str, value) do{perror(str); return (value);} while(0)

int main(void){
    int signal;
    pid_t pid = fork();
    if(pid == -1) E_MSG("fork", -1);
    if(pid == 0){//运行子进程代码
        printf("Run child process %d\n", getpid());
        getchar();
        exit(-1);
    }
    else {//运行父进程代码
        //父进程回收子进程的资源,阻塞状态
        wait(&signal);
        if(WIFEXITED(signal)) //子进程正常终止
            printf("exit code: %d\n", WEXITSTATUS(signal));
        if(WIFSIGNALED(signal))//子进程被其他程序打断
            printf("Signum... %d \n", WTERMSIG(signal));
        printf("parent process...%d", getpid());
    }
    return 0;
}

waitpid用法

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

#define E_MSG(str, value) do{perror(str); return (value);} while(0)

int main(void){
    int signal;
    pid_t pid = fork();
    if(pid == -1) E_MSG("fork", -1);
    if(pid == 0){//运行子进程代码
        printf("Run child process %d\n", getpid());
        getchar();
        exit(-1);
    }
    else {//运行父进程代码
        int w = waitpid(-1, &signal, WNOHANG); //父进程回收子进程的资源,非阻塞
        if(w == 0){//没有子进程的终止
            printf("parent exit...");
            return 0;
        }
        if(WIFEXITED(signal)) //子进程正常终止
            printf("exit code: %d\n", WEXITSTATUS(signal));
        if(WIFSIGNALED(signal))//子进程被其他程序打断
            printf("Signum... %d \n", WTERMSIG(signal));
        printf("parent process...%d", getpid());
    }
    return 0;
}

相关文章

  • 11.进程操作

    1.创建子进程 运行结果: 2.进程终止 3.遗言函数,注册多次,运行多次。注册顺序和调用顺序相反。子进程继承父进...

  • Linux Shell脚本经典案例(二)

    11.统计 /proc 目类下 Linux 进程相关数量信息,输出总进程数,running 进程数,stoped ...

  • JavaScript-DOM

    操作DOM 更新DOM 9.表单 10.操作文件 11.回调

  • java基础知识总结11-20

    11.进程和线程的区别是什么? https://www.cnblogs.com/alanfeng/p/505514...

  • 五、python多进程与多线程。

    进程进程的概念是需要理解的,进程是操作系统中正在运行的一个程序实例,操作系统通过进程操作原语来对其进行调度。操作系...

  • linux API记录

    文件内容操作 进程操作

  • 进程操作

    一、进程创建 进程树 父进程创建若干子进程,子进程再创建其子进程,依次类推,构成进程树。进程的调度,其实就是在进程...

  • 进程操作

    关闭进程 psto show them all. and type:kill -9 PID_of_process

  • LInux-基本指令

    1.基本文件和目录的操作 2.系统,进程,网络 系统操作 进程操作 网络操作 3.其他工具 日期

  • 进程和线程的理解

    进程:对于操作系统而言,进程是整个现代操作系统的核心根本,操作系统是以进程为单位执行任务。 线程:随着技术的发展,...

网友评论

      本文标题:11.进程操作

      本文链接:https://www.haomeiwen.com/subject/diivmhtx.html