Untitled
unknown
plain_text
2 years ago
1.1 kB
4
Indexable
#include <iostream> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> int main() { int pipefd[2]; if (pipe(pipefd) == -1) { std::cerr << "Błąd przy tworzeniu potoku" << std::endl; return 1; } pid_t pid = fork(); if (pid == -1) { std::cerr << "Błąd przy tworzeniu procesu potomnego" << std::endl; return 1; } if (pid == 0) { // Proces potomny close(pipefd[1]); // Zamykamy końcówkę zapisującą potoku char buffer[6]; // Bufor na odczytane dane read(pipefd[0], buffer, sizeof(buffer)); close(pipefd[0]); // Zamykamy końcówkę odczytującą potoku std::cout << "Odczytano z potoku: " << buffer << std::endl; } else { // Proces macierzysty close(pipefd[0]); // Zamykamy końcówkę odczytującą potoku const char* message = "HALLO!"; write(pipefd[1], message, 6); close(pipefd[1]); // Zamykamy końcówkę zapisującą potoku wait(NULL); // Czekamy na zakończenie procesu potomnego } return 0; }
Editor is loading...