Sha256: 4ddf55774af7ade1190d6e28484925f75647364cf7b9e776242da79021d1cf08
Contents?: true
Size: 1.56 KB
Versions: 6
Compression:
Stored size: 1.56 KB
Contents
#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <fcntl.h> /* * simple program creating a pipe between two processes */ int main(int argc, char **argv) { pid_t child_id; pid_t my_id; int pipe_desc[2]; char *string; char read_buffer[4096]; int err; /* * create the pipe */ err = pipe(pipe_desc); if(err < 0) { printf("error creating pipe\n"); exit(1); } /* * then fork */ child_id = fork(); if(child_id != 0) { /* * parent will be the writer * doesn't need the read end */ my_id = getpid(); close(pipe_desc[0]); /* * send the child a string */ string = "a string made by the parent\n"; printf("pid: %d -- writing %s to pipe_desc[1]\n", (int)my_id, string); write(pipe_desc[1],string,strlen(string)); /* * and another string */ string = "and another string"; printf("pid: %d -- writing %s to pipe_desc[1]\n", (int)my_id, string); write(pipe_desc[1],string,strlen(string)); /* * close the pipe to let the read end know we are * done */ } else { /* * child will read until pipe closes * close the write end */ my_id = getpid(); close(pipe_desc[1]); memset(read_buffer,0,sizeof(read_buffer)); while(read(pipe_desc[0],read_buffer,sizeof(read_buffer))) { printf("pid: %d -- received %s from parent\n", (int)my_id, read_buffer); memset(read_buffer,0,sizeof(read_buffer)); } printf("pid: %d -- child detects write end closed\n", (int)my_id); close(pipe_desc[0]); } printf("pid: %d -- I am exiting\n",my_id); exit(0); }
Version data entries
6 entries across 6 versions & 1 rubygems