Thread: help with pipe and socket in C

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    29

    help with pipe and socket in C

    Are these three statements true?
    The first one, i don't think its true because what sets up the pipes is the pipe() function.
    The second one, isn't pipes one way, but sockets 2 way?
    For the last one, select only tells you how many are ready to read from right, so its not true?


    The reason we can use pipes between parent and child processes is that fork() will always set up the pipes.

    Both pipes and sockets are two-way: in order to be able to both write to a process and read from a process, we use a single pipe or a socket.

    Select allows a process to wait for multiple file descriptors at the same time, without getting blocked on any one of them.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Since you're working on some sort of *nix system, you should have man pages for all these functions at your disposal, and if not, just Google "man fork". All the answers are in there. But to help you out:

    1.You are correct. man fork and man pipe should clarify this.

    2.
    Quote Originally Posted by man pipe
    DESCRIPTION
    pipe() creates a pipe, a unidirectional data channel that can be used for interprocess communication.
    So yes, pipes are one-way. Sockets are two-way (though this one at least isn't clear from the man page).

    3.
    Quote Originally Posted by man select
    RETURN VALUE
    On success, select() and pselect() return the number of file descriptors contained in the three returned descriptor sets
    It actually is true. The fact that select only tells you how many are ready for reading/writing doesn't mean that it blocks. In fact, it was designed specifically for non-blocking I/O. If you call select and it says 5 descriptors are ready for reading, it doesn't stay there until you read everything. It lets you chose which ones you want to read from. You can specify a timeout, how long you want it to wait, but still nothing blocks.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    29
    So with the right parameters you can make it wait until more than one are ready to read from?

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    No, you can't make it wait until there are multiple things ready for I/O. You can ask it to wait for a while and report back how many things are ready for I/O at that time. It may be 0 things, 1 thing or 10 things. You can make it block indefinitely, but it will return as soon as just one thing is ready. No forcing it to wait for multiple things. Really, read the man page thoroughly, it will explain a lot. You should also experiment with a few simple programs to test blocking and non-blocking I/O, pipes and sockets, etc. Reading and studying is great, but nothing beats coding for learning how things actually work.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    29
    is this true for a shell script


    In order for a file to be called from the command line and execute as a shell script on Unix, it must have a shebang (#!) line as one of its lines.


    I know it should be the first line (if it has the shebang line, which you may need depending on which shell u have), so isn't this false?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pipe multiple programs using pipe() and c
    By HaitiBoy in forum C Programming
    Replies: 1
    Last Post: 12-07-2010, 05:19 PM
  2. pipe
    By vapanchamukhi in forum C Programming
    Replies: 2
    Last Post: 09-23-2008, 01:35 AM
  3. Pipe problem, popen(), pipe().
    By apacz in forum C Programming
    Replies: 7
    Last Post: 06-08-2006, 12:55 AM
  4. pipe()
    By bojan in forum C Programming
    Replies: 2
    Last Post: 04-13-2006, 07:47 AM
  5. n00b doing a Socket operation on non-socket
    By Kinasz in forum Networking/Device Communication
    Replies: 2
    Last Post: 03-25-2004, 03:29 AM