我想實現使用c語言的管道。 我通過創建子進程在c中實現管道,但它不工作
繼執行由管道分隔每個命令的循環是我的代碼:
int main(int argc, char **argv)
{
char *cmd,*splitcmd;
int i,j,nargc=0,characters;
char **cmdArray;
size_t bufsize = 1024;
int *pipefd;
int pipeArrCount;
pid_t pid,wpid;
int status = 0;
int savestdoutfd = dup(fileno(stdout));
int savestdinfd = dup(fileno(stdin));
cmd = (char *)malloc(bufsize * sizeof(char));
characters = getline(&cmd,&bufsize,stdin);
// printf("%s%d",cmd,characters);
if(cmd[characters-1]=='\n')
{
// printf("in c");
cmd[characters-1]='\0';
characters--;
}
cmdArray = (char**)malloc(sizeof(char *) * 100);
splitcmd=strtok(cmd,"|");
// printf("%s\n",cmd);
while((splitcmd))
{
cmdArray[nargc] = splitcmd;
if(cmdArray[nargc][(strlen(cmdArray[nargc]))-1]==' ')
cmdArray[nargc][(strlen(cmdArray[nargc]))-1]='\0';
printf("%d %s",nargc,cmdArray[nargc]);
nargc++;
splitcmd = strtok(NULL,"|");
}
pipefd=(int*)malloc(2*nargc*sizeof(int));
printf("%d\n",nargc);
pipeArrCount=2*(nargc-1);
printf("%d\n",pipeArrCount);
//exit(0);
for(i=0;i<pipeArrCount;i)
{
printf("making pipe for process %d\n",i);
pipe(pipefd+i);
i=i+2;
}
//exit(0);
for(i=0;i<nargc;i)
{
printf("parent count %d\n",i);
if(i==0)
{
printf("Creating child %d\n",i);
// As it is first process we need to make write end of the pipe as stdout.
if ((pid=fork()) == 0)
{
printf("Creating first child %d for command %s\n",i,cmdArray[i]);
printf("EXECUTING FIRST PROCESS\n");
printf("Writing in pipe[%d]\n",2*i+1);
//close(pipefd[0]);
dup2(pipefd[2*i+1],fileno(stdout));
//closing all other pipes
for(j=0;j<(2*nargc);j++)
{
//if(j!=2*i+1)
close(pipefd[j]);
}
system(cmdArray[i]);
//dup2(savestdoutfd,fileno(stdout));
printf("Stdout is again restored\n");
//printf("pipe [%d] contains %d ",2*i+1,pipefd[2*i+1]);
exit(0);
}
}
else if(i!=nargc-1)
{
if (fork() == 0)
{
printf("EXECUTING MIDDLE PROCESS\n");
printf("Command to execute %s \n",cmdArray[i]);
printf("Reading from pipe[%d]\n",(2*(i-1)));
printf("writing on pipe[%d]\n",(2*i)+1);
dup2(pipefd[(2*(i-1))], 0); //Read end of the previous process pipe as stdin
dup2(pipefd[(2*i)+1], 1); //Write end of the pipe of current process as stdout
//closing all other pipes
for(j=0;j<(2*nargc);j++)
{
//if((j!=(2*(i-1))) && (j!=(2*i)+1))
close(pipefd[j]);
}
system(cmdArray[i]);
exit(0);
}
}
else
{
if (fork() == 0)
{
printf("Creating last child %d for command %s\n",i,cmdArray[i]);
printf("Reading from pipe[%d]\n",(2*(i-1)));
//close(pipefd[1]);
dup2(pipefd[(2*(i-1))],fileno(stdin)); //Read from the end of the previous process pipe as stdin
//printf("EXECUTING LAST PROCESS\n");
//closing all other pipes
for(i=0;j<(2*nargc);j++)
{
//if(j!=(2*(i-1)))
close(pipefd[j]);
}
dup2(savestdoutfd,fileno(stdout));
close(savestdoutfd);
system(cmdArray[i]);
dup2(savestdinfd,fileno(stdin));
close(savestdinfd);
exit(0);
}
}
i=i+1;
sleep(1);
}
while ((wpid = wait(&status)) > 0);
}
沒有得到第二個命令 的任何輸出現在我試圖執行此'A | B'類型的命令 命令和管道之間的空間是必要的,如果你執行代碼
你能更具體嗎?預計什麼?出了什麼問題? –
我想用c實現unix pipe('|')。 –
我想實現unix pipe('|')使用c.for這個我正在使用pipe()函數進行不同commands.so之間的通信。首先,我嘗試掃描命令並查看需要多少個管道我多次調用管道函數。現在我舉例說明了命令'cat out。c |排序'這個程序做的是獲得一個管道和父進程分別爲兩個命令分出兩個進程第一個命令是使用系統調用執行的,但是這個命令的輸出應該在p [1]的管道的寫入結束處,然後關閉所有pipes.once這樣做....... –