Linux中通过fork()同时创建多个进程

xiaoxiao2021-02-28  69

1、使用系统调用fork()创建三个子进程;

2、各个子进程显示和输出一些提示信息和自己的进程标识符;

3、父进程显示自己的进程ID和一些提示信息,然后调用waitpid()等待多个子进程结束,并在子进程结束后显示输出提示信息表示程序结束。

#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <stdarg.h> #include <sys/types.h> #include <sys/wait.h> #include <time.h> int tprintf(const char *fmt,...); int main(void) { printf("I'm your father,PID is %d.\n",getpid()); pid_t pid = fork(); if(pid == 0){ printf("I'm a first son.PID is %d.\n",getpid()); exit(1);//若此处没有exit(1), 进程也会执行 pid_t pid2 = fork()语句,会出现孙进程 printf("You should never see this.\n"); } pid_t pid2 = fork(); if(pid2 == 0){ printf("I'm a second son.PID is %d.\n",getpid()); exit(1); printf("You should never see this.\n"); } pid_t pid3 = fork(); if(pid3 ==0){ printf("I'm a third son.PID is %d.\n",getpid()); exit(1); printf("You should never see this.\n"); } else if(pid != -1){ tprintf("Parent forked child process--%d.\n",pid); tprintf("Parent is waiting for child to exit.\n"); waitpid(pid,NULL,0); waitpid(pid2,NULL,0); waitpid(pid3,NULL,0); tprintf("Child Process had exited.\n"); tprintf("Parent had exited.\n"); } else tprintf("Everything was done without error.\n"); return 0; } /* * 设置输出格式 */ int tprintf(const char* fmt,...) { va_list args; struct tm *tstruct; time_t tsec; tsec = time(NULL); tstruct = localtime(&tsec); printf("d:d:d:]|",tstruct->tm_hour,tstruct->tm_min,tstruct->tm_sec,getpid()); va_start(args,fmt); return vprintf(fmt,args); }
转载请注明原文地址: https://www.6miu.com/read-2620856.html

最新回复(0)