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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:21:56+00:00 2026-06-13T23:21:56+00:00

Recently I’ve taken up learning pipes for fun. I’ve gotten stuck on a few

  • 0

Recently I’ve taken up learning pipes for fun. I’ve gotten stuck on a few parts but I have it mostly figured out I think, however I cannot figure out how to get input to forward to a program and output from that program at the same time.

Currently I have this Perl script that handles the piping:

#!/usr/bin/perl
use strict;
use warnings;
use threads;
use FileHandle;
use IPC::Open2;

my $cv_program = "./test"; #test is the compiled C program below
my $cv_message = "";
my $cv_currentkey = "";
my $pid = open2(*PIN, *POUT, $cv_program);
my $thread_pipeout = threads->create('PIPEOUT', \&PIN);

$thread_pipeout->detach();

while($cv_currentkey ne "\n")
{
    $cv_currentkey = getc(STDIN);
    $cv_message .= $cv_currentkey;
}
print POUT $cv_message;

sub PIPEOUT
{
    my $PIN = shift;
    while(<PIN>)
    {
        print $_;
    }
}

And then I have this C program which just outputs something, asks for a string, then prints that string:

#include <stdio.h>

int main(int argc, char const *argv[])
{
    char input[100] = {0};

    printf("This is a test.\n");
    fgets(input, 100, stdin);
    printf("You entered %s\n", input);
    return 0;
}

The output from running the Perl script is:

~/Programming/Perl Pipes$ ./pipe.pl
Hello
This is a test.
You entered Hello

Notice it blocks while taking input and then prints everything in a block after. I need it to print This is a test, then wait for input like the actual program would.

Also I’ll note is the reason I use getc instead of STDIN in the Perl script is because I couldn’t find a way to get STDIN to not block the output from test.c, however getc isn’t working too well at the moment either.

  • 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-13T23:21:57+00:00Added an answer on June 13, 2026 at 11:21 pm

    I think the problem is mostly on the C end rather than the Perl end. C sees that its standard-output is going to a pipe, so it isn’t very punctilious about making sure it flushes its standard-output buffer before querying standard-input.

    To fix this, you should be able to just add this line:

    fflush(stdout);
    

    after the first printf and before the fgets.

    (Disclaimer: Not tested.)

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

Sidebar

Related Questions

Recently I have released my app into the US AppStore. Now I Planed for
Recently I have done an assignment, on overloading basic functionalists(+,-,conjugate...) of complex class using
Recently, I have followed a discussion about assignments to expressions in C++ as shown
Recently i am making an app on facebook. So i have use facebook api.
Recently,I am handling a solution for WebPart internationalization,but I am not familiar with the
Recently I've been doing quite the project mostly working with the DateTime class. Now,..
Recently, I'm trying to solve all the exercises in CLRS. but there are some
Recently I have been dealing with windows LogonUser API. The LogonUser api returns different
Recently we have released our product. Our team decided to preserve the source code
Recently learning Android, there is a Handler class in android which is very useful.

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.