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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:31:13+00:00 2026-05-31T07:31:13+00:00

I am writing a program that will eventually be used to have one child

  • 0

I am writing a program that will eventually be used to have one child process send randomly generated characters through a pipe to another child process to convert to uppercase values and output, but before I get that far, I am trying to create the child processes and make some expected output. I have written the following application:

#include <stdio.h> /* printf, stderr, fprintf */
#include <sys/types.h> /* pid_t */
#include <unistd.h> /* fork */
#include <stdlib.h> /* _exit */
#include <errno.h> /* errno */

int main()
{
    pid_t writer;
    pid_t reader;

    writer = fork();
    if (writer == -1)
    {
        fprintf(stderr, "can't fork, error %d\n", errno);
        exit(1);
    }

    if (writer == 0)
    {
        printf("Writer process created.\n");
        reader = fork();
        if (reader == -1)
        {
            fprintf(stderr, "can't fork, error %d\n", errno);
            exit(1);
        }

        if (reader == 0)
        {
            printf("Reader process created.\n");
            kill(reader);
            printf("Reader was successfully murdered.\n");
        }
        kill(writer);
    }
    wait();
    printf("Writer killed.\n");
    return 0;
}

I would ideally like the output to come out as the following:

Writer process created.
Reader process created.
Reader was successfully murdered.
Writer killed.

But as of this point, it outputs:

Writer process created.
Reader process created.
Reader was successfully murdered.
Writer killed.
Writer killed.
Writer killed.

It is as if it is going through the entire iteration of the code for the parent process, and two child processes from the point that they are created, which also leads me to believe that they are not being killed at the appropriate time that I want them to. Could someone point me in the right direction?

  • 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-31T07:31:14+00:00Added an answer on May 31, 2026 at 7:31 am

    Here is your full working example with pipes (they also synchronize processes, so you do not have to kill them). Bear in mind that reader process is reading very inefficiently (by one character – I’ll leave it as an exercise to you :))

    #include <stdio.h> /* printf, stderr, fprintf */
    #include <sys/types.h> /* pid_t */
    #include <unistd.h> /* fork */
    #include <stdlib.h> /* _exit */
    #include <errno.h> /* errno */
    #include <string.h> /* strlen */
    
    int main()
    {
        pid_t writer;
        pid_t reader;
        char buf;
        int pipefd[2] = {-1,-1};
        const char* pcszTextToChild = "Text sent to child";
    
        writer = fork();
        if (writer == -1)
        {
            fprintf(stderr, "can't fork, error %d\n", errno);
            exit(1);
        }
    
        if (writer == 0)
        {/* writer process */
            printf("Writer process created.\n");
            if(pipe(pipefd) == -1)
            {
               perror("pipe");
               exit(EXIT_FAILURE);
            }
            reader = fork();
            if (reader == -1)
            {
                fprintf(stderr, "can't fork, error %d\n", errno);
                exit(1);
            }
    
            if (reader == 0)
            {/* reader process */
                close(pipefd[1]);  /* Close unused write end */
                printf("Reader process created.\n\tReading: ");
                fflush(stdout);
                while (read(pipefd[0], &buf, 1) > 0)
                    write(STDOUT_FILENO, &buf, 1);
                write(STDOUT_FILENO, "\n", 1);
                close(pipefd[0]);
                printf("Exiting reader process.\n");
                exit(EXIT_SUCCESS);
            }
            else
            {/* writer process */
                close(pipefd[0]);          /* Close unused read end */
                write(pipefd[1], pcszTextToChild, strlen(pcszTextToChild));
                close(pipefd[1]);          /* Reader will see EOF */
                wait(NULL);                /* Wait for child */
                printf("Exiting writer process.\n");
                exit(EXIT_SUCCESS);
            }
        }
        wait();
        return 0;
    }
    

    Result:

    Writer process created.
    Reader process created.
        Reading: Text sent to child
    Exiting reader process.
    Exiting writer process.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a writing a program that will determine the number of lines, characters, and
I'm writing a program that sends an array through a function (upperhess) that will
I'm writing a program that will continuously process files placed into a hot folder.
I'm writing a program that will have both a server side and a client
I've been writing a C++ program that will eventually remove certain undesired formatting from
Since I am writing a program that will eventually run on Windows and Linux
I am writing a program that will read and write characters, converting lowercase characters
I am contemplating writing a program that will move some newly created dirs to
I'm writing a simple program that will run entirely client-side. (Desktop programming? do people
I am writing an HTML program that will be accessed by over 500 people

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.