Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6714131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:27:36+00:00 2026-05-26T08:27:36+00:00

#include <unistd.h> #include <stdio.h> int main(int argc, char* argv[]) { int f1[2], f2[2]; char

  • 0
#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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:27:36+00:00Added an answer on May 26, 2026 at 8:27 am

    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]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include<stdio.h> #include<unistd.h> #include<stdlib.h> int main(int argc,char **argv) { int fd[2]; pid_t childpid; pipe(fd); childpid=fork();
#include<stdio.h> #include<zlib.h> #include<unistd.h> #include<string.h> int main(int argc, char *argv[]) { char *path=NULL; size_t size;
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main (int argc, const char * argv[])
#include <stdlib.h> #include <unistd.h> #include <string.h> #include <sys/types.h> #include <stdio.h> int main(int argc, char
Here is my code: #include<stdio.h> #include<stdlib.h> #include<sys/stat.h> #include<sys/types.h> #include<fcntl.h> #include<unistd.h> #include<errno.h> int main(int argc,char
#include<stdio.h> int main() { float a; printf(Enter a number:); scanf(%f,&a); printf(%d,a); return 0; }
#include <stdio.h> #include <unistd.h> #include <string.h> int good(int addr) { printf(Address of hmm: %p\n,
I created a small program, as follows: #include <math.h> #include <stdio.h> #include <unistd.h> int
In C, I can say #include <stdio.h> #include <unistd.h> #include <signal.h> int continue_running =
Here is my code for fork() system call, #include<stdio.h> #include<stdlib.h> #include<sys/types.h> #include<sys/stat.h> #include<unistd.h> #include<errno.h>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.