Skip to content

Instantly share code, notes, and snippets.

@olastor
Created November 5, 2017 13:52
Show Gist options
  • Save olastor/64016e6b70703ba4c17d6fdec9bf619c to your computer and use it in GitHub Desktop.
Save olastor/64016e6b70703ba4c17d6fdec9bf619c to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <limits.h>
int main() {
pid_t pid;
int fd[2];
// initialize pipe that will be used
// to send data from the child process
// to the parent process
pipe(fd);
switch ((pid = fork())) {
case -1:
// error occurred
printf("Error using fork()");
break;
case 0:
// close reading side of pipe in child process
close(fd[0]);
// write message to pipe
char message[] = "Hello there!";
write(fd[1], message, strlen(message));
break;
default:
// close writing side of pipe in parent process
close(fd[1]);
// read message to buffer
char buffer[100];
read(fd[0], buffer, PIPE_BUF);
// print out message
printf("Got message: %s\n", buffer);
}
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment