该代码将命令作为输入并执行它。管道也被处理。问题是假设如果我输入 ls | grep x 作为命令。进程 grep 不退出,因此程序停止。有任何想法吗。
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
void parse(int *cont,char *command,char **exarg)
{
char *x=strchr(command,'&');
if(x!=NULL)
{
*cont=1;
*(--x)='\0';
}
x=strtok(command," ");
while(x!=NULL)
{
*exarg++=x;
x=strtok(NULL," ");
}
*exarg='\0';
}
int divide(char *cmd,char **cmdarr)
{
int i=0;
char *x;
x=strtok(cmd,"|");
while(x!=NULL)
{
i++;
*cmdarr++=x;
x=strtok(NULL,"|");
}
*cmdarr='\0';
return i;
}
void main()
{
pid_t pid;
int cont;
int i=0;
int orgnum;
char *cmdarr[50];
char c[50];
int p1[2];
int p2[2];
char *argument[50];
if(pipe(p1)<0)
printf("error\n");
if(pipe(p2)<0)
printf("error2\n");
for(;;)
{
printf("Enter the command:");
gets(c);
if(!strcmp("exit",c))
break;
orgnum=divide(c,cmdarr);
printf("%d\n",orgnum);
for(i=0;i<orgnum;i++)
{
pid=fork();
if(pid==0)
{
if(i==0)
{
close(p1[0]);
close(p1[1]);
close(p2[0]);
if((i+1)!=orgnum)
{
dup2(p2[1],STDOUT_FILENO);
dup2(p2[1],STDERR_FILENO);
}
close(p2[1]);
parse(&cont,cmdarr[i],argument);
if((execvp(argument[0],argument))<0)
{
printf("Wrong cmd");
exit(0);
}
}
else if(i>0)
{
if((i+1)%2==0)
{
close(p1[0]);
close(p2[1]);
dup2(p2[0],STDIN_FILENO);
close(p2[0]);
if((i+1)!=orgnum)
{
dup2(p1[1],STDOUT_FILENO);
dup2(p1[1],STDERR_FILENO);
}
close(p1[1]);
}
else if((i+1)%2==1)
{
close(p2[0]);
close(p1[1]);
dup2(p1[0],STDIN_FILENO);
close(p1[0]);
if((i+1)!=orgnum)
{
dup2(p2[1],STDOUT_FILENO);
dup2(p2[1],STDERR_FILENO);
}
close(p2[1]);
}
parse(&cont,cmdarr[i],argument);
if((execvp(argument[0],argument))<0)
{
printf("Wrng cmd");
exit(0);
}
}
}
else if(pid>0){
close(p1[0]);
close(p1[1]);
close(p2[0]);
close(p2[1]);
wait(NULL);}
}
}
}