Advertisement
Guest User

pipe io

a guest
Sep 9th, 2021
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.22 KB | None | 0 0
  1. #define _GNU_SOURCE
  2. #define _POSIX_C_SOURCE 199309L
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <sched.h>
  7. #include <unistd.h>
  8. #include <time.h>
  9.  
  10. #define BILLION 1e9
  11.  
  12. int main(int argc, char *argv[]) {
  13.     int pipefd_1[2];
  14.  
  15.     struct timespec start, stop;
  16.     clockid_t clk_id = CLOCK_REALTIME;
  17.  
  18.     // for child and parent process run on the same cpu
  19.     cpu_set_t set;
  20.     int parentCPU, childCPU;
  21.  
  22.     char testChar = 'a';        /* Use for test */
  23.  
  24.     if (argc != 5) {
  25.         fprintf(stderr, "Usage: %s parent-cpu child-cpu times\n",
  26.                 argv[0]);
  27.         exit(EXIT_FAILURE);
  28.     }
  29.  
  30.     parentCPU = atoi(argv[1]);
  31.     childCPU = atoi(argv[2]);
  32.     int const times  = atoi(argv[3]);  
  33.     int const writeEh  = atoi(argv[0]);
  34.  
  35.     CPU_ZERO(&set);
  36.  
  37.     if (pipe(pipefd_1) == -1) {
  38.         perror("pipe");
  39.         exit(EXIT_FAILURE);
  40.     }
  41.  
  42.     char ch;
  43.     switch (fork()) {
  44.         case -1:    /* error */
  45.             perror("fork");
  46.             exit(EXIT_FAILURE);
  47.            
  48.         case 0:     /* child process */
  49.             CPU_SET(childCPU, &set);
  50.  
  51.             if (sched_setaffinity(getpid(), sizeof(set), &set) == -1) {
  52.                 perror("set cpu for child process");
  53.                 exit(EXIT_FAILURE);
  54.             }
  55.  
  56.  
  57.  
  58.             for (int i = 0; i < times; ++i) {
  59.                 if(!writeEh) write(pipefd_1[1], &ch, 1);
  60.                 else read(pipefd_1[0], &ch, 1);
  61.             }
  62.  
  63.             exit(EXIT_SUCCESS);
  64.  
  65.         default:    /* parent process */
  66.             CPU_SET(parentCPU, &set);
  67.  
  68.             if (sched_setaffinity(getpid(), sizeof(set), &set) == -1) {
  69.                 perror("set cpu for parent process");
  70.                 exit(EXIT_FAILURE);
  71.             }
  72.  
  73.  
  74.             if(0>clock_gettime(clk_id, &start)) return perror("clock_gettime"),1;
  75.             for (int i = 0; i < times; ++i) {
  76.                 if(writeEh) write(pipefd_1[1], &ch, 1);
  77.                 else read(pipefd_1[0], &ch, 1);
  78.             }
  79.             if(0>clock_gettime(clk_id, &stop)) return perror("clock_gettime"),1;
  80.  
  81.             printf("the average cost of io is: %lf nsec\n", ((stop.tv_sec - start.tv_sec) * BILLION
  82.                          + stop.tv_nsec - start.tv_nsec) / times);
  83.     }
  84.  
  85.     exit(EXIT_SUCCESS);
  86. }
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement