Last active
March 30, 2024 13:34
-
-
Save rajkumar-p/7e9c991d12f6109f4f599d9c71b22c66 to your computer and use it in GitHub Desktop.
Linux: Parent process sending data to child, using a pipe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <cstring> | |
| #include <iostream> | |
| #include <sys/wait.h> | |
| #include <unistd.h> | |
| #define BUF_SIZE 10 | |
| int main(int argc, char *argv[]) { | |
| int p_fds[2]; | |
| if (pipe(p_fds) == -1) { | |
| std::cerr << "Error: pipe() - " << strerror(errno) << std::endl; | |
| exit(1); | |
| } | |
| switch (int pid = fork()) { | |
| case -1: | |
| std::cerr << "Error: fork() failed - " << strerror(errno) << std::endl; | |
| exit(1); | |
| break; | |
| case 0: | |
| // Child | |
| std::cout << "Child with PID: " << getpid() << " executing..." << std::endl; | |
| // close unwated file descriptors - write descriptor of the pipe end | |
| close(p_fds[1]); // IMPORTANT | |
| char buf[BUF_SIZE]; | |
| int n_read; | |
| std::cout << "Child with PID: " << getpid() << ", reading from parent..." | |
| << std::endl; | |
| while ((n_read = read(p_fds[0], buf, BUF_SIZE)) != 0) { | |
| if (n_read == -1) { | |
| std::cerr << "Error reading: " << strerror(errno) << std::endl; | |
| exit(1); | |
| } | |
| // Write to stdout | |
| write(STDOUT_FILENO, buf, n_read); | |
| } | |
| // Write \n to stdout | |
| write(STDOUT_FILENO, "\n", 1); | |
| std::cout << "Completed read by child with PID " << getpid() << std::endl; | |
| close(p_fds[0]); // IMPORTANT | |
| break; | |
| default: | |
| // Parent | |
| std::cout << "Parent with PID: " << getpid() << " executing..." | |
| << std::endl; | |
| // close unwated file descriptors - read descriptor of the pipe end | |
| close(p_fds[0]); // IMPORTANT | |
| // Random data | |
| char to_send[] = {"BErGgTYpK6" | |
| "KoAGxKxIdz" | |
| "y4KdQIcTx2" | |
| "YfDVUIxmQA" | |
| "t3k6ErdzfB" | |
| "kBpAX4UpBE" | |
| "qi9f45TeFZ" | |
| "WUUgONJ94I" | |
| "UKJqXYjGO2" | |
| "8qCpR4zQPl" | |
| "2M4dPnMLbd" | |
| "XEwwtYsTo3" | |
| "9g9GoJSqrz" | |
| "vdRvhPXlzU" | |
| "ZRYQ91B7lE"}; | |
| size_t to_send_len = strlen(to_send); | |
| std::cout << "Parent with PID: " << getpid() << ", writing to child..." | |
| << std::endl; | |
| int n_written = write(p_fds[1], to_send, to_send_len); | |
| if (n_written == -1) { | |
| std::cerr << "Error reading: " << strerror(errno) << std::endl; | |
| exit(1); | |
| } | |
| if (n_written != to_send_len) { | |
| std::cerr << "Incomplete write. To write: " << to_send_len | |
| << ". Actual written: " << n_written << std::endl; | |
| } | |
| // Notify write complete | |
| std::cout << "Completing write by parent with PID " << getpid() | |
| << std::endl; | |
| // close() notifies end-of-data on the fd | |
| close(p_fds[1]); // IMPORTANT | |
| break; | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment