home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / ddjmag / ddj9209.zip / REAL.ASC < prev    next >
Text File  |  1992-08-10  |  4KB  |  154 lines

  1. _DEBUGGING REAL-TIME SYSTEMS_
  2. by Gurjot Singh, Moses Joseph, and Dave Barnett
  3.  
  4. [LISTING ONE]
  5.  
  6. /*  simulate.c -- Simulate a real-time system and measure and/or record. 
  7. **     1. Device interrupt response time; 2. Device driver interrupt service 
  8. **     time; 3. Task response time
  9. ** Each measured time is exagerated by a constant amount of time equal to the
  10. ** length of time it takes to make the measurement.
  11. */
  12.  
  13. #include <stdio.h>
  14. #include <pthread.h>
  15.  
  16. #define PRODUCER_PRIO 17
  17.  
  18. extern void producer();
  19. extern void display();
  20. extern void record();
  21.  
  22. struct {
  23.     void (*f)();    /* task entry point                                */
  24.     int p_bias; /* priority relative to producer (always negative) */
  25. } consumers = {
  26.     { display, -1 },
  27.     { record, -1 }
  28. };
  29. #define NCONSUMERS (sizeof consumers / sizeof consumers[0])
  30. main(argc, argv)
  31. int argc;
  32. char *argv[];
  33. {
  34.     int i;
  35.     if (argc != 2) {
  36.         fprintf(stderr, "Usage: %s timer_device\n", argv[0]);
  37.         exit(1);
  38.     }
  39.     init_data(NCONSUMERS);
  40.     for (i = 0; i < NCONSUMERS; i++) {
  41.           pthread_attr_t attr;
  42.           pthread_t tid;
  43.  
  44.           pthread_attr_create(&attr);
  45.           pthread_attr_setinheritsched(&attr, PTHREAD_DEFAULT_SCHED);
  46.           pthread_attr_setprio(&attr, PRODUCER_PRIO + consumers[i].p_bias);
  47.           if (pthread_create(&tid, attr, consumers[i].f, i) == -1) {
  48.             perror("pthread_create");
  49.             exit(1);
  50.           }
  51.     }
  52.     init_timer(argv[1]);
  53.     producer();
  54.     exit(0);
  55. }
  56. /* producer.c */
  57. #define wait_for_interrupt() sleep(5)
  58. #define record_trt()
  59. #define record_tct()
  60.  
  61. void producer()
  62. {
  63.     for (;;) {
  64.         wait_for_interrupt();
  65.         record_trt();
  66.         signal_consumers();
  67.         wait_for_consumers();
  68.         record_tct();
  69.     }
  70. }
  71. /* consumer1 */
  72. void display(id)
  73. int id;
  74. {
  75.         for (;;) {
  76.         wait_for_producer(id);
  77.         signal_producer(id);
  78.     }
  79. }
  80. /* synch.c */
  81. #include <stdio.h>
  82. #include <stdlib.h>
  83. #include <pthread.h>
  84.  
  85. static pthread_mutex_t mutex;
  86. static pthread_cond_t write_cond; /* O.K. to write */
  87. static pthread_cond_t read_cond; /* O.K. to read */
  88. static int readers_done;
  89. int *done;
  90. static int num_readers;
  91.  
  92. void init_data(readers)
  93. int readers;
  94. {
  95.     int i;
  96.     if (pthread_mutex_init(&mutex, pthread_mutexattr_default) == -1) {
  97.         perror("pthread_mutex_init");
  98.         exit(1);
  99.     }
  100.     if (pthread_cond_init(&write_cond, pthread_condattr_default) == -1) {
  101.         perror("pthread_cond_init");
  102.         exit(1);
  103.     }
  104.     if (pthread_cond_init(&read_cond, pthread_condattr_default) == -1) {
  105.         perror("pthread_cond_init");
  106.         exit(1);
  107.     }
  108.     num_readers = readers;
  109.     readers_done = num_readers;
  110.     if (!(done = (int *)malloc(num_readers * sizeof(int)))) {
  111.         perror("malloc");
  112.         exit(1);
  113.     }
  114.     for (i = 0; i < num_readers; i++) done[i] = 1;
  115. }
  116. void wait_for_consumers()
  117. {
  118.     pthread_mutex_lock(&mutex);
  119.     if (readers_done != num_readers) {
  120.         pthread_cond_wait(&write_cond, &mutex);
  121.     }
  122.     pthread_mutex_unlock(&mutex);
  123. }
  124. void signal_consumers()
  125. {
  126.     int i;
  127.     pthread_mutex_lock(&mutex);
  128.     readers_done = 0;
  129.     for (i = 0; i < num_readers; i++) done[i] = 0;
  130.     pthread_cond_broadcast(&read_cond);
  131.     pthread_mutex_unlock(&mutex);
  132. }
  133. void signal_producer(id)
  134. int id;
  135. {
  136.     pthread_mutex_lock(&mutex);
  137.     readers_done++;
  138.     if (readers_done == num_readers) {
  139.         pthread_cond_signal(&write_cond);
  140.     }
  141.     done[id]  = 1;
  142.     pthread_mutex_unlock(&mutex);
  143. }
  144. void wait_for_producer(id)
  145. int id;
  146. {
  147.     pthread_mutex_lock(&mutex);
  148.     if (done[id]) pthread_cond_wait(&read_cond, &mutex);
  149.     pthread_mutex_unlock(&mutex);
  150. }
  151.  
  152.  
  153.  
  154.