Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Stwórz programy do dialogu mi ę dzy dwoma u ż ytkownikami w systemie. Do
- komunikacji u ż yj łą cza FIFO stworzonego za pomoc ą funkcji mknod.
- */
- // Program pierwszy
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <string.h>
- #include <signal.h>
- #include <stdbool.h>
- #define BUFFER_SIZE 256
- #define SEND_FIFO_DIR "./send_fifo"
- #define RECV_FIFO_DIR "./recv_fifo"
- int send_fd, recv_fd;
- void sigint_handler(int signum)
- {
- if((close(send_fd) == -1) || (close(recv_fd) == -1)){
- perror("close");
- exit(EXIT_FAILURE);
- }
- if((unlink(SEND_FIFO_DIR) == -1) || (unlink(RECV_FIFO_DIR))){
- perror("unlink");
- exit(EXIT_FAILURE);
- }
- printf("\n");
- exit(EXIT_SUCCESS);
- }
- int main(void)
- {
- char buffer[BUFFER_SIZE];
- bool send_recv_order = true;
- if(signal(SIGINT, sigint_handler) == SIG_ERR){
- perror("signal");
- return EXIT_FAILURE;
- }
- if((mknod(SEND_FIFO_DIR, S_IFIFO | 0600, 0) == -1) || mknod(RECV_FIFO_DIR, S_IFIFO | 0600, 0)){
- perror("mknod");
- return EXIT_FAILURE;
- }
- printf("Oczekiwanie na drugiego uzytkownika...\n");
- if((send_fd = open(SEND_FIFO_DIR, O_WRONLY)) == -1){
- perror("open");
- return EXIT_FAILURE;
- }
- if((recv_fd = open(RECV_FIFO_DIR, O_RDONLY)) == -1){
- perror("open");
- return EXIT_FAILURE;
- }
- while(1){
- if(send_recv_order){
- printf("> ");
- memset(buffer, '\0', BUFFER_SIZE);
- fgets(buffer, BUFFER_SIZE, stdin);
- if(write(send_fd, buffer, BUFFER_SIZE) == -1){
- perror("write");
- return EXIT_FAILURE;
- }
- }
- else{
- memset(buffer, '\0', BUFFER_SIZE);
- if(read(recv_fd, buffer, BUFFER_SIZE) == -1){
- perror("read");
- return EXIT_FAILURE;
- }
- printf("< %s\n", buffer);
- }
- send_recv_order = !send_recv_order;
- }
- if((close(send_fd) == -1) || (close(recv_fd) == -1)){
- perror("close");
- return EXIT_FAILURE;
- }
- return EXIT_SUCCESS;
- }
Add Comment
Please, Sign In to add comment