Files
CS3841/Examples/ipc/ipcpipe2.c
Joshua Powers b0caf2463e stuff
2022-09-14 10:34:38 -05:00

76 lines
1.9 KiB
C

/*
* ipcpipe2.c - Pipes are unidirectional and require
* two pipes to send data in two directions
*/
#include <sys/types.h> // needed for pid_t
#include <sys/wait.h> // needed for wait system call
#include <unistd.h> // needed for fork, read, write, close system calls
#include <stdlib.h> // needed for exit
#include <stdio.h> // needed for printf, perror
int main()
{
/* Create pipes */
int pipe_to_child[2];
if (pipe(pipe_to_child) == -1) {
printf("PIPE FAILURE\n");
exit(EXIT_FAILURE);
}
int pipe_from_child[2];
if (pipe(pipe_from_child) == -1) {
printf("PIPE FAILURE\n");
exit(EXIT_FAILURE);
}
pid_t pid = fork(); // fork into 2 processes
if (pid == -1) // error
{
perror("fork");
exit(EXIT_FAILURE);
}
else if (pid == 0) //
{
/* Close unused pipe ends */
close(pipe_to_child[1]);
close(pipe_from_child[0]);
/* Write to parent */
write(pipe_from_child[1], "CHELLO", 6);
close(pipe_from_child[1]);
/* Read from parent */
char data[32];
read(pipe_to_child[0], data, sizeof(data));
printf("Child received %s from parent\n", data);
/* Close pipe */
close(pipe_to_child[0]);
return 0; // Return success
}
else // parent
{
/* Close unused pipe ends */
close(pipe_to_child[0]);
close(pipe_from_child[1]);
/* Write to child */
write(pipe_to_child[1], "PHELLO", 6);
close(pipe_to_child[1]);
/* Read from child */
char data[32];
read(pipe_from_child[0], data, sizeof(data));
printf("Parent recieved %s from child\n", data);
/* Close pipe */
close(pipe_from_child[0]);
/* Wait for child */
wait(0);
}
return 0; // Return success
}