Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Napisac program, który stworzy 2 procesy: macierzysty i potomny. Przez proces rodzicielski 10 raz wyslać wiadomosć (np. Hello world) do potomnego co 3 sekundy( sleep() ) Dla komunikacji należy użyc lącza nienazwanego (pipe)
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #define WIADOMOSC "Hello world"
- #define ROZMIAR 128
- int main(void)
- {
- pid_t pid;
- int pipefd[2];
- char buffer[ROZMIAR];
- if(pipe(pipefd) == -1){
- perror("pipe");
- return EXIT_FAILURE;
- }
- pid = fork();
- if(pid == -1){
- perror("fork");
- return EXIT_FAILURE;
- }
- else if(pid == 0){
- for(int i = 0; i < 10; i++){
- memset(buffer, '\0', ROZMIAR);
- if(read(pipefd[0], buffer, ROZMIAR) == -1){
- perror("read");
- return EXIT_FAILURE;
- }
- printf("Wiadomosc: %s\n", buffer);
- }
- if((close((pipefd[0]) == -1)) || (close(pipefd[1]) == -1)){
- perror("close");
- return EXIT_FAILURE;
- }
- }
- else{
- for(int i = 0; i < 10; i++){
- if(write(pipefd[1], WIADOMOSC, strlen(WIADOMOSC)) == -1){
- perror("write");
- return EXIT_FAILURE;
- }
- sleep(3);
- }
- if((close(pipefd[1]) == -1) || (close(pipefd[0]) == -1)){
- perror("close");
- return EXIT_FAILURE;
- }
- }
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment