进程的管理

#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;
}
相关推荐
C_心欲无痕2 小时前
ts - tsconfig.json配置讲解
linux·前端·ubuntu·typescript·json
冰西瓜6003 小时前
国科大2025操作系统高级教程期末回忆版
linux
HIT_Weston4 小时前
93、【Ubuntu】【Hugo】搭建私人博客:面包屑(一)
linux·运维·ubuntu
cuijiecheng20184 小时前
Linux下Beyond Compare过期
linux·运维·服务器
喵叔哟4 小时前
20.部署与运维
运维·docker·容器·.net
HIT_Weston4 小时前
92、【Ubuntu】【Hugo】搭建私人博客:侧边导航栏(六)
linux·运维·ubuntu
CodeAllen嵌入式4 小时前
Windows 11 本地安装 WSL 支持 Ubuntu 24.04 完整指南
linux·运维·ubuntu
期待のcode5 小时前
前后端分离项目 Springboot+vue 在云服务器上的部署
服务器·vue.js·spring boot
AI 智能服务5 小时前
第6课__本地工具调用(文件操作)
服务器·人工智能·windows·php
码农小韩6 小时前
基于Linux的C++学习——指针
linux·开发语言·c++·学习·算法