This commit is contained in:
Joshua Powers
2022-09-14 10:34:38 -05:00
parent 5c5b03b6e0
commit b0caf2463e
14 changed files with 696 additions and 0 deletions

75
Examples/ipc/ipcpipe2.c Normal file
View File

@@ -0,0 +1,75 @@
/*
* 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
}