Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.13 KB | None | 0 0
  1. #include <string.h>
  2. #include <errno.h>
  3. #include <semaphore.h>
  4. #include <sys/mman.h>
  5. #include <sys/types.h>
  6. #include <unistd.h>
  7. #include <fcntl.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <signal.h>
  11. #include <time.h>
  12.  
  13. #define SIZEMAP 1
  14.  
  15. static void handler(int signal) {
  16.     switch (signal) {
  17.         case SIGUSR1:
  18.         default:
  19.             ;
  20.     }
  21. }
  22.  
  23. static void handler_install() {
  24.     struct sigaction action;
  25.    
  26.     action.sa_handler = handler;
  27.     action.sa_flags = 0;
  28.     sigemptyset(&action.sa_mask);
  29.     sigaction(SIGUSR1, &action, NULL);
  30. }
  31.  
  32. void timespec_add(struct timespec * tv_a, const struct timespec * tv_b) {
  33.     tv_a->tv_sec += tv_b->tv_sec;
  34.     tv_a->tv_nsec += tv_b->tv_nsec;
  35.    
  36.     if (tv_a->tv_nsec > 999999999) {
  37.         tv_a->tv_sec += 1;
  38.         tv_a->tv_nsec -= 999999999;
  39.     }
  40. }
  41.  
  42. void timespec_minus(struct timespec * tv_a, const struct timespec * tv_b) {
  43.     tv_a->tv_sec -= tv_b->tv_sec;
  44.     tv_a->tv_nsec -= tv_b->tv_nsec;
  45.    
  46.     if (tv_a->tv_nsec < 0) {
  47.         tv_a->tv_sec -= 1;
  48.         tv_a->tv_nsec += 1000 * 1000 * 1000;
  49.     }
  50. }
  51.  
  52. int main() {
  53.     int shm_fd;
  54.     struct timespec* desc_map;
  55.     struct timespec* t_temp;
  56.     struct timespec* t_final;
  57.     int i;
  58.    
  59.     shm_fd = shm_open("shm_testfork", O_RDWR | O_CREAT, 0666);
  60.     if (!shm_fd) {
  61.         fprintf(stderr, "%s(%d): %s\n", __FILE__, __LINE__, strerror(errno));
  62.         exit(1);
  63.     }
  64.  
  65.     if (ftruncate(shm_fd, (SIZEMAP * sizeof(struct timespec))) != 0) {
  66.         fprintf(stderr, "%s(%d): %s\n", __FILE__, __LINE__, strerror(errno));
  67.         exit(1);
  68.     }
  69.    
  70.     handler_install();
  71.    
  72.     desc_map = malloc(sizeof(struct timespec));
  73.     t_temp = malloc(sizeof(struct timespec));
  74.     t_final = malloc(sizeof(struct timespec));
  75.    
  76.     t_final->tv_sec = 0;
  77.     t_final->tv_nsec = 0;
  78.    
  79.     for (i = 0; i < 1000; i++) {
  80.         clock_gettime(CLOCK_REALTIME, t_temp);
  81.         if (fork() == 0) {
  82.             pause();
  83.             timespec_minus(t_temp, desc_map);
  84.             timespec_add(t_final, t_temp);
  85.         }
  86.         else {
  87.             clock_gettime(CLOCK_REALTIME, desc_map);
  88.             kill(getppid(), SIGUSR1);
  89.             kill(getpid(), SIGKILL);
  90.         }
  91.     }
  92.    
  93.     free(desc_map);
  94.     free(t_temp);
  95.     free(t_final);
  96.     shm_unlink("shm_testfork");
  97.     exit(0);
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement