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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:02:12+00:00 2026-05-28T18:02:12+00:00

I am using pipes, fork , dup2 to implement ls | more or ls

  • 0

I am using pipes, fork , dup2 to implement “ls | more” or “ls | sort” etc.
I am just not able to understand the issue here.
When I run my program, I get this error:

./a.out  
Missing filename ("less --help" for help)

Why am I getting “less” ??

What is wrong with this code ? If I change “more” to “ls” again, it works fine. I mean, its like doing ls | ls.

#define STDIN 0
#define STDOUT 1

int main()
{
   int fd[2];
   int pid;
   char *lschar[20]={"ls",NULL};
   char *morechar[20]={"more",NULL};
   pid = fork();
   if (pid == 0) {
   /* child */
     int cpid;
     cpid = fork();
     if(cpid == 0) {
       //printf("\n in ls \n");
       pipe(fd);
       dup2(fd[1], STDOUT);
       close(fd[0]);
       close (fd[1]);
       execvp("ls",lschar);
     } else if(cpid>0) {
       waitpid(cpid, NULL,0);
       dup2(fd[0],STDIN);
       close(fd[0]);
       close(fd[1]);
       execvp("more", morechar);
     }
   } else if (pid > 0) {
     /* Parent */
     waitpid(pid, NULL,0);
   }
   return 0;
}

Appreciate your help.

  • 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-28T18:02:13+00:00Added an answer on May 28, 2026 at 6:02 pm

    Your main problem lies in your placement of the pipe() call. You must call it before you fork():

    #include <unistd.h>
    #include <stdio.h>
    #include <sys/types.h>
    
    #define STDIN 0
    #define STDOUT 1
    
    int main()
    {
      int fd[2];
      int pid;
      char *lschar[20]={"ls",NULL};
      char *morechar[20]={"more", NULL};
      pid = fork();
      if (pid == 0) {
        /* child */
        int cpid;
        pipe(fd);
        cpid = fork();
        if(cpid == 0) {
          //printf("\n in ls \n");
          dup2(fd[1], STDOUT);
          close(fd[0]);
          close (fd[1]);
          execvp("ls",lschar);
        } else if(cpid>0) {
          dup2(fd[0],STDIN);
          close(fd[0]);
          close(fd[1]);
          execvp("more", morechar);
        }
      } else if (pid > 0) {
        /* Parent */
        waitpid(pid, NULL,0);
      }
      return 0;
    }
    

    Otherwise, the more process doesn’t have the correct file descriptors. Further, the waitpid() in your more process is problematic and unnecessary (more will wait for input on its own). If ls had a particularly long output the pipe could get full causing ls to block on its writes. The result is a deadlock and it waits forever. Hence, I’ve also removed the offending waitpid() call.

    Also, if you make a good practice of checking the return values of functions like pipe() and dup2() this error would have been much easier to find — you would have seen that your dup2() was failing.

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

Sidebar

Related Questions

I tried to run commands using pipes. Basic : single=ls -l $single which works
I'm trying to implement the following pipe using anonymous pipes : cat filename |
I'm trying to send and receive using pipes: send.cpp struct { long a; long
The subject line says it all. I'd also like to do this using pipes.
I remember using Named Pipes for communicating between 2 machines (back in Windows NT).
Lately I've been favoring using named pipes (option --enable-named-pipes) in MySQL running on windows,
I'm using Linq2Sql with pipes/filters. My database consists of Accounts and Transactions tables where
Is an IPC mechanism using shared memory and semaphores for synchronization simplex like pipes
I've been trying various methods (popen, pipes + fork/exec, ...) to read a child
I remember vaguely a technique using pipes for communication between two shell processes. Are

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.