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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:23:05+00:00 2026-06-14T02:23:05+00:00

I was practicing pipes in system programming when i realized that my program isn’t

  • 0

I was practicing pipes in system programming when i realized that my program isn’t exiting. I added exit() in both child and parent, but the child still isn’t exiting. Please help…
Here is the code:

#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
//#include "apue.h"

main() {
        int n,max=20;
        pid_t pid;
        int fd[2];
        char line[max];
        int i;
        for(i=0;i<20;i++) {
            line[i]='\0';
        }

        if(pipe(fd)<0) {
            perror("pipe error");
        }
        if((pid=fork())<0) {
            perror("fork error");
        }
        else if(pid > 0) {
            close(fd[0]);
            write(fd[1], "hello world\n", 12);
            exit(1);
        } else {
            close(fd[1]);
            read(fd[0], line, max);
        }
        puts(line);
        exit(1);
}
  • 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-14T02:23:07+00:00Added an answer on June 14, 2026 at 2:23 am

    First of all, fork returns 0 in the child not in the parrent. So, when you write

    else if(pid > 0) {

           close(fd[0]);
           write(fd[1], "hello world\n", 12);
            exit(1); }
    

    You are in the parrent process. To be in the child process space, you shoud use else if(pid **==** 0)

    The seccond thing you should do to make sure everything works fine, you should not call in the child process code space the function exit(). You would better wait your child process in the parrent process. For this you should use the wait() function in the parrent process.

    The good code would be:

    main() {
        int n,max=20;
        pid_t pid;
        int fd[2];
        char line[max];
        int i;
        int status;
        for(i=0;i<20;i++) {
            line[i]='\0';
        }
            if(pipe(fd)<0) {
            perror("pipe error");
        }
        pid=fork();
        if(pid <0) {
            perror("fork error");
        }
        else if(pid == 0) { // Here is the child process
            close(fd[0]);
            write(fd[1], "hello world\n", 12);
            **// Do not kill child process because is dangeorus to do this when you use pipes**
        } else { // Parrent process
            close(fd[1]);
            read(fd[0], line, max);
            puts(line);
    
            wait(&status);      // Wait the child process to end its job
    
        }
    
      return 0;
    

    }

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

Sidebar

Related Questions

I'm practicing my programming skills on an app that has a pivot page with
I was practicing some programming problems and tried to code the popular reverse words
I am practicing inheritance. I have two similar classes that I'd like to assimilate
I'm practicing writing MVC applications. I have a Mastermind game, that I would like
practicing programming with MyProgrammingLab and getting following compile error: ApartmentBuilding.java:4: error: expected its also
Been practicing with those system calls, but I stucked into this code: #include <stdio.h>
I'm practicing programming in Assembly, making code C. I don't understand the conversion of
While practicing a tutorial on GTK+ I have encountered sample code that looks like
I was practicing the dynamic programming problem on SPOJ. But I have no idea
I've been practicing certain techniques in XNA and lately came across an issue that's

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.