Thread: IPC using PIPES

  1. #16
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by BMathis View Post
    seems to work so far
    Looks good to me.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #17
    Registered User
    Join Date
    Feb 2009
    Posts
    20
    Quote Originally Posted by brewbuck View Post
    You don't "send" EOF. You just close your end of the pipe, and the OS itself will generate EOF. The situation is different if the reading end of the pipe gets closed -- that doesn't generate EOF to the writer, instead it causes a SIGPIPE to be delivered, which by default will crash your program. You should design a real protocol with true shutdown negotiation to avoid this problem.
    That is originally what I wanted to do. In my parent I
    close(commpipe[1]);
    when I am done writing into the pipe. Is that EOF sent through the pipe just before it is closed?
    If that is the case, my child is not seeing the EOF. It continues to sit there and wait for input till I shove an EOF through the pipe myself using printf().

  3. #18
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by BMathis View Post
    That is originally what I wanted to do. In my parent I
    close(commpipe[1]);
    when I am done writing into the pipe. Is that EOF sent through the pipe just before it is closed?
    If that is the case, my child is not seeing the EOF. It continues to sit there and wait for input till I shove an EOF through the pipe myself using printf().
    Well, I can't explain that. Closing the write end of the pipe should cause the read end to receive an EOF. I haven't really looked at your code though.

    Also, EOF doesn't get sent through the pipe. It's not a character. The OS just causes the next read() on the pipe to return 0, which means "EOF".
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #19
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by brewbuck View Post
    Well, I can't explain that. Closing the write end of the pipe should cause the read end to receive an EOF. I haven't really looked at your code though.
    This was probably because fgets won't catch EOF, and the while (!feof(stdin)) was ineffective (I've never tried this method but I've seen some caveats against it).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #20
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by MK27 View Post
    This was probably because fgets won't catch EOF, and the while (!feof(stdin)) was ineffective (I've never tried this method but I've seen some caveats against it).
    It won't? I'm pretty sure that fgets() will return NULL if it receives EOF before any character has been read.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #21
    Registered User
    Join Date
    Feb 2009
    Posts
    20
    would it make more sense for the child to be using read() to read in from the pipe rather than fgets()

  7. #22
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by BMathis View Post
    would it make more sense for the child to be using read() to read in from the pipe rather than fgets()
    read() is great because it just needs a file descriptor but you should use fread(), because fread() is more portable and so will work everywhere
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #23
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by brewbuck View Post
    It won't? I'm pretty sure that fgets() will return NULL if it receives EOF before any character has been read.
    I don't use fgets(), but looking at the GNU reference there is no mention of that, and based on the OP's observations it really must not...

    But what happens if it reads the EOF after the first character? Would the EOF be gone so the next fgets just hangs?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #24
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by MK27 View Post
    I don't use fgets(), but looking at the GNU reference there is no mention of that, and based on the OP's observations it really must not...

    But what happens if it reads the EOF after the first character? Would the EOF be gone so the next fgets just hangs?
    you are talking as if EOF is some character.
    EOF is condition "no more characters to read", and this condition goes nowhere, no matter how many tries you do to read from the closed stream
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #25
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by vart View Post
    you are talking as if EOF is some character.
    EOF is condition "no more characters to read", and this condition goes nowhere, no matter how many tries you do to read from the closed stream
    Hmmm, I wonder what this is?
    Code:
    #include <stdio.h>
    
    int main() {
         printf("%x\n",EOF);
         return 0;
    }
    My imagination?

    Again: I don't use fgets, I was going by what the OP claimed. And NO ONE has actually contributed a working piece of code to demonstrate the OP wrong, so s/he is probably right.
    Last edited by MK27; 03-14-2009 at 11:47 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #26
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by MK27 View Post
    Hmmm, I wonder what this is?
    My imagination?
    .
    This is a macro used by SOME functions to indicate the EOF condition. It is NOT the character read from the stream
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #27
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MK27
    Hmmm, I wonder what this is?
    A program that prints a negative integer in hexadecimal representation to standard output.

    Quote Originally Posted by MK27
    Again: I don't use fgets, I was going by what the OP claimed. And NO ONE has actually contributed a working piece of code to demonstrate the OP wrong, so s/he is probably right.
    At this point I would like to ask BMathis: what is your current code and what is the corresponding input, expected output and actual output?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #28
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by MK27 View Post
    read() is great because it just needs a file descriptor but you should use fread(), because fread() is more portable and so will work everywhere
    It doesn't matter, because you could not have created a pipe using a portable method in the first place.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  14. #29
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by vart View Post
    This is a macro used by SOME functions to indicate the EOF condition. It is NOT the character read from the stream
    Live and learn.

    Quote Originally Posted by brewbuck
    It doesn't matter, because you could not have created a pipe using a portable method in the first place.
    Oh pooh! Some aspect of the Original Point remains worthwhile, perhaps, anyway.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pipes sometimes fail
    By EVOEx in forum Linux Programming
    Replies: 2
    Last Post: 05-02-2009, 01:47 PM
  2. Pipes
    By Martin Kovac in forum C Programming
    Replies: 1
    Last Post: 03-31-2009, 03:09 AM
  3. Simulating wireless netowork using IPC
    By tenkasian in forum C Programming
    Replies: 3
    Last Post: 03-23-2009, 04:16 PM
  4. Segmentation fault - IPC
    By kbfirebreather in forum C Programming
    Replies: 7
    Last Post: 02-01-2009, 03:17 PM
  5. Services and Pipes
    By nickname_changed in forum Windows Programming
    Replies: 0
    Last Post: 07-16-2003, 06:46 AM