#include #include #include #include /* Reading some text from the pipe. */ void read_from_pipe (int file) { FILE *stream; int c; stream = fdopen (file, "r"); while ( (c=fgetc(stream)) != EOF) putchar(c); //while(1==1) printf("%d\n",fgetc(stream)); fclose (stream); } /* Writing some text to the pipe. */ void write_to_pipe (int file) { FILE *stream; stream = fdopen (file, "w"); fprintf (stream, "hello, world!\n"); fprintf (stream, "goodbye, world!\n"); fclose (stream); } int main (void) { pid_t pid; int mypipe[2]; /* Creating unnamed pipe. */ if (pipe (mypipe)) { fprintf(stderr, "Pipe failed.\n"); return EXIT_FAILURE; } /* Creating child process because we need 2 processes to work with the pipe. */ pid = fork (); if (pid == (pid_t) 0) { /* Child process opens the pipe for reading. */ printf("child process for reading started\n"); read_from_pipe (mypipe[0]); // write_to_pipe (mypipe[1]); close(mypipe[1]); printf("child process ended\n"); return EXIT_SUCCESS; } else if (pid < (pid_t) 0) { /* If fork() failed. */ fprintf(stderr,"Fork failed.\n"); return EXIT_FAILURE; } else { /* Main (parent) process writes some data to the pipe. */ printf("parent process for writing started\n"); write_to_pipe (mypipe[1]); // read_from_pipe (mypipe[0]); printf("parent process ended\n"); return EXIT_SUCCESS; } }