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 9146881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:51:11+00:00 2026-06-17T10:51:11+00:00

I am writing a C program which will run Linux commands, like: cat /etc/passwd

  • 0

I am writing a C program which will run Linux commands, like:
cat /etc/passwd | grep list | cut -c 1-5
and i didnt have any result
*here the parent wait for the first child(child_pid) to finish;
and the first child wait for the second(pfils) !!
any idea?
code :

main()
{
    pid_t child_pid;
    int fd[2];
    int pfd[2];
    pipe(pfd);
    child_pid = fork ();

    if (child_pid != 0) 
    {
        wait(child_pid);
        close(pfd[1]);
        dup2(pfd[0],0);
        close(pfd[0]);
        execlp("cut","cut","-c","1-5",NULL);
    }
    else
    {
        pid_t pfils = fork();
        pipe(fd);

        if(pfils == 0)
        {
            close(fd[0]);
            dup2(fd[1],1);
            close(fd[1]);
            execlp("cat", "cat","/etc/passwd",NULL);
        }
        else
        {
            wait(pfils);
            close(fd[1]);
            dup2(fd[0],0);
            close(fd[0]);
            close(pfd[0]);
            dup2(pfd[1],1);
            close(pfd[1]);
            execlp("grep","grep","list",NULL);
        }
    }
}
  • 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-06-17T10:51:12+00:00Added an answer on June 17, 2026 at 10:51 am
        pid_t pfils = fork();
        pipe(fd);
    

    Fork will make a copy of the process. An independent copy of the process. So, if you call pipe after fork, each copy of the process gets their own pipe. Put pipe() before fork() and it should work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a C program which will run Linux commands, like: $ cat
I am writing a C program which will run Linux commands, like: $ cat
I am writing a window program which will run from the system tray. which
I have been tasked with writing a program which will run a set of
I am writing a java program that will run tasks in parallel, which I
Since I am writing a program that will eventually run on Windows and Linux
I am writing a .NET program which will run on a computer with several
I'm writing a program which will use scan conversion on triangles to fill in
I'm writing a program which will allow to load a specific managed .DLL file
I am writing a program in Ruby which will search for strings in text

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.