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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:24:16+00:00 2026-05-18T08:24:16+00:00

I’m writing a little program, and here is what it should do. In the

  • 0

I’m writing a little program, and here is what it should do.

In the main process I have to create a new one and that one should execute another program which only does a printf(“text”). I want to redirect the pipe write end on stdout and the main process should read from its pipe read and and print it on stdout. I wrote the code but again and again I get a segmentation fault when the parent process tries to read from the pipe.

#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

void write_to(FILE *f){
  char buf[50];
  fprintf(f,"KOMA");
}

int main(){
  int cpPipe[2];
  int child1_fd;
  int child2_fd;

  if(pipe(cpPipe) == -1){

    fprintf(stderr,"ERROR PIPE creation");
    exit(1);

  }else{printf("pipe couldn't be created\n");}

  child1_fd = fork();

  if(child1_fd < 0){
    fprintf(stderr, " CHILD creation error");
    exit(1);
  }

  if(child1_fd == 0){
    printf("*CHILD*\n");
    char program[] = "./Damn";
    int dupK;
    printf("stdout %d \n", STDOUT_FILENO);
    printf("stdin %d \n", STDIN_FILENO);
    printf("pipe1 %d \n", cpPipe[1]);
    printf("pipe0 %d \n", cpPipe[0]);

    // closing pipe write
    close(cpPipe[0]);
    close(1);
    dup(cpPipe[1]);

    printf("and");

    close(cpPipe[1]);
    exit(0);
  }else{
    printf("*Parent*\n");
    char *p;
    char *buf;
    FILE *pipe_read;

    close(cpPipe[1]);
    pipe_read = fdopen(cpPipe[0],"r");

    while((buf = fgets(p,30,pipe_read)) != NULL){
      printf("buf %s \n", buf);
    }

    wait();
    printf("Child is done\n");
    fclose(pipe_read);

    exit(0);
  }
}

Do I have to close the pipe write end when I redirect stdout to it?

  • 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-18T08:24:16+00:00Added an answer on May 18, 2026 at 8:24 am

    Uhm,… the reason for your segmentation fault is here:

    buf = fgets(p,30,pipe_read);
    

    p is a pointer to essentially nowhere of importance. It’s content is whatever is in the stack at the time of execution, you never initialize it. You need it to point to a chunk of memory you can use! Assign the return of a malloc() call to it, or declare it as char p[LEN].

    Edit: you are also reopening already open file descriptors. Check the documentation on fgets and pipe, I think you are confused as to how they work.

    Now, that said, the flow of your function is kinda confusing. Try working on clarifying it! Remember, code is meant to express intentions, ideas of functionality. Try using pencil and paper to organize your program, and then write it as actual code :).

    Cheers!

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put

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.