Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.79 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <time.h>
  3. #include "errors.h"
  4.  
  5. typedef struct alarm_tag {
  6.     struct alarm_tag    *link;
  7.     int                 seconds;
  8.     time_t              time;   /* seconds from EPOCH */
  9.     char                message[128];
  10.     int                 id;
  11.     int                 dsp_id;
  12. } alarm_t;
  13.  
  14. pthread_mutex_t alarm_mutex = PTHREAD_MUTEX_INITIALIZER;
  15. alarm_t *alarm_list = NULL;
  16.  
  17. int main (int argc, char *argv[])
  18. {
  19.     int status;
  20.     char line[128];
  21.     alarm_t *alarm, **last, *next;
  22.     pthread_t thread;
  23.  
  24.     status = pthread_create (
  25.         &thread, NULL, alarm_thread, NULL);
  26.     if (status != 0)
  27.         err_abort (status, "Create alarm thread");
  28.     while (1) {
  29.         printf ("alarm> ");
  30.         if (fgets (line, sizeof (line), stdin) == NULL) exit (0);
  31.         if (strlen (line) <= 1) continue;
  32.         alarm = (alarm_t*)malloc (sizeof (alarm_t));
  33.         if (alarm == NULL)
  34.             errno_abort ("Allocate alarm");
  35.  
  36.         if (sscanf (line, "Start_Alarm(%d) %d %128[^\n]",
  37.         &alarm->id, &alarm->seconds, alarm->message)) {
  38.             status = pthread_mutex_lock (&alarm_mutex);
  39.             if (status != 0)
  40.                 err_abort (status, "Lock mutex");
  41.  
  42.             printf ("Alarm(%d) Inserted by Main Thread %ld Into Alarm List at\
  43.            %ld: %d %s\n", alarm->id, pthread_self(), time(NULL),
  44.             alarm->seconds, alarm->message);
  45.  
  46.             alarm->time = time (NULL) + alarm->seconds;
  47.  
  48.             /*
  49.              * Insert the new alarm into the list of alarms, sorted by
  50.              * order of their id (increasing order).
  51.              */
  52.             last = &alarm_list;
  53.             next = *last;
  54.             while (next != NULL) {
  55.                 if (next->id <= alarm->id) {
  56.                     alarm->link = next;
  57.                     *last = alarm;
  58.                     break;
  59.                 }
  60.                 last = &next->link;
  61.                 next = next->link;
  62.             }
  63.  
  64.             if (next == NULL) {
  65.                 *last = alarm;
  66.                 alarm->link = NULL;
  67.             }
  68.  
  69.             status = pthread_mutex_unlock (&alarm_mutex);
  70.             if (status != 0)
  71.                 err_abort (status, "Unlock mutex");            
  72.         }
  73.         else if (sscanf (line, "Change_Alarm(%d) %d %128[^\n]",
  74.         &alarm->id, &alarm->seconds, alarm->message)) {
  75.             status = pthread_mutex_lock (&alarm_mutex);
  76.             if (status != 0)
  77.                 err_abort (status, "Lock mutex");
  78.  
  79.             // Do Something
  80.  
  81.             status = pthread_mutex_unlock (&alarm_mutex);
  82.             if (status != 0)
  83.                 err_abort (status, "Unlock mutex");
  84.         }
  85.         else {
  86.             fprintf (stderr, "Bad command\n");
  87.             free (alarm);
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement