#include #include #include #include #include int main () { int pipe1[2]; // Parent to child int pipe2[2]; // Child to parent // Create both pipes if (pipe(pipe1) == -1 || pipe(pipe2) == -1) { fprintf(stderr, "Could not open pipes"); return 1; } // Fork process pid_t pid = fork(); if (pid == 0) { // Child process // Close unused pipe ends close(pipe1[1]) ; // Close write end of pipe1 close(pipe2[0]) ; // Close read end of pipe2 // Read message, send response char *message = malloc(100); read(pipe1[0], message, 100); printf("%s", message); free(message); char response[] = "Response from child to parent\n"; write(pipe2[1], response, sizeof(response)); } else { // Parent process // Close unused pipe ends close(pipe1[0]) ; // Close read end of pipe1 close(pipe2[1]) ; // Close write end of pipe2 // Send message, read response char message[] = "Sending message from parent to child\n"; write(pipe1[1], message, sizeof(message)); char *response = malloc(100); read(pipe2[0], response, 100); printf("%s", response); free(response); // Wait for child process to terminate waitpid(pid, NULL, 0); } return 0; }