Solved

parent-child with pipe

Posted on 2011-03-17
4
318 Views
Last Modified: 2012-06-21
where is the problem in my code below?
/*
pipes2.c
*/
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>

int main(){
    int pfd[2];
    char buff[80];
    int chid;
    char* parent_message="hello my child!!\n";
    
    
    int result=pipe(pfd);
    if (result==-1){return -1;}
    
    chid=fork();
    if (chid==-1){return -1;}
    if (chid==0){
         char msg[80];
         //memset(msg,0,strlen(msg));
         read(pfd[0], msg, sizeof(msg));
         printf("   read: %s", msg );  
         exit (0);       
    }
    else{//parent
        write(pfd[1], parent_message, strlen(parent_message));
        printf("written: %s", parent_message);
        wait(NULL);
        }

    
    return 0;
}

Open in new window

I 'm taking some "garbage" at printf
0
Comment
Question by:dimi67
  • 2
4 Comments
 
LVL 10

Expert Comment

by:Martin_J_Parker
ID: 35158623
I just compiled that code on Redhat Enterprise 4 and it writes out:
written: hello my child!!
   read: hello my child!!

What system/compiler are you using?
0
 
LVL 32

Accepted Solution

by:
phoffric earned 500 total points
ID: 35158917
29:   write(pfd[1], parent_message, strlen(parent_message));

Notice that you are not including the terminating string null byte.

22:         char msg[80];  
23:         //memset(msg,0,strlen(msg));

Probably you meant sizeof since msg[80] is on the stack and has garbage in it.
Or, leave 23 commented out and use char msg[80] = ""; which initializes the entire msg to 0's.

But filling the array with 0's is unnecessary (and time-consuming) if the parent includes the terminating null byte. Without this null byte your printf writes the message but doesn't stop until it reaches a null byte (and if far enough out, you could get a segmentation fault).



0
 

Author Closing Comment

by:dimi67
ID: 35160384
yes, it's ok, now..
I'm using cygwin, but  why there is no problem to "native" linux?
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35160635
For a POD type auto variable on the stack, in absence of explicit initialization, they are said to have garbage in them. This means that whatever was in memory before is still there. It can vary from one compiler to another. In your case there weren't enough 0's lying around to act as a lucky null terminator.

(BTW - on an older SCO Unix system, the stack was often zero'd out, and this default behavior caused havoc when a new SCO compiler arrived due to poor programming practices of failing to properly initialize variables on the stack.)

If you do a man g++ on your cygwin compiler, you will find an enhanced warning switch:

=================
-Wuninitialized
    Warn if an automatic variable is used without first being initialized or if a variable may be clobbered by
    a "setjmp" call.

    These warnings are possible only in optimizing compilation, because they require data flow information that
    is computed only when optimizing.  If you do not specify -O, you will not get these warnings. Instead, GCC
    will issue a warning about -Wuninitialized requiring -O.

    If you want to warn about code which uses the uninitialized value of the variable in its own initializer,
    use the -Winit-self option.

    These warnings occur for individual uninitialized or clobbered elements of structure, union or array
    variables as well as for variables which are uninitialized or clobbered as a whole.  They do not occur for
    variables or elements declared "volatile".  Because these warnings depend on optimization, the exact
    variables or elements for which there are warnings will depend on the precise optimization options and
    version of GCC used.

    Note that there may be no warning about a variable that is used only to compute a value that itself is
    never used, because such computations may be deleted by data flow analysis before the warnings are printed.

    These warnings are made optional because GCC is not smart enough to see all the reasons why the code might
    be correct despite appearing to have an error.  Here is one example of how this can happen:

            {
              int x;
              switch (y)
                {
                case 1: x = 1;
                  break;
                case 2: x = 4;
                  break;
                case 3: x = 5;
                }
              foo (x);
            }

    If the value of "y" is always 1, 2 or 3, then "x" is always initialized, but GCC doesn't know this. ...

This warning is enabled by -Wall or -Wextra in optimizing compilations (-O1 and above).

=================

Bottom line is for you to always compile with the -Wall option to get as many warnings as possible and then correct them. This will not only help you get a working program faster, but should also improve portability.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now