Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h> // printf(), perror()
- #include <stdlib.h> // exit()
- #include <unistd.h> // EXIT_SUCCESS and EXIT_FAILURE
- #include <sys/stat.h> // mknod()
- #include <sys/wait.h> // wait()
- #include <sys/types.h> // read(), write(), close()
- #include <fcntl.h> // open()
- int main()
- {
- int fd; // file descriptor
- int pr; // process
- int st; // status [for wait()]
- int FIFO; // for FIFO file
- size_t s1, s2; // sizes for the arrays
- char str[] = "Hello world!"; // text to be written
- s2 = sizeof(str);
- char re_str[s2];
- char name[] = "abc.fifo"; // full file name (path)
- (void) umask(0);
- FIFO = mknod(name, S_IFIFO | 0644, 0); /* FIFO file */
- if (FIFO < 0)
- {
- perror("Unable to create FIFO!\n");
- exit(EXIT_FAILURE);
- }
- pr = fork(); // Creating two and exact process from this point ->
- if (pr < 0)
- {
- perror("Process hasn\'t been created!\n");
- exit(EXIT_FAILURE);
- }
- else // If the process has been created then ->
- {
- if (pr == 0) // CHILD
- {
- printf("[CHILD]\n");
- fd = open(name, O_WRONLY); // write only
- if (fd < 0)
- {
- perror("Can\'t open FIFO!\n");
- exit(EXIT_FAILURE);
- }
- s1 = write(fd, str, s2);
- if (s1 != s2)
- {
- printf("Information hasn\'t been written properly!\n");
- exit(EXIT_FAILURE);
- }
- close(fd);
- printf("[CHILD END]\n");
- }
- else // PARENT
- {
- printf("[PARENT]\n");
- wait(&st);
- fd = open(name, O_RDONLY);
- if (fd < 0)
- {
- perror("Can\'t open FIFO!\n");
- exit(EXIT_FAILURE);
- }
- s1 = read(fd, re_str, s2);
- if (s1 != s2)
- {
- printf("Information hasn\'t been read properly!\n");
- exit(EXIT_FAILURE);
- }
- close(fd);
- printf("[PARENT END]\n");
- }
- printf("\n%s\n", re_str);
- }
- exit(EXIT_SUCCESS);
- }
Advertisement
Add Comment
Please, Sign In to add comment