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

  • Home
  • SEARCH
  • 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 7857999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:13:24+00:00 2026-06-02T21:13:24+00:00

I want to simulate this Unix command : cat file.txt | sort | tail

  • 0

I want to simulate this Unix command :

cat file.txt | sort | tail -4

I followed the technique, but it does not work, it remains blocked.
Maybe i need to use something else when there are files.
I used two pipes, and two processes and i used two DUP in a single process, maybe that’s wrong.

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>

int main()
{
  int p1[2];
  int p2[2];

  if(pipe(p1))
  {
    perror("pipe1");
    exit(0);
  }

  if(pipe(p2))
  {
    perror("pipe2");
    exit(0);
  }

  switch(fork())
  {
    case -1: perror(" fork1 error ");
             exit(0);

    case  0: close(STDOUT_FILENO);
             (void)dup(p1[1]);
             close(p1[1]);
             close(p1[0]);
             execlp("cat", "cat", "file.txt", NULL);
             exit(0);
    default: 
            switch(fork())
            {
              case -1: perror(" fork2 error ");
               exit(0);

              case  0: close(STDIN_FILENO);
                       (void)dup(p1[0]);
                       close(p1[1]);
                       close(p1[0]);

                       close(STDOUT_FILENO);
                       (void)dup(p2[1]);
                       close(p2[1]);
                       close(p2[0]);

                       execlp("sort", "sort", NULL);
                       exit(0);

              default: 
                       wait(NULL);
                       close(STDIN_FILENO);
                       (void)dup(p2[0]);
                       close(p2[0]);
                       close(p2[1]);
                       execlp("tail", "tail", "-4", NULL); 
            }             
  }
}

this is the file.txt :

g
f
d
b
c
a
e
  • 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-02T21:13:27+00:00Added an answer on June 2, 2026 at 9:13 pm

    The parent process never closes the pipe p1 so its child keep trying to read on it. Add close(p1[0]); close(p1[1]); before the execlp("tail", "tail", "-4", NULL);.

    Also note that you should not wait(NULL): this is another hang waiting to happen when file.txt is big and starts to fill the pipe buffer.

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

Sidebar

Related Questions

I don't know what exactly this feature is, but I would like to simulate
I have this small piece of code and i want to to simulate the
How can I do this? Specifically, I want to simulate those mouseclicks at certain
I need to wrap the Unix command tail -f in a BufferedInputStream. I don't
I have this swing client-server app. I want to simulate the ' Ctrl+Shift+f1 '
I'm using this program called MMlogic. I want to simulate a traffic light. Counter
I have one IP packet, which includes IGMP packet. I want to simulate this
I want to simulate N-sided biased die? def roll(N,bias): '''this function rolls N dimensional
If I want to simulate press on home button I do this Intent startMain
I want to simulate the Right click/Update service reference command in a VS2010 addin.

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.