Domiik
Domiik

Reputation: 233

fork, execlp in Linux

#include <unistd.h>
#include <stdio.h>


int main(int argc, char* argv[])
{

  int f1[2], f2[2];
  char buff;

  if(pipe(f1) != -1);
   printf("Pipe1 allright! \n");

  if(pipe(f2) != -1);
   printf("Pipe2 allright \n");



  if(fork()==0)
  {
    close(1);
    dup(f1[1]);
    close(0);
    execlp("ls", "ls", "-l", NULL);  
  }
  else
  {
    if(fork()==0)
    {  
    close(0);
    dup(f1[0]);
    close(1);
    dup(f2[1]);
     execlp("grep", "grep", "^d", NULL); 
    }
    else 
    {
        if(fork()==0)
        {
            close(0);
            dup(f2[0]);
            execlp("wc", "wc", "-l", NULL); 
      } 
    }

  }

  return 0;

}

I am trying to do ls -l | grep ^d | wc -l in C.

I tried everythig...

What is wrong? :(

Output: Pipe1 allright!, Pipe2 allright!

Ps. Your post does not have much context to explain the code sections; please explain your scenario more clearly.

Upvotes: 1

Views: 7084

Answers (2)

A.H.
A.H.

Reputation: 66283

There are several problems with your code:

if(pipe(f1) != -1);
  printf("Pipe1 allright! \n");

I assume this should be a real error check, so please remove the ; in the if line.

Running your program after that you will notice, that the grep and wc commands are still there, they don't terminate. Check this with the ps(1) command. The ls command seems to have terminated.

Let's assume, the pids of the four processes are :

  • 9000 (main)
  • 9001 (ls)
  • 9002 (grep)
  • 9003 (wc)

Looking into /proc/9002/fd you will see, that filehandle 0 (stdin) is still open for reading:

> ll /proc/9002/fd/0
lr-x------ 1 as as 64 2011-10-22 20:10 0 -> pipe:[221916]

And looking around, who has this handle still open with

> ll /proc/*/fd/* 2>/dev/null | grep 221916

you will see, that many handles to this pipe are open: both grep and wc have two of them open. Same is true for the other pipe handles.

Solution:

You have to close the pipe handles after dup rigorously. Look here:

#include <unistd.h>
#include <stdio.h>


int main(int argc, char* argv[])
{
     int f1[2];
     char buff;

     if(pipe(f1) != -1)
          printf("Pipe1 allright! \n");

     int pid = fork();
     if(pid==0)
     {
          close(1);
          dup(f1[1]);

          close(f1[0]);
          close(f1[1]);

          close(0);
          execlp("ls", "ls", "-l", NULL);  
     }
     printf("ls-pid = %d\n", pid);

     int f2[2];
     if(pipe(f2) != -1)
          printf("Pipe2 allright \n");

     pid = fork();
     if(pid==0)
     {  
          close(0);
          dup(f1[0]);

          close(f1[0]);
          close(f1[1]);

          close(1);
          dup(f2[1]);

          close(f2[0]);
          close(f2[1]);

          execlp("grep", "grep", "^d", NULL);
          // system("strace grep '^d'"); exit(0);
     }
     printf("grep-pid = %d\n", pid);

     close(f1[0]);
     close(f1[1]);

     pid = fork();
     if(pid==0)
     {
          close(0);
          dup(f2[0]);
          close(f2[0]);
          close(f2[1]);

          execlp("wc", "wc", "-l", NULL); 
     }
     printf("wc-pid = %d\n", pid);

     close(f2[0]);
     close(f2[1]);
}

Upvotes: 3

unbeli
unbeli

Reputation: 30248

you probably want to use dup2 instead of dup, that is, instead of

close(1);
dup(f1[1]);

do

dup2(f1[1], 1);

and so on for other occurencies

Upvotes: 1

Related Questions