⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wait.c

📁 《Beginning Linux Programming》书的配置实例源代码。
💻 C
字号:
#include <sys/types.h>#include <sys/wait.h>#include <unistd.h>#include <stdio.h>int main(){    pid_t pid;    char *message;    int n;    int exit_code;    printf("fork program starting\n");    pid = fork();    switch(pid)     {    case -1:        exit(1);    case 0:        message = "This is the child";        n = 5;        exit_code = 37;        break;    default:        message = "This is the parent";        n = 3;        exit_code = 0;        break;    }    for(; n > 0; n--) {        puts(message);        sleep(1);    }/*  This section of the program waits for the child process to finish.  */    if(pid) {        int stat_val;        pid_t child_pid;        child_pid = wait(&stat_val);        printf("Child has finished: PID = %d\n", child_pid);        if(WIFEXITED(stat_val))            printf("Child exited with code %d\n", WEXITSTATUS(stat_val));        else            printf("Child terminated abnormally\n");    }    exit (exit_code);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -