basically i'm learning linux and more about process. Here is what i want to do : I'm trying to execute the command "ls | wc -l"
So i got a child proc, closing 1 (the standard output) and calling a ls, the main proc is closing 0, the read output and exec a wc -l .. It's compiling but it returns nothing when i test it
Here is what i have done :
#include <stdio.h>
#include <stdlib.h>
#include "erreur.c"
#include <unistd.h>
#include <sys/types.h>
// ls | wc -l
int pfd[2];
main()
{
if(pipe(pfd) == -1) erreur("pipe");
switch(fork())
{
case -1 : erreur("fork");
case 0 : fils();
default : pere();
}
}
fils()
{
if(close(1)==-1) erreur("close");
if(dup(pfd[1])!=1) erreur("close");
if((close(pfd[0])==-1) || (close(pfd[1])==-1)) erreur("close");
execlp("ls","ls",NULL);
}
pere()
{
if(close(0)==-1) erreur ("close");
if(dup(pfd[0])!=0) erreur("dup");
if((close(pfd[0])==-1) || (close(pfd[1])==-1))
execlp("wc","wc","-l",NULL);
}
I'm learning, so any kind of help is appreciated!
Aucun commentaire:
Enregistrer un commentaire