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

  • Home
  • SEARCH
  • 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 8239617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:12:33+00:00 2026-06-07T20:12:33+00:00

Given this code : #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main(void) { int

  • 0

Given this code :

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

int main(void)
{
        int     fd[2], nbytes;
        pid_t   childpid;
        char    string[] = "Hello, world! I'm the son and this my message!\n";
        char    readbuffer[80];

        pipe(fd);   // piping fd[0] & fd[1]

        if((childpid = fork()) == -1)   // here we create a SON process
        {
                perror("fork");
                exit(1);
        }

        if(childpid == 0)    // child process
        {
                /* Child process closes up input side of pipe */
                close(fd[0]);       // closing the READ end from reading , after that the SON would write into fd[1]

                /* Send "string" through the output side of pipe */
                write(fd[1], string, (strlen(string)+1));
                printf("Verification : Message was sent successfully by the SON!\n");
                exit(0);
        }
        else    // father process
        {
                /* Parent process closes up output side of pipe */
                close(fd[1]);

                /* Read in a string from the pipe */
                nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
                printf("I'm the father and I received that string: %s", readbuffer);
        }

        return(0);
}

The output is :

I'm the father and I received that string: Hello, world! I'm the son and this my message!
Verification : Message was sent successfully by the SON!

I’m trying to understand pipes , and few things are not clear to me :

  1. If the son sends his message in that line write(fd[1], string, (strlen(string)+1)); and after that we have the printf that verifies that the message was sent, why am I getting the verification (e.g. Verification : Message was sent successfully by the SON!
    ) after the father received the message from the son ? wasn’t it suppose to be first the verification from the son and only then the string ?

  2. If the father tries to read from the pipe and the son want to write to the pipe , somewhere here hides (I think) a deadlock , doesn’t ? why am I not getting a deadlock ?

Thanks

  • 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-07T20:12:36+00:00Added an answer on June 7, 2026 at 8:12 pm

    1) The reason the message from the child process comes later is because writing to a pipe can block until there is sufficient space in the buffer (from here):

    If a process attempts to write to a full pipe (see
    below), then write(2) blocks until sufficient data has been read from the pipe
    to allow the write to complete.

    So in other words the child process waits for the parent process to read the message within the call to write().

    2) If the child process fails to write anything to the pipe, then yes the parent will block (it won’t deadlock as such).

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

Sidebar

Related Questions

I have a program that has this code : #include<stdio.h> main(){ int input; char
I came across this code: #include<stdio.h> void main() { int x; float t; scanf(%f,&t);
Given this C code compiled with gcc 4.3.3 #include <stdio.h> #include <stdlib.h> int main(int
Given this code : #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/wait.h> #include <unistd.h>
Given this code : #include <stdio.h> #include <assert.h> void print_number(int* somePtr) { assert (somePtr!=NULL);
I have this code: #include <stdio.h> #include <openssl/sha.h> #include <openssl/ssl.h> int main(){ printf(OpenSSL version:
This is my code: #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <pthread.h> int num_mezzo_1(int
In the code given below: #include <stdio.h> #include <fcntl.h> #include <unistd.h> #include <stdlib.h> #include
#include <stdio.h> #include <stdlib.h> void badf(int n, char c, char* buffer) { int i;
Consider the following code: #include <stdio.h> void main() { uint32_t num = 2; __asm__

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.