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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:30:00+00:00 2026-06-17T18:30:00+00:00

I am learning the way to use ordinary pipeline in linux for the communication

  • 0

I am learning the way to use ordinary pipeline in linux for the communication between parent and child process. The basic task is just to send a message to the child process from parent process, and then the child do some conversion and pass the result back to the parent. My result shown is some random character like ���. I have been contemplating for a long while and still couldn’t figure out the bug. Thanks for your help.

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

#define READ_END 0
#define WRITE_END 1

void convert(char* str);

int main(int argc, char *argv[]){
  int pid; /* Process ID */
  int status;
  char *input;
  char *read_msg_c;
  char *read_msg_p;
  int pfd1[2], pfd2[2];
  if (argc !=2){/* argc should be 2 for correct execution */
    /* We print argv[0] assuming it is the program name */
    printf("Please provide the string for conversion \n");
    exit(-1);
  }
  input = argv[1];

  if(pipe(pfd1) < 0 || pipe(pfd2) < 0){
    printf("Failed to create a pipe between parent and child \n");
    exit(-1);
  }
  if((pid = fork()) < 0){ /* Fork the process */
     printf("Fork error \n");
     exit(-1);
  }
  else if(pid > 0){ /* Parent code */
    close(pfd1[READ_END]);
    close(pfd2[WRITE_END]);
    printf("Process ID of the parent is %d. \n", getpid()); /* Print parent's process ID */
    write(pfd1[WRITE_END],input,strlen(input)+1);
    close(pfd1[WRITE_END]);

    read(pfd2[READ_END],read_msg_p,strlen(input)+1);
    printf("%s\n",read_msg_p);
    close(pfd2[READ_END]);
  }
  else if(pid == 0){ /* Child code */
    close(pfd1[WRITE_END]);
    close(pfd2[READ_END]);

    printf("Process ID of the child is %d. \n", getpid()); /* Print child's process ID */
    read(pfd1[READ_END],read_msg_c, strlen(input)+1);
    printf("Child: Reversed the case of the received string. \n");
    write(pfd2[WRITE_END],read_msg_c,strlen(input)+1);
    close(pfd1[READ_END]);
    close(pfd2[WRITE_END]);
    exit(0); /* Child exits */
   }
}

void convert(char *str){
  int i = 0;
  while (str[i]){
    if (isupper(str[i])){
      str[i] = tolower(str[i]);
    }
    else if (islower(str[i])){
      str[i] = toupper(str[i]);
    }
    i++;
  }
}
  • 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-17T18:30:01+00:00Added an answer on June 17, 2026 at 6:30 pm

    Your primary bug is that your variables read_msg_p and read_msg_c are uninitialized pointers.

    Make them into arrays:

    char read_msg_p[1024];
    char read_msg_c[1024];
    

    You seem to be missing <stdio.h> (but you don’t really need <sys/types.h> any more). You should error check your reads and writes; your reads will probably use a different maximum size once you’ve allocated the space for them. Etc.

    I spotted the problem by looking at the compiler warnings:

    $ gcc -O3 -g -std=c99 -Wall -Wextra pipes-14420398.c -o pipes-14420398
    pipes-14420398.c: In function ‘main’:
    pipes-14420398.c:40:22: warning: ‘read_msg_p’ may be used uninitialized in this function [-Wuninitialized]
    pipes-14420398.c:52:22: warning: ‘read_msg_c’ may be used uninitialized in this function [-Wuninitialized]
    $
    

    Ignore the line numbers; I’d moderately seriously hacked your code by the time these were the only warnings left. But the lines in question are the read() calls.


    Example output form the hacked code, working correctly.

    $ ./pipes-14420398 string-to-convert
    Process ID of the parent is 37327. 
    Process ID of the child is 37328. 
    Child read 18 bytes: <<string-to-convert>>
    Parent read 18 bytes: <<string-to-convert>>
    $
    

    Note that the code below reads 18 bytes (including the null), but does not print the null (because of the nbytes-1 argument to printf().

    #include <unistd.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
    
    #define READ_END 0
    #define WRITE_END 1
    
    
    int main(int argc, char *argv[])
    {
      int pid; /* Process ID */
      char *input;
      char read_msg_c[1024];
      char read_msg_p[1024];
      int pfd1[2], pfd2[2];
    
      if (argc !=2){/* argc should be 2 for correct execution */
        /* We print argv[0] assuming it is the program name */
        fprintf(stderr, "Usage: %s string-to-convert\n", argv[0]);
        exit(-1);
      }
      input = argv[1];
    
      if(pipe(pfd1) < 0 || pipe(pfd2) < 0){
        printf("Failed to create a pipe between parent and child \n");
        exit(-1);
      }
      if((pid = fork()) < 0){ /* Fork the process */
         printf("Fork error \n");
         exit(-1);
      }
      else if(pid > 0){ /* Parent code */
        close(pfd1[READ_END]);
        close(pfd2[WRITE_END]);
        printf("Process ID of the parent is %d. \n", getpid()); /* Print parent's process ID */
        write(pfd1[WRITE_END], input, strlen(input)+1);
        close(pfd1[WRITE_END]);
    
        int nbytes = read(pfd2[READ_END], read_msg_p, sizeof(read_msg_p));
        if (nbytes <= 0)
            printf("Parent: read failed\n");
        else
            printf("Parent read %d bytes: <<%.*s>>\n", nbytes, nbytes-1, read_msg_p);
        close(pfd2[READ_END]);
      }
      else if(pid == 0){ /* Child code */
        close(pfd1[WRITE_END]);
        close(pfd2[READ_END]);
    
        printf("Process ID of the child is %d. \n", getpid()); /* Print child's process ID */
        int nbytes = read(pfd1[READ_END], read_msg_c, sizeof(read_msg_c));
        if (nbytes <= 0)
            printf("Child: read failed\n");
        else
        {
            printf("Child read %d bytes: <<%.*s>>\n", nbytes, nbytes-1, read_msg_c); 
            write(pfd2[WRITE_END], read_msg_c, nbytes);
        }
        close(pfd1[READ_END]);
        close(pfd2[WRITE_END]);
        exit(0); /* Child exits */
       }
    }
    

    As noted by WhozCraig, there are numerous other changes that could be made. This, however, gets things working reasonably cleanly. You were very close to OK.

    Note the debugging techniques:

    1. Compile with high warning levels and fix all warnings.
    2. Print information as it becomes available (or run in a debugger and observe the information as it becomes available).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am interested in learning an elegant way to use currying in a functional
I Just started learning ruby and I don't see the difference between an @instace_variable
Learning my way through this stuff and could use some help. I'm trying to
I am learning Designer and Sharepoint. What is the recommended way to use source
I'm learning to use WinDbg and I might be way off track on this,
I am learning to use <cfscript> . Is there any way I can log
Is there any way to use a php file in the parent folder. I
I am in a learning way of mysql and want help to know where
I've been learning my way through ggplot2, and I've made it to using polar
I'm on my way learning about PDO from phpro.org, and a little bit confuse.

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.