home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_03 / 9n03058a < prev    next >
Text File  |  1990-12-10  |  2KB  |  97 lines

  1. Listing 6 (lidar_graph.c) 
  2. /* ---
  3.    lidar_graph task.  This program processes 
  4.    the raw frame buffer.  Frame buffers are received
  5.    through the graph q, when processing is finished, 
  6.    the frame buffer is placed on the available queue.
  7.  
  8.    For the sample program, processing and graphing 
  9.    are replaced by a call to delay which uses 
  10.    DELAY_TIME cpu time.
  11. --- */
  12.  
  13. #include <stdio.h>
  14. #include <signal.h>
  15. #include "lidar.h"
  16.  
  17. #define DELAY_TIME 0.3
  18.  
  19. main ()
  20. {
  21.     int     terminate = 0;
  22.     int     graph_qid, avail_qid;
  23.     char    *fb_shm;
  24.     int     alive_sem;
  25.     message mptr;
  26.  
  27.     /* --- set process priority --- */
  28.     setpriority (PRIO_PROCESS, 0, LIDAR_GRAPH);
  29.  
  30.     /* --- open message queues --- */
  31.     graph_qid = msgget (GRAPHICS_Q, 0666 | IPC_CREAT);
  32.     avail_qid = msgget (AVAILABLE_Q, 0666 | IPC_CREAT);
  33.  
  34.     /* --- create alive semaphore --- */
  35.     alive_sem = sem_get (ALIVE_KEY, 0);
  36.  
  37.     /* -- create shared memory buffers  -- */
  38.     fb_shm = shmat (shmget (FB_KEY, FRAME_SIZE * N_FRAMES,
  39.                     0666 | IPC_CREAT), 0, 0);
  40.  
  41.     /* --- tell the world we're open for business --- */
  42.     sem_signal (alive_sem);
  43.  
  44.     /* --- while lidar program running --- */
  45.     while (!terminate) {
  46.        /* --- wait for the next buffer --- */
  47.        msgrcv (graph_qid, &mptr, MSG_SIZE, ANYTYPE, NOFLAGS);
  48.  
  49.        /* --- check for termination --- */
  50.        if (mptr.key == QUIT)
  51.           terminate = 1;
  52.        else 
  53.           delay (DELAY_TIME);
  54.  
  55.        /* --- place buffer on the availaible queue --- */
  56.        msgsnd (avail_qid, &mptr, MSG_SIZE, IPC_NOWAIT);
  57.        }
  58.  
  59.     /* -- release memory -- */
  60.     shmdt (fb_shm);
  61. }
  62.  
  63. int cpu_hog_func ();
  64. int take_cpu_time = 1;
  65. #define SETTIMER(t,p)  \
  66.       t.it_value.tv_sec = (int)delta; \
  67.       t.it_value.tv_usec = (p - (int)p) * 1000000; \
  68.       setitimer (ITIMER_PROF, &t, 0);
  69.  
  70. /* --- delay and grab cpu time --- */
  71. delay (delta)
  72. double delta;
  73. {
  74.    struct itimerval t;
  75.  
  76.    /* --- signal handler for profiler interrupt --- */
  77.    signal (SIGPROF, cpu_hog_func); 
  78.  
  79.    /* --- turn on timer --- */
  80.    SETTIMER(t,delta);
  81.  
  82.    /* --- loop until stop condition --- */
  83.    while (take_cpu_time) 
  84.       /* do nothing */;
  85.  
  86.    /* --- reset loop condition --- */
  87.    take_cpu_time++;
  88.  
  89.    /* --- turn off timer --- */
  90.    SETTIMER(t,0);
  91. }
  92.  
  93. cpu_hog_func ()
  94. {
  95.    take_cpu_time--;
  96. }
  97.