я пытаюсь сделать ls -la |wc
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
int main(int argc, char **argv)
{
int pipes=3;
char *ls[] = {"ls","-la",NULL};
char *wc[] = {"wc",NULL};
char *base64[] = {"base64","-w0",NULL};
//char **commands[] = {ls,wc,base64};
int fds[pipes][2];
for(int i=0;i<pipes;i++)
{
int err = pipe(fds[i]);
if(err == -1)
{
perror("Pipe failed.\n");
}
}
int status;
pid_t childPid;
//Child 1.
if((childPid = fork()) == 0)
{
dup2(fds[0][1],1);
for(int i=0;i<pipes;i++)
{
close(fds[i][0]);
close(fds[i][1]);
}
execvp(ls[0],ls);
exit(0);
}
else if(childPid == -1)
{
perror("Child 1 failed.\n");
}
// Second child.
if((childPid = fork()) == 0)
{
dup2(fds[0][0],0);
for(int i=0;i<pipes;i++)
{
close(fds[i][0]);
close(fds[i][1]);
}
execvp(wc[0],wc);
}
else if(childPid == -1)
{
perror("Child 2 failed.\n");
}
for(int i=0;i<pipes;i++)
{
close(fds[i][0]);
close(fds[i][1]);
}
waitpid(childPid,&status,WUNTRACED|WNOHANG);
return 0;
}
out ожидают:
root @ danial # gcc -o pip pip.c
root@danial#./pip
10 83 436
вывод Я получаю:
root@danial#./pip
root @ danial # 10 83 436
остается курсоромздесь, пока я не нажму клавишу ввода.
Я пытался сделать это без каналов, только что написал простую программу:
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/wait.h>
int main(int argc, char **argv)
{
if(fork() == 0)
{
execlp("ls","ls","-la",NULL);
exit(0);
}
return 0;
}
и то же случилось:
root@danial#./test
root @ danial # всего 84
drwxr-xr-x 3 root root 4096 30 марта, 06:49.
drwxr-xr-корень корня x 9 4096 29 марта 09:33 ..
-rwxr-xr-x 1 корневой корень 16960 30 марта 06:49 пипсов
-rw-r - r-- 1root root 1310 30 марта 06:48 pip.c