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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:36:54+00:00 2026-05-24T04:36:54+00:00

I’m trying to write code that spawns two child processes that send each other

  • 0

I’m trying to write code that spawns two child processes that send each other a message over a pipe then terminate. However, when I run the following code only child2 prints it’s greeting but child 1 still prints the message it gets from child 2 where child 1 doesn’t.

Does anybody know what’s wrong with my methodology?

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

int main(int argc, char** argv) {
    char chld_1_send[20] = "hello from child 1";
    char chld_1_recv[20];
    char chld_2_send[20] = "hi from child 2";
    char chld_2_recv[20];

    int chld_1_outgoing[2];
    int chld_2_outgoing[2];

    pipe(chld_1_outgoing);
    pipe(chld_2_outgoing);

    int chld_1_status, chld_2_status;
    pid_t chld_1, chld_2;

    chld_1 = fork();

    if(chld_1 == 0) {
        chld_2 = fork();
    }

    if(chld_1 == 0 && chld_2 == 0) {
        printf("parent [pid:%d] waiting on both children to finish\n", getpid());

        while(wait(&chld_1_status) != chld_1 && wait(&chld_2_status) != chld_2) {}

        printf("done waiting\n");
    }
    else if(chld_1 != 0 && chld_2 == 0) {
        printf("this is child 1 [pid:%d] with parent [pid:%d]\n", getpid(), getppid());

        write(chld_1_outgoing[1], chld_1_send, strlen(chld_1_send));
        while(read(chld_1_outgoing[0], &chld_1_recv, sizeof(chld_2_recv)) < 0) {}

        printf("child 2 said '%s'\n", chld_1_recv);
        exit(0);
    }
    else if(chld_2 != 0 && chld_1 == 0) {
        printf("this is child 2 [pid:%d] with parent [pid:%d]\n", getpid(), getppid());

        write(chld_2_outgoing[1], chld_2_send, strlen(chld_2_send));
        while(read(chld_2_outgoing[0], &chld_2_recv, sizeof(chld_2_recv)) < 0) {}

        printf("child 1 said '%s'\n", chld_2_recv);

        exit(0);
    }

    printf("both children have terminated successfully\n");

    return 0;
}

However, running this command prints out this to the terminal and goes into an infinite loop:

$ this is child 2 [pid:15713] with parent [pid:1]
child 1 said 'hi from child 2'
parent [pid:15714] waiting on both children to finish
  • 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-24T04:36:56+00:00Added an answer on May 24, 2026 at 4:36 am

    Following is a simple example which certainly can be improved but should get you started.
    Also follow Link1 and Link2 for more info.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/wait.h>
    #include <unistd.h>
    
    /* max  receiving buffer size; Note: no check or enforcement is made on this value*/
    #define BUF_SIZE 256
    
    int main()
    {
        int pfd1[2];
        int pfd2[2];
    
        ssize_t numRead = -1;
        /* Note: working under the assumption that the messages
           are of equal length*/
        const char* messageOne = "Hello from child ONE.\n";
        const char* messageTwo = "Hello from child TWO.\n";
    
        const unsigned int commLen = strlen(messageOne) + 1;
    
        char buf[BUF_SIZE];
    
        if (pipe(pfd1) == -1)
        {
            printf("Error opening pipe 1!\n");
            exit(1);
        }
    
        if (pipe(pfd2) == -1)
        {
            printf("Error opening pipe 2!\n");
            exit(1);
        }
    
        printf("Piped opened with success. Forking ...\n");
    
        // child 1
        switch (fork())
        {
            case -1:
                printf("Error forking child 1!\n");
                exit(1);
    
            case 0:
                printf("\nChild 1 executing...\n");
                /* close reading end of first pipe */
                if (close(pfd1[0]) == -1)
                {
                    printf("Error closing reading end of pipe 1.\n");
                    _exit(1);
                }
                /* close writing end of second pipe */
                if (close(pfd2[1]) == -1)
                {
                    printf("Error closing writing end of pipe 2.\n");
                    _exit(1);
                }
    
                /* write to pipe 1 */
                if (write(pfd1[1], messageOne, commLen) != commLen)
                {
                    printf("Error writing to pipe 1.\n");
                    _exit(1);
                }
    
                if (close(pfd1[1]) == -1)
                {
                    printf("Error closing writing end of pipe 1.\n");
                    _exit(1);
                }
    
                /* reding from pipe 2 */
                numRead = read(pfd2[0], buf, commLen);
                if (numRead == -1)
                {
                    printf("Error reading from pipe 2.\n");
                    _exit(1);
                }
    
                if (close(pfd2[0]) == -1)
                {
                    printf("Error closing reding end of pipe 2.\n");
                    _exit(1);
                }
    
                printf("Message received child ONE: %s", buf);
                printf("Exiting child 1...\n");
                _exit(0);
    
            default:
                break;
        }
    
        // child 2
        switch (fork())
        {
            case -1:
                printf("Error forking child 2!\n");
                exit(1);
            case 0:
                printf("\nChild 2 executing...\n");
                /* close reading end of second pipe */
                if (close(pfd2[0]) == -1)
                {
                    printf("Error closing reading end of pipe 2.\n");
                    _exit(1);
                }
                /* close writing end of first pipe */
                if (close(pfd1[1]) == -1)
                {
                    printf("Error closing writing end of pipe 1.\n");
                    _exit(1);
                }
    
                /* read from the first pipe */
                if (read(pfd1[0], buf, commLen) == -1)
                {
                    printf("Error reading from pipe 1.\n");
                    _exit(EXIT_FAILURE);
                }
    
                if (close(pfd1[0]) == -1)
                {
                    printf("Error closing reading end of pipe 1.\n");
                    _exit(EXIT_FAILURE);
                }
    
                /* write to the second pipe */
                if (write(pfd2[1], messageTwo, commLen) != commLen)
                {
                    printf("Error writing to the pipe.");
                    _exit(EXIT_FAILURE);
                }
    
                if (close(pfd2[1]) == -1)
                {
                    printf("Error closing writing end of pipe 2.");
                    _exit(EXIT_FAILURE);
                }
    
                printf("Message received child TWO: %s", buf);
                printf("Exiting child 2...\n");
                _exit(EXIT_SUCCESS);
    
            default:
                break;
        }
    
        printf("Parent closing pipes.\n");
    
        if (close(pfd1[0]) == -1)
        {
            printf("Error closing reading end of the pipe.\n");
            exit(EXIT_FAILURE);
        }
    
        if (close(pfd2[1]) == -1)
        {
            printf("Error closing writing end of the pipe.\n");
            exit(EXIT_FAILURE);
        }
    
        if (close(pfd2[0]) == -1)
        {
            printf("Error closing reading end of the pipe.\n");
            exit(EXIT_FAILURE);
        }
    
        if (close(pfd1[1]) == -1)
        {
            printf("Error closing writing end of the pipe.\n");
            exit(EXIT_FAILURE);
        }
    
        printf("Parent waiting for children completion...\n");
        if (wait(NULL) == -1)
        {
            printf("Error waiting.\n");
            exit(EXIT_FAILURE);
        }
    
        if (wait(NULL) == -1)
        {
            printf("Error waiting.\n");
            exit(EXIT_FAILURE);
        }
    
        printf("Parent finishing.\n");
        exit(EXIT_SUCCESS);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.