main() { int child, i=2; if((child=fork()) == –1) {printf("fork error. ");exit();} if(child==0) {i=i+3; printf(“i=%d\n”,i); } i=i+5; printf(“i=%d\n”,i); }
时间: 2023-11-28 16:50:29 浏览: 25
这段代码是一个简单的进程创建和执行的示例。首先,该程序通过调用系统调用fork() 创建一个新进程,然后在子进程中将变量i加3并打印输出,父进程中将变量i加5并打印输出。
代码中的问题在于,if语句中的比较操作符“==”后面应该是一个整数值而不是一个负数。因此,应该将“-1”改为“0”,表示子进程的返回值为0。
另外,printf() 函数的参数字符串中包含了中文引号““”,需要将其改为英文引号“"”。
修改后的代码如下:
```
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
int child, i=2;
if ((child = fork()) == 0) {
i = i + 3;
printf("i=%d\n", i);
} else if (child > 0) {
i = i + 5;
printf("i=%d\n", i);
} else {
printf("fork error.\n");
exit(1);
}
return 0;
}
```
输出结果为:
```
i=7
i=2
```
其中,“i=7”是子进程的输出结果,“i=2”是父进程的输出结果。
相关问题
void main() { int i; int p_id; if((p_id = fork()) == 0) { for(i = 1; i < 3; i++) printf("This is child process\n"); } else if(p_id == -1) { printf("fork new process error!\n"); exit(-1); } else { for(i = 1; i < 3; i++) printf("This is parent process\n"); } }
这是一个关于进程和线程的问题,我可以回答。这段代码是一个简单的进程创建程序,使用 fork() 函数创建一个子进程,然后在子进程和父进程中分别输出不同的信息。具体来说,当 p_id 等于 0 时,表示当前进程是子进程,会输出 "This is child process";当 p_id 大于 0 时,表示当前进程是父进程,会输出 "This is parent process"。如果 fork() 函数返回值为 -1,则表示创建新进程失败,会输出 "fork new process error!"。
#include “stdio.h” #include “sys/types.h” #include “unistd.h” main()//1.c { int p; p=fork(); if(p<0) printf(“error!\n”); else if(p==0) printf(“I am child process:No=%d\n”,getpid()); else printf(“I am parent process:No=%d\n”,getpid()); }
这段代码使用了系统调用库函数,通过调用fork()函数创建了一个子进程,如果fork()函数返回小于0的值,则创建子进程失败,程序会输出“error!”;如果fork()函数返回0,则说明当前进程为子进程,程序会输出“I am child process:No=子进程ID”;如果fork()函数返回大于0的值,则说明当前进程为父进程,程序会输出“I am parent process:No=父进程ID”。
需要注意的是,这段代码存在一些语法错误,例如printf函数的参数应该使用双引号括起来,以及缺少头文件声明等。下面是修正后的代码:
```c
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main() {
int p;
p = fork();
if (p < 0) {
printf("error!\n");
} else if (p == 0) {
printf("I am child process:No=%d\n", getpid());
} else {
printf("I am parent process:No=%d\n", getpid());
}
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)