进程的管理

#include <unistd.h>

void _exit(int status);

#include <stdlib.h>

void _Exit(int status);

status参数:是进程退出时的状态信息,父进程在回收子进程资源的时候可以获取到

cpp 复制代码
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>

int main() {
    printf("hello\n");
    printf("world");
    // exit(0);
    _exit(0);
    return 0;
}

exit()函数退出时会刷新I/O缓冲,而_exit()函数退出时不会刷新I/O缓冲

cpp 复制代码
#include <sys/types.h>
#include <unistd.h>
#include<stdio.h>
int main() {
    //创建子进程
    pid_t pid = fork();
    //判断是父进程还是子进程
    if(pid > 0) {
        printf("I am parent process, pid : %d, ppid : %d\n", getpid(), getppid());
    } else if(pid == 0) {
        sleep(1);
        printf("I am child process, pid : %d, ppid : %d\n", getpid(), getppid());
    }
    for(int i = 0; i < 5; i++) {
        printf("i : %d\n", i);
    }
    return 0;
}
cpp 复制代码
#include <sys/types.h>
#include <unistd.h>
#include<stdio.h>
int main() {
    //创建子进程
    pid_t pid = fork();
    //判断是父进程还是子进程
    if(pid > 0) {
        while(1) {
            printf("I am parent process, pid : %d, ppid : %d\n", getpid(), getppid());
            sleep(1);
        }
    } else if(pid == 0) {
        printf("I am child process, pid : %d, ppid : %d\n", getpid(), getppid());
    }
    for(int i = 0; i < 5; i++) {
        printf("i : %d\n", i);
    }
    return 0;
}
相关推荐
java龙王*几秒前
开放端口,开通数据库连接权限,无法连接远程数据库 解决方案
linux
bcbobo21cn1 小时前
Linux命令的命令历史
linux·histsize·histfile
jingyu飞鸟2 小时前
linux系统源代码安装apache、编译隐藏版本号
linux·运维·apache
世事如云有卷舒2 小时前
Ubunt20.04搭建GitLab服务器,并借助cpolar实现公网访问
linux·服务器·gitlab
Little-Hu2 小时前
QML TextEdit组件
java·服务器·数据库
riverz12272 小时前
TCP backlog工作机制
服务器·网络·tcp/ip
2401_858286113 小时前
OS15.【Linux】gdb调试器的简单使用
linux·运维·服务器·开发语言·gdb
c30%004 小时前
内网渗透——红日靶场五
运维·服务器
zjw_rp5 小时前
centos停止维护后更换yum源
linux·运维·centos
宇钶宇夕5 小时前
EPLAN 电气制图:建立自己的部件库,添加部件-加SQL Server安装教程(三)上
运维·服务器·数据库·程序人生·自动化