Note that there are some explanatory texts on larger screens.

plurals
  1. POworking with pipes
    primarykey
    data
    text
    <p>I am trying to make this work but no luck, basically i need to write to the pipe and then make the pipe return back with the text i sent. I have a server.c and client.c , so i make the server.c run..., open a new terminal and then run the client.. the problem is that the client doesnt do anything when i run it.. I am sure i am missing something.. like closing the pipe. i am not sure.. I would really appreciate some guidance</p> <p>server.c</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;errno.h&gt; #include &lt;ctype.h&gt; #include &lt;sys/types.h&gt; #include &lt;sys/stat.h&gt; #include &lt;fcntl.h&gt; #include &lt;string.h&gt; #define PIPE1 "PIPE1" #define PIPE5 "PIPE5" #define MAX_BUF_SIZE 255 int main(int argc, char *argv[]) { int rdfd1,rdfd2,rdfd3,rdfd4, wrfd1,wrfd2,wrfd3,wrfd4,ret_val, count, numread1,numread2,numread3,numread4; char buf1[MAX_BUF_SIZE]; char buf2[MAX_BUF_SIZE]; char buf3[MAX_BUF_SIZE]; char buf4[MAX_BUF_SIZE]; /* Create the first named - pipe */ ret_val = mkfifo(PIPE1, 0666); if ((ret_val == -1) &amp;&amp; (errno != EEXIST)) { perror("Error creating the named pipe"); return 1; } ret_val = mkfifo(PIPE5, 0666); if ((ret_val == -1) &amp;&amp; (errno != EEXIST)) { perror("Error creating the named pipe"); return 1; } /* Open the first named pipe for reading */ rdfd1 = open(PIPE1, O_RDONLY); /* Open the first named pipe for writing */ wrfd1 = open(PIPE5, O_WRONLY); /* Read from the pipes */ numread1 = read(rdfd1, buf1, MAX_BUF_SIZE); buf1[numread1] = '0'; printf("Server : Read From the pipe : %sn", buf1); /* * Write the converted content to * pipe */ write(wrfd1, buf1, strlen(buf1)); } </code></pre> <p>client.c</p> <pre><code>#include &lt;errno.h&gt; #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include &lt;unistd.h&gt; #include &lt;ctype.h&gt; #include &lt;sys/types.h&gt; #include &lt;sys/stat.h&gt; #include &lt;fcntl.h&gt; #define PIPE1 "PIPE1" #define PIPE5 "PIPE5" #define MAX_BUF_SIZE 255 int main(int argc, char *argv[ ]) { pid_t childpid; int error; int i; int nprocs; /* check command line for a valid number of processes to generate */ int wrfd1, rdfd1, numread; char rdbuf[MAX_BUF_SIZE]; if ( (argc != 2) || ((nprocs = atoi (argv[1])) &lt;= 0) ) { fprintf (stderr, "Usage: %s nprocs\n", argv[0]); return 1; } for (i = 1; i &lt; nprocs; i++) { /* create the remaining processes */ if ((childpid = fork()) == -1) { fprintf(stderr, "[%ld]:failed to create child %d: %s\n", (long)getpid(), i, strerror(errno)); return 1; } /* Open the first named pipe for writing */ wrfd1 = open(PIPE5, O_WRONLY); /* Open the second named pipe for reading */ rdfd1 = open(PIPE1, O_RDONLY); if (childpid) break; char string1[100]; if(sprintf(string1, "This is process %d with ID %ld and parent id %ld\n", i, (long)getpid(), (long)getppid())) { write(wrfd1,string1, strlen(string1)); } /* Read from the pipe */ numread = read(rdfd1, rdbuf, MAX_BUF_SIZE); rdbuf[numread] = '0'; printf("Full Duplex Client : Read From the Pipe : %sn", rdbuf); } return 0; } </code></pre>
    singulars
    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.
 

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