Advertisement
3axap_010

server.c

Apr 16th, 2020
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.17 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <sys/types.h>
  3. #include <signal.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <semaphore.h>
  8. #include <fcntl.h>
  9. #include <sys/stat.h>
  10.  
  11. #define BUF_SIZE 512
  12.  
  13. const char* read_sem_name = "read_semaphore";
  14. const char* write_sem_name = "write_semaphore";
  15. const char* child_name = "/home/zakhar/Documents/C/Pipe/Client";
  16.  
  17. sem_t* open_sem(const char* sem_name, unsigned int value)
  18. {
  19.     sem_t* sem = sem_open(sem_name, O_CREAT, S_IRWXO | S_IRWXU, value); // other processes have write and read permission
  20.     if (sem == SEM_FAILED)
  21.     {
  22.         fprintf(stderr, "Can't create a semaphore\n");
  23.         return NULL;
  24.     }
  25.  
  26.     return sem;
  27. }
  28.  
  29. int main(int argc, char** argv)
  30. {  
  31.     sem_t* read_sem = open_sem(read_sem_name, 0);
  32.     sem_t* write_sem = open_sem(write_sem_name, 1);
  33.  
  34.     int pipefd[2];
  35.  
  36.     if (pipe(pipefd) == -1)
  37.     {
  38.         fprintf(stderr, "Can't make a pipe\n");
  39.         exit(EXIT_FAILURE);
  40.     }  
  41.  
  42.     char string[BUF_SIZE];
  43.  
  44.     char** args = malloc(sizeof(char*) * 2);
  45.     args[0] = malloc(sizeof(char) * (strlen(child_name) + 1));
  46.     strncpy(args[0], child_name, strlen(child_name) + 1);
  47.     args[1] = NULL;
  48.  
  49.     pid_t pid = fork();
  50.     if (pid == -1)
  51.     {
  52.         fprintf(stderr, "Can't create a child process\n");
  53.         close(pipefd[1]);
  54.         close(pipefd[0]);
  55.         sem_close(read_sem);
  56.         sem_close(write_sem);
  57.         sem_unlink(write_sem_name);
  58.         sem_unlink(read_sem_name);
  59.         exit(EXIT_FAILURE);
  60.     }
  61.     else
  62.     {
  63.         if (pid == 0)
  64.         {
  65.             close(pipefd[1]);
  66.             if (dup2(pipefd[0], 0) == -1)
  67.             {
  68.                 fprintf(stderr, "Failed to dup read end\n");
  69.                 exit(EXIT_FAILURE);
  70.             }
  71.             close(pipefd[0]);
  72.  
  73.             if (execvp(child_name, args) == -1)
  74.             {
  75.                 fprintf(stderr, "Execvp error!\n");
  76.                 exit(EXIT_FAILURE);
  77.             }
  78.         }
  79.     }
  80.        
  81.     close(pipefd[0]);
  82.  
  83.     while (1)
  84.     {
  85.         sem_wait(write_sem);
  86.  
  87.         fgets(string, BUF_SIZE, stdin);
  88.         if (*string == '\0' || *string == '\n' || *string == '\r')
  89.         {
  90.             kill(pid, SIGTERM);
  91.             break;
  92.         }
  93.  
  94.         write(pipefd[1], string, BUF_SIZE);
  95.         sem_post(read_sem);
  96.     }
  97.  
  98.     close(pipefd[1]);
  99.     sem_close(read_sem);
  100.     sem_close(write_sem);
  101.     sem_unlink(write_sem_name);
  102.     sem_unlink(read_sem_name);
  103.  
  104.     free(args[0]);
  105.     free(args);
  106.  
  107.     exit(EXIT_SUCCESS);
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement