#include <iostream>
#include <unistd.h>
using namespace std;
int main()
{
int fd[2];
//fd[0] - read
//fd[1] -write
//declare all the variables
int n;
int countN;
int arr[3];
//prompt for the numbers called n
//there are at most 3 nums(use countN to check)
while(countN <= 2){
cin >> n;
// checked whether the num is from range 1 to 15
if(n >= 1 && n <= 15)
{
//if yes, then push the num to array called arr
arr[countN] = n;
countN++;
}
else
{
//if no, then only increase the countN so that we dont cin more than 3 nums
countN++;
}
}
//print all the nums in the array named arr
for(int i = 0; i < 3; i++)
{
cout << arr[i];
}
//initializes fork
pid_t c_pid = fork();
//everything belows this gonna gets executed twice
return 0;
}
因此,我尝试初始化 fork 系统调用,但出现错误:
在函数“int main()”中:“fork”未在此范围内声明。有人可以告诉我我做错了什么,因为我已经检查以确保 fork 初始化不在 if 或 while 循环等任何块内。
我什至尝试在代码的第一行(main()之后)声明它,但它仍然有这个错误。