C 管道错误:int不是函数
我使用这段代码试图在一个简单的shell中实现我自己的管道。然而,它不会编译,因为它告诉我fd不是一个函数。我见过的其他地方都以完全相同的方式将fd作为pipe()的参数。我不知道为什么会出现这个错误C 管道错误:int不是函数,c,shell,pipe,C,Shell,Pipe,我使用这段代码试图在一个简单的shell中实现我自己的管道。然而,它不会编译,因为它告诉我fd不是一个函数。我见过的其他地方都以完全相同的方式将fd作为pipe()的参数。我不知道为什么会出现这个错误 int startProcess (StringArray sa) { int pid; int status; int fd1; int fd2; int current_in; int current_out; int fd0; int fd00; in
int startProcess (StringArray sa)
{
int pid;
int status;
int fd1;
int fd2;
int current_in;
int current_out;
int fd0;
int fd00;
int in = 0;
int out = 0;
char input[64]="";
char output[64]="";
char cmd1[64] ="";
char cmd2[64] ="";
int pipe = 0;
int fd[2];
switch( pid = fork()){
case -1://This is an error
perror("Failure of child.");
return 1;
case 0: // This is the child
// Redirection
/* finds where '<' or '>' or '|' occurs and make that sa[i] = NULL ,
to ensure that command wont' read that*/
for(int i=0;sa[i]!='\0';i++)
{
if(strcmp(sa[i],"<")==0)
{
sa[i]=NULL;
strcpy(input,sa[i+1]);
in=2;
}
if(strcmp(sa[i],">")==0)
{
sa[i]=NULL;
strcpy(output,sa[i+1]);
out=2;
}
if(strcmp(sa[i],"|")==0)
{
sa[i]=NULL;
strcpy(cmd1,sa[i-1]);
strcpy(cmd2,sa[i+1]);
pipe=2;
}
}
//if '<' char was found in string inputted by user
(erased for brevity)
//if '>' char was found in string inputted by user
(erased for brevity)
//if '|' char was found in string inputted by user
if(pipe)
{
pipe(fd);
if (!fork)
{
close(1);
dup(fd[1]);
close(fd[0]);
int error;
error = 0;
if (fork() == 0){
error = execvp(cmd1, sa);
} if (error == -1) {
printf("ERROR: unknown command (%s)\n)", cmd1);
exit(0);
} else {
waitpid(0,NULL,0);
}
}
} else {
close(0);
dup(fd[0]);
close(fd[1]);
execvp(cmd2, sa);
}
execvp(sa[0], sa);
perror("execvp");
_exit(1);
printf("Could not execute '%s'\n", sa[0]);
default:// This is the parent
wait(&status);
return (status == 0) ? 0: 1;
}
}
int启动进程(StringArray sa)
{
int-pid;
智力状态;
int-fd1;
int-fd2;
int当前_in;
int电流输出;
int-fd0;
int fd00;
int in=0;
int out=0;
字符输入[64]=“”;
字符输出[64]=“”;
char cmd1[64]=“”;
char cmd2[64]=“”;
int管道=0;
int-fd[2];
开关(pid=fork()){
案例1://这是一个错误
perror(“儿童的失败”);
返回1;
案例0://这是孩子
//重定向
/*查找“”或“|”出现的位置,并使sa[i]=NULL,
以确保命令不会读取该内容*/
对于(int i=0;sa[i]!='\0';i++)
{
if(strcmp(sa[i],“”)==0)
{
sa[i]=NULL;
strcpy(输出,sa[i+1]);
out=2;
}
if(strcmp(sa[i],“|”)==0)
{
sa[i]=NULL;
strcpy(cmd1,sa[i-1]);
strcpy(cmd2,sa[i+1]);
管道=2;
}
}
//如果在用户输入的字符串中找到“”字符
(为简洁起见删除)
//如果在用户输入的字符串中找到“|”字符
如果(管道)
{
管道(fd);
如果(!fork)
{
关闭(1);
dup(fd[1]);
关闭(fd[0]);
整数误差;
误差=0;
如果(fork()==0){
错误=execvp(cmd1,sa);
}如果(错误==-1){
printf(“错误:未知命令(%s)\n)”,cmd1);
出口(0);
}否则{
waitpid(0,NULL,0);
}
}
}否则{
关闭(0);
dup(fd[0]);
关闭(fd[1]);
执行副总裁(cmd2,sa);
}
execvp(sa[0],sa);
perror(“执行副总裁”);
_出口(1);
printf(“无法执行“%s”\n”,sa[0]);
默认值://这是父项
等待(&状态);
返回(状态==0)?0:1;
}
}
首先,您需要
int pipe = 0;
那么你也有
pipe(fd);
两者不可能都是正确的
我建议您重命名变量。确切的错误消息是什么?您的
管道
既是int
又是函数。这是故意的吗?非常感谢,让别人查看我的代码非常有帮助。