Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's an example of a program that creates a pipe and then forks the process and calls a sender function in the parent and a receiver in the child. The pipe creation and file descriptors are in one source code file with an associated header file, as are the sender and receiver. The <code>main</code> file requests the pipe be created then does the <code>fork()</code> and calls the <code>sender</code> and <code>receiver</code> functions.</p> <p>pipe.h - this contains the <code>extern</code> declaration for the pipe file descriptors as well as the declaration of the function that creates the pipe:-</p> <pre><code>#ifndef PIPE_H #define PIPE_H extern int pipe_fd[2]; void create_pipe(void); #endif </code></pre> <p>pipe.c - contains the actual definition of the <code>pipe_fd</code> array:-</p> <pre><code>#include "pipe.h" #include &lt;unistd.c&gt; int pipe_fd[2]; void create_pipe(void) { pipe(pipe_fd); } </code></pre> <p>sender.h - declares the prototype for the sender() function</p> <pre><code>#ifndef SENDER_H #define SENDER_H void sender(void); #endif </code></pre> <p>sender.c:-</p> <pre><code>#include "sender.h" #include "pipe.h" #include &lt;unistd.h&gt; #include &lt;stdio.h&gt; void sender(void) { char buf[]="Hello world"; printf("Sender: PID = %d\n", getpid()); close(pipe_fd[0]); write(pipe_fd[1], buf, sizeof(buf)); } </code></pre> <p>receiver.h:-</p> <pre><code>#ifndef RECEIVER_H #define RECEIVER_H void receiver(void); #endif </code></pre> <p>receiver.c - mirror image of sender</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;unistd.h&gt; #include "receiver.h" #include "pipe.h" void receiver(void) { int bytes; char buf[101]; printf("Receiver: PID = %d\n", getpid()); close(pipe_fd[1]); bytes = read(pipe_fd[0], buf, 100); buf[bytes]='\0'; printf("Receiver got: %s\n", buf); } </code></pre> <p>main.c - ties it all together</p> <pre><code>#include "pipe.h" #include "sender.h" #include "receiver.h" #include &lt;sys/types.h&gt; #include &lt;unistd.h&gt; void launch_sender_receiver(void) { pid_t forkpid; forkpid = fork(); if (forkpid == 0) receiver(); /* child */ else sender(); } int main(int argc, char* argv[]) { create_pipe(); launch_sender_receiver(); return 0; } </code></pre> <p>Hopefully you can follow all this from the code but if not here's a little extra explanation.</p> <p>The create_pipe() function in pipe.c creates a pipe and puts the two file descriptors into <code>file_fd</code>. The pipe.h file provides an <code>extern</code> declaration for the file descriptors so that they can be accessed by the sender and receiver files (better programming practice would be to provide "getter" functions for these file descriptors in pipe.h so that <code>sender()</code> and <code>receiver()</code> are not accessing global variables).</p> <p>Sender and Receiver use the <code>pipe_fd</code> array to either write or read from the pipe after they close the file descriptor that they don't need. The <code>main()</code> function ties it all together by calling the pipe creation function and then doing the fork and calling sender or receiver depending on whether it is the parent or child respectively.</p> <p>Running this as a complete program should get you the following output (although of course the PIDs you get will be different):-</p> <pre><code>Receiver: PID = 3285 Sender: PID = 3284 Receiver got: Hello world </code></pre> <p>Does all that make sense?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload