PracticeDev/study_clang/mult-progress/test2.c

34 lines
770 B
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <stdio.h>
#include <unistd.h>
int main(int arg,char* argv[]){
// int 被 typedef 为 pid_t
pid_t pid=fork();
// 当pid==0时是子进程代码运行区域。其他则是父进程运行区域。
if(pid<0){
printf("Create child process failed ...\n");
}else if(pid==0){
//子进程执行体
printf("Create child process successfully %i \n",getpid());
}
else{
//父进程执行体
printf("This is parent process %i \n",getpid());
}
// 执行体结束标志
if(pid==0)
{
sleep(3);
printf("pid=%i child process end ... \n",getpid());
}
else{
// 睡眠5s,等待子进程结束
sleep(5);
printf("pid=%i Parent process end ... \n",getpid());
}
return 0;
}