printf("%d child %4d %4d %4d/n", i, getppid(), getpid(), fpid);
时间: 2024-02-01 20:12:23 浏览: 25
这是一个 C 语言中的 printf 函数调用,用于在终端打印输出一段文本。其中 %d 表示输出一个整数,%4d 表示输出一个占据 4 个字符位置的整数(不足 4 位则在前面填充空格),/n 表示换行。
具体来说,这段代码输出了三个整数,分别是当前进程的父进程 ID、当前进程的 ID,以及 fork 函数返回的子进程 ID。其中 i 是一个变量,表示循环的次数或者某个计数器的值。这段代码可能用于在一个父进程中创建多个子进程,并打印输出它们的 ID 以及父进程的 ID。
相关问题
#include <stdio.h> #include <unistd.h> int main(){ int fpid = fork(); if(fpid == 0){ printf("%d I am Child 1. My parent is %d\n",getpid(),getppid()); //此子进程下一条语句将会执行return 0; }else if(fpid > 0){ int fpid2 = fork(); if(fpid2 == 0){ printf("%d I am Child 2. My parent is %d\n",getpid(),getppid()); //此子进程下一条语句将会执行return 0; }else if(fpid2 > 0){ printf("%d I am Parent. My children are %d,%d\n",getpid(),fpid,fpid2); }else{ printf("子进程fpid2创建失败\n"); } }else{ printf("子进程fpid创建失败\n"); } return 0; }
这是一个使用 fork() 函数创建子进程的 C 语言程序。该程序创建了两个子进程,第一个子进程被称为 Child 1,第二个子进程被称为 Child 2,而父进程被称为 Parent。程序输出的内容包括每个进程的进程 ID 和它的父进程的进程 ID。以下是该程序的运行结果:
```
5237 I am Parent. My children are 5238,5239
5238 I am Child 1. My parent is 5237
5239 I am Child 2. My parent is 5237
```
可以看出,父进程创建了两个子进程,每个子进程都输出了自己的进程 ID 和父进程的进程 ID。值得注意的是,每个子进程的下一条语句将会执行 return 0,因此它们会在输出完信息后立即结束。而父进程会在子进程结束后继续执行,直到执行完 return 0。
修改这段代码,使其能够顺利运行:#include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/wait.h> int main(){ pid_t pc1,pc2,pw1,pw2; pc1=fork(); pc2=fork(); if (pc1>0&&pc2>0) { //*父进程 pw1=wait(NULL); pw2=wait(NULL); printf("***Catch a dead child process with pid: %d\n",pw1); printf("***Catch a dead child process with pid: %d\n",pw2); printf("***I'M %d,THE MAIN PROCESS LEAVE!\n",getpid()); }//if if (pc1==0&&pc2>0) { //*子进程1 printf("===I'M the first child PID:%d,my father is:%d\n",getpid(),getppid()); sleep(10); }//if if (pc1>0&&pc2==0) //*子进程2 printf("===I'M the 2nd child PID:%d,my father is:%d,i don't sleep.\n",getpid(),getppid()); if (pc1==0&&pc2==0) //*孙进程 printf("I'M grandson PID:%d,my father is:%d,no one is waiting for – me.\n",getpid(),getppid()); exit(0); } //main
这段代码存在一些问题,需要进行修改:
1. 在第一个子进程执行pc2=fork()之前需要加上exit(0),否则会产生多余的孙进程。
2. 在if (pc1>0&&pc2>0)的代码块中需要加上else,否则会执行多次输出的操作。
修改后的代码如下:
```
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(){
pid_t pc1,pc2,pw1,pw2;
pc1=fork();
if (pc1 == 0) {
//*子进程1
printf("===I'M the first child PID:%d,my father is:%d\n",getpid(),getppid());
exit(0);
} else if (pc1 > 0) {
pc2=fork();
if (pc2 == 0) {
//*子进程2
printf("===I'M the 2nd child PID:%d,my father is:%d,i don't sleep.\n",getpid(),getppid());
exit(0);
} else if (pc2 > 0) {
//*父进程
pw1=wait(NULL);
pw2=wait(NULL);
printf("***Catch a dead child process with pid: %d\n",pw1);
printf("***Catch a dead child process with pid: %d\n",pw2);
printf("***I'M %d,THE MAIN PROCESS LEAVE!\n",getpid());
}
}
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)