/* * A simple program to illustrate the work of a pair of pipes, * a process can write to the pipe and read directly back from it. */ #include #include #include #define BUFFER_SIZE 32 #define READ_END 0 // read 0 and write 1 can't be chosen randomly #define WRITE_END 1 int main(int argc, char * argv[]) { char sent_msg[BUFFER_SIZE] = "Greetings"; char read_msg[BUFFER_SIZE]; pid_t pid; int fd[2]; int amt_read; // number of bytes read char c; /* create the pipe */ if (pipe(fd) == -1) { fprintf(stderr," pipe failed \n"); return -1; } printf("fd[0] %u fd[1] %u\n", fd[0], fd[1]); /* write to the pipe */ write(fd[WRITE_END], sent_msg, strlen(sent_msg)); /* read the message */ amt_read = read(fd[READ_END], read_msg, BUFFER_SIZE); read_msg[amt_read] = 0; // terminate the string printf("read %s\n", read_msg); return 0; }