Advertisement
Guest User

processes.c

a guest
May 26th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.60 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <math.h>
  4. #include <sys/time.h>
  5. #include <time.h>
  6. #include <stdlib.h>
  7. #include <sys/types.h>
  8. #include <sys/wait.h>
  9. #include <signal.h>
  10. #define CHILDREN_COUNT 2
  11.  
  12. int childs[CHILDREN_COUNT];
  13. char *getCurrentTime();
  14. void childHandler(int sig);
  15. void parentHandler(int sig, siginfo_t *info, void *context);
  16.  
  17. int main()
  18. {
  19.     struct sigaction sa;
  20.     sa.sa_flags = SA_SIGINFO;
  21.     sa.sa_sigaction = parentHandler;
  22.     sigaction(SIGUSR2, &sa, NULL);
  23.     for (int i = 0; i < CHILDREN_COUNT; i++)
  24.     {
  25.         int pid = fork();
  26.         childs[i] = pid;
  27.         if (pid == 0)
  28.         {
  29.             signal(SIGUSR1, childHandler);
  30.             for(int j = 0; j < 10; j++){
  31.                 printf("Child %d paused...\n", getpid());
  32.                 pause();
  33.             }
  34.            
  35.             exit(0);
  36.         }
  37.         else if (pid > 0)
  38.         {
  39.         }
  40.         else
  41.         {
  42.             printf("error");
  43.             return -1;
  44.         }
  45.     }
  46.     usleep(100);
  47.     kill(childs[0], SIGUSR1);
  48.     kill(childs[1], SIGUSR1);
  49.     for(int j = 0; j < 10; j++){
  50.         printf("Parent %d paused...\n", getpid());
  51.         pause();
  52.     }
  53.     return 0;
  54. }
  55.  
  56. void childHandler(int sig)
  57. {
  58.     printf("Signal %d\n", sig);
  59.     if (sig == SIGUSR1)
  60.     {
  61.         static int n = 1;
  62.         printf("CHILD GET %d %d %d %s\n", n, getpid(), getppid(), getCurrentTime());
  63.         usleep(100);
  64.         printf("CHILD SEND %d %d %d %s\n", n, getpid(), getppid(), getCurrentTime());
  65.         kill(getppid(), SIGUSR2);
  66.         n++;
  67.     }
  68. }
  69.  
  70. void parentHandler(int sig, siginfo_t *info, void *context)
  71. {
  72.     printf("Signal %d\n", sig);
  73.     if (sig == SIGUSR2)
  74.     {
  75.         static int n = 1;
  76.         printf("PARENT GET %d %d %d %s\n", n, getpid(), info->si_pid, getCurrentTime());
  77.         usleep(100);
  78.         printf("PARENT SEND %d %d %d %s\n", n, getpid(), info->si_pid, getCurrentTime());
  79.         kill(info->si_pid, SIGUSR1);
  80.         n++;
  81.     }
  82. }
  83.  
  84. char *getCurrentTime()
  85. {
  86.     char buffer[15];
  87.     char *current_time;
  88.     int millisec;
  89.     struct tm *tm_info;
  90.     struct timeval tv;
  91.  
  92.     gettimeofday(&tv, NULL);
  93.  
  94.     millisec = lrint(tv.tv_usec / 1000.0); // Round to nearest millisec
  95.     if (millisec >= 1000)
  96.     { // Allow for rounding up to nearest second
  97.         millisec -= 1000;
  98.         tv.tv_sec++;
  99.     }
  100.  
  101.     tm_info = localtime(&tv.tv_sec);
  102.  
  103.     strftime(buffer, 15, "%H:%M:%S", tm_info);
  104.     current_time = (char *)malloc(20 * sizeof(char));
  105.     sprintf(current_time, "%s:%03d\n", buffer, millisec);
  106.     return current_time;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement