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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:22:42+00:00 2026-05-27T05:22:42+00:00

I have a program that fork() s a new process and then overwrites that

  • 0

I have a program that fork()s a new process and then overwrites that new process’s stdin with my file descriptor pipe fd. In this process, I then use execvp() to execute wc and it should read its input from the parent.
In the parent, I then write to the write end of the pipe and when done writing, close the pipe.

The problem is that wc is still expecting input and des not exit.
Usually, I can stop wc by typing CTRLD but I can’t seem to send that signal in C/C++.

How do I tell wc to stop reading?

EDIT: I did follow the pipe/fork/dup/exec idiom (I think that’s what it’s called.)
The streaming works with other programs that need input, its just that wc needs the special EOF to stop reading.

int in_fd[2];
//parent-child setup
pipe(in_fd);
child = fork();
if(child == 0) {
    close(in_fd[1]);
    dup2(in_fd[0], 0);
    close(in_fd[0]);
    execvp(cmdArg[0], cmdArg);
} else {
    close(in_fd[0]);
    in_pid = fork_in_proc();
    close(in_fd[1]);
}

//writing function
pid_t fork_in_proc() {
    string line;
    pid_t in_pid;
    if((in_pid = fork()) == 0) {
        close(in_fd[0]);
        ifstream file(stream_file[STREAM_IN].c_str(), ios_base::in);
        if (file.bad()) {
            cerr << "File read error\n";
            return 1;
        }

        while(file.good()) {
            getline(file, line);
            if(file.good()) {
                write(in_fd[1], line.c_str(), line.length());
            }
        }
        int end = 3;
        write(in_fd[1], &end, sizeof(end));
        file.close();
        close(in_fd[1]);
        cout << "PIPE IN" << endl;
        exit(0);
    } else {
        return in_pid;
    }
}

Sorry if the code seems a little disjointed. I had to pull it together from around the file.

  • 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-27T05:22:42+00:00Added an answer on May 27, 2026 at 5:22 am

    wc exits when you close the stdout.

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

Sidebar

Related Questions

I have to write program that create process using pipe() . My first task
I have this program in C++ that forks two new processes: #include <pthread.h> #include
I wrote a little program using fork to create new processes which use pipe
Say I want a have a C program that does this: 1. User will
I have a program that writes to a FILE *cgiOut and just after it
I have a program with a parent and a child process. Before the fork(),
How to solve this using scala Actors: I have a program that finds out
I am trying to have a program that uses multiple forks. I used this
I have wrote a server program that can handle multiple client by using fork().
Hi I have a program that requires a child process to modify a link

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.