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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T13:25:23+00:00 2026-05-29T13:25:23+00:00

I am trying to create a child that calls sort. The parent sends data

  • 0

I am trying to create a child that calls sort. The parent sends data to the child through a pipe. My code compiles and runs, but there is no output. What am I doing wrong? Am I not closing the pipes correctly, writing the pipes or outputting the data correctly?

[eddit] On my system I need to call /bin/sort NOT /usr/bin/sort!

#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <ctype.h>

int main(void){
int pipes[2];
pid_t pid;
FILE *stream;

if(pipe(pipes) == -1)
    printf("could not create pipe\n");

switch(fork()){
    case -1:
        fprintf(stderr, "error forking\n");
        break;
    case 0:
        dup2(pipes[0], STDIN_FILENO);

        pid = getpid();
        printf("in child, pid=%d\n");

        if(close(pipes[1]) == -1)
            fprintf(stderr,"err closing write end pid=%d\n", pid);

        execl("/usr/bin/sort", "sort",  (char*) NULL);
        break;
    default:
        stream = fdopen(pipes[1], "w");
        pid = getpid();
        printf("in parent, pid=%d\n", pid);

        if (stream == NULL)
            fprintf(stderr, "could not create file streami\n");

        if(close(pipes[0]) == -1)
            printf("err closing read end pid=%d\n");

                   fputs("bob\n",stream);
        fputs("cat\n",stream);
        fputs("ace\n",stream);
        fputs("dog\n",stream);

        if(fclose(stream) == EOF)
            fprintf(stderr, "error while closing stream\n");
        break;
    }
    return 0;
}

[edit] Here is my working code. Thank you everyone

#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main(void){
int pipes[2];
pid_t pid;
FILE *stream;

int stat;
if(pipe(pipes) == -1)
    printf("could not create pipe\n");

switch(fork()){
    case -1:
        fprintf(stderr, "error forking\n");
        break;
    case 0:
        dup2(pipes[0], STDIN_FILENO);

        pid = getpid();
        printf("in child, pid=%d\n", pid);

        if(close(pipes[1]) == -1)
            fprintf(stderr,"err closing write end pid=%d\n", pid);

        if(close(pipes[0]) == -1)
            fprintf(stderr,"err closing write end pid=%d\n", pid);

        execl("/bin/sort", "sort",  (char*) NULL);
        exit(EXIT_FAILURE);
        break;
    default:
        stream = fdopen(pipes[1], "w");
        pid = getpid();
        printf("in parent, pid=%d\n", pid);

        if (stream == NULL)
            fprintf(stderr, "could not create file streami\n");

        if(close(pipes[0]) == -1)
            printf("err closing read end pid=%d\n");

        fputs("bob\n",stream);
        fputs("cat\n",stream);
        fputs("ace\n",stream);
        fputs("dog\n",stream);

        if(fclose(stream) == EOF)
            fprintf(stderr, "error while closing stream\n");
        break;
}

wait(&stat);
return 0;
}   
  • 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-29T13:25:24+00:00Added an answer on May 29, 2026 at 1:25 pm

    You most certainly do not have enough close() calls in the code, which will lock the processes up.

    Pseudo code:

    Create pipe
    Fork
    In parent:
        Close read end of pipe
        Write data to be sorted down write end of pipe
        Close write end of pipe
        Wait for child to die
    In child
        Close write end of pipe
        Duplicate read end of pipe to stdin
        Close read end of pipe
        Exec the sort program
        Exit with an error if the exec returns
    

    Note that the pseudo code ends up closing all four ends of the pipe – the two in the parent and the two in the child. If you don’t do that, you will run into deadlock.

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

Sidebar

Related Questions

I'm trying to create a child form that helps the user to enter data
I'm trying to create a function that searches up a WebControl's parent-child relationship (basically
I am trying to create Linq Expression that I can use to query child
I've been trying to use the pipe() system call to create a shell that
I am trying to take a stored procedure that copies parent/child/grandchild rows into the
I'm trying to create a child process in another process. I am writing both
I am trying create a WCF service that leverages the WPF MediaPlayer on the
Trying to create a user account in a test. But getting a Object reference
I'm trying to create a child class of TForm with a special constructor for
I am trying to create a custom jQuery selector that will put focus() on

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.