Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- include <sys/wait.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <fcntl.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- int main(int argc, char *argv[])
- {
- int potok[2];
- int strumien;
- char wiadomosc[] = "Wiadomosc";
- int pid;
- char bufor[255],bufor1[255],bufor2[255];
- printf("Podaj tresc wiadomosci /n");
- scanf("%s",bufor1);
- if (pipe(potok) == -1) {
- perror("pipe:");
- exit(EXIT_FAILURE);
- }
- if (mkfifo("fifo", 0600) == -1) {
- perror("Blad");
- exit(EXIT_FAILURE);
- }
- fcntl(potok[0], O_NDELAY);
- fcntl(potok[1], O_NDELAY);
- pid = fork();
- if (pid == -1) {
- perror("fork:");
- exit(EXIT_FAILURE);
- }
- if (pid == 0) {
- close(potok[1]);
- read(potok[0], bufor, sizeof(bufor));
- printf("\nWiadomosc od rodzica(nienazwane): %s\n",bufor);
- close(potok[0]);
- strumien = open("fifo", O_WRONLY);
- if (strumien == -1) {
- perror("Blad otwarcia");
- exit(EXIT_FAILURE);
- }
- if (write(strumien, wiadomosc, sizeof(wiadomosc)) == -1) {
- perror("Blad zapisu");
- exit(EXIT_FAILURE);
- }
- exit(EXIT_SUCCESS);
- }
- else {
- close(potok[0]);
- write(potok[1], bufor1, strlen(bufor1));
- close(potok[1]);
- strumien = open("fifo", O_RDONLY);
- if (strumien == -1)
- perror("Blad otwarcia");
- if (read(strumien, bufor2, 50) == -1) {
- perror("Blad odczytu");
- exit(1);
- }
- printf("Wiadomosc od potomka(nazwane): %s\n\n", bufor2);
- unlink("fifo");
- wait(0);
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement