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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:24:08+00:00 2026-05-27T17:24:08+00:00

Basically I want to do in C (and without buffering) the same as this

  • 0

Basically I want to do in C (and without buffering) the same as this bash-script:

#!/bin/sh
cat ./fifo_in | myprogram > ./fifo_out

In other words I want to exec “myprogram” and redirect its stdin and stdout to two pipes which have been created previously.

Another program is feeding data into fifo_in and reading out of fifo_out.

Of course it would be easy to just read from ./fifo_in, buffer it in the parent and write to myprogram’s stdin (and reverse for stdout and ./fifo_out) but I think there is probably a way to let “myprogram” read/write directly from/to the fifos without buffering in the parent process.

Edit:

Eugen’s answer seems to be the correct one, but I cannot get it to work.

I use this function on the C-side, which seems correct to me:

pid_t execpipes(const char *wd, const char *command, const char *pipename)
{
char pipename_in[FALK_NAMESIZE];
char pipename_out[FALK_NAMESIZE];
strcpy(pipename_in, FALKPATH);
strcat(pipename_in, "/");
strcat(pipename_in, FALK_FIFO_PATH);
strcat(pipename_in, "/");
strncat(pipename_in, pipename, FALK_NAMESIZE-2);
strcpy(pipename_out, pipename_in);
strcat(pipename_out, "R");

pid_t pid;
pid = fork();
if (pid < 0)
{   //Error occured
    perror("fork");
    exit(1);
}
if (pid == 0)
{
    chdir(wd);
    d("execpipes: pipename_in=\"%s\"\n", pipename_in);
    d("          pipename_out=\"%s\"\n", pipename_out);
    freopen(pipename_in,"r",stdin);
    freopen(pipename_out,"w",stdout);

    d("execpipes: command=\"%s\"\n", command);

    execl("/bin/sh", "sh", "-c", command, (char *)NULL); // using execv is probably faster
    // Should never get here
    perror("execl");
    exit(1);
}
return pid;
}

I read and write the pipes from a PHP-script (only relevant part posted):

$pipe_in = fopen($fp.$pipename, "w");
$DEBUG .= "Write to pipe_in\n";
$ret = fwrite($pipe_in, $in);

$pipe_out = fopen($fp.$pipename.'R', "r");
$DEBUG .= "Read from pipe_out\n";
$atext = fread($pipe_out, 200000);  // Program hangs here

The program is started correctly, receives the input via $pipe_in correctly, processes the data correctly and (because it ran fine for many months) I assume it puts out the data correctly to stdout, but when I try to read from $pipe_out, it hangs. I know that the pipes themselves are set up correctly because if I don’t open $pipe_out, the program does not get any input – which makes sense because there is no reader for $pipe_out and therefore the pipeline is not complete. So I can open $pipe_out, but I cannot read anything from it, which is quite strange.

Edit2:

Program works now, thanks guys – For some reason the first pipe has to be closed before you can read from the second pipe:

$pipe_in = fopen($fp.$pipename, "w");
$pipe_out = fopen($fp.$pipename.'R', "r");
$DEBUG .= "Write to pipe_in\n";
$ret = fwrite($pipe_in, $in);
fclose($pipe_in);

$DEBUG .= "Read from pipe_out\n";
$atext = fread($pipe_out, 200000);
fclose($pipe_out);

unlink($fp.$pipename);
unlink($fp.$pipename.'R');
  • 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-27T17:24:09+00:00Added an answer on May 27, 2026 at 5:24 pm

    I’d write a small wrapper for myprogram, that does

    freopen("./fifo_in","r",stdin)
    freopen("./fifo_out","w",stdout)
    

    (Ofcourse not with constant paths!), then execve myprogram

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

Sidebar

Related Questions

OK, I'm struggling to make this work but without success. Basically I want to
Basically, I want to do zipzam&&&?&&&?&&& -> zipzam%26%26%26?&&&?&&&. I can do that without regex
I basically want to do this: grep 'example.com' www_log > example.com.YYYY-MM-DD-H:i:S.log ...with of course
I basically want to do this in code: PersonList myPersonList; //populate myPersonList here, not
I basically want to be able to deploy multiple versions of the same EAR
How can I call a web service directly without using soap? I basically want
Having some trouble getting this to work... I basically want the report to look
I was writing this small script the other day.It is a small program which
I basically want to do this: <% @videos.each do |vid| %> <div id=vid.location> ...
I basically want to show a UITableViewCellAccessoryDisclosureIndicator depending on certain situations, in this case,

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.