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

  1. Listing 3 (lidar_acq.c)
  2. /* ---
  3.    lidar_acq task.  This is the task synchronization task.
  4.    This program initialiazes shared memory and the message
  5.    queues for the real-time portion of the lidar program.
  6. --- */
  7.  
  8. #include    "lidar.h"
  9.  
  10. main()
  11. {
  12.     int     i, j, trigger_fd, alive_sem, shm_seg_id;
  13.     int     avail_qid, graph_qid, read_qid, write_qid;
  14.     message mptr;
  15.     trigger_clock cycles;
  16.  
  17.     /* --- set routine priority --- */
  18.     setpriority (PRIO_PROCESS, 0, LIDAR_ACQ);
  19.  
  20.     /* --- open task alive semaphore --- */
  21.     alive_sem = sem_get(ALIVE_KEY, 0); 
  22.  
  23.     /* --- open message queues --- */
  24.     avail_qid = msgget (AVAILABLE_Q, 0666 | IPC_CREAT);
  25.     graph_qid = msgget (GRAPHICS_Q, 0666 | IPC_CREAT);
  26.     read_qid = msgget (READ_Q, 0666 | IPC_CREAT);
  27.     write_qid = msgget (WRITE_Q, 0666 | IPC_CREAT);
  28.  
  29.     /* --- create shared memory segment ids --- */
  30.     shm_seg_id = shmget (FB_KEY, FRAME_SIZE * N_FRAMES, 
  31.                          0666 | IPC_CREAT);
  32.  
  33.     /* --- create messages for shared memory 
  34.            indexing  and place them in the available
  35.            message queue --- */
  36.     for (i = 0; i < N_FRAMES; i++) {
  37.         mptr.key = i;
  38.         if (msgsnd (avail_qid, &mptr, MSG_SIZE, 0) < 0) {
  39.            printf ("Queue problem.\n");
  40.            exit (1);
  41.            }
  42.         }
  43.  
  44.     /* --- wait for the read, write, and graph 
  45.            tasks to check in --- */
  46.     sem_wait (alive_sem);
  47.     sem_wait (alive_sem);
  48.     sem_wait (alive_sem);
  49.  
  50.     /* --- open trigger timing device and set to
  51.            10 Hz for N_RUNS pulses --- */
  52.     trigger_fd = open("/dev/tclock", O_RDWR);
  53.     cycles.count = 250;         /* 250 cycles = .1 sec */
  54.     cycles.int_count = N_RUNS;  /* number of pulses    */
  55.     i = write (trigger_fd, &cycles, 4);
  56.  
  57.     /* --- real time data acquisition loop --- */
  58.     for (i = 0; i < N_RUNS; i++) {
  59.         /* --- wait for trigger --- */
  60.         if (read (trigger_fd, &cycles, 2)) {
  61.             printf ("Missed a pulse. Fatal Error\n");
  62.             break;
  63.             }
  64.  
  65.         /* --- get a free frame buffer --- */
  66.         j = msgrcv (avail_qid, &mptr, MSG_SIZE, 0, IPC_NOWAIT);
  67.  
  68.         /* --- if there aren't any available buffers --- */
  69.         if (j < 0) {
  70.             /* --- remove all of the frame buffers
  71.                    from the graph message queue and
  72.                    place them in the available queue  --- */
  73.             while (msgrcv(graph_qid, &mptr, MSG_SIZE, 
  74.                    ANYTYPE, IPC_NOWAIT) >= 0) 
  75.                msgsnd (avail_qid, &mptr, MSG_SIZE, 0);
  76.  
  77.             /* --- if there still are no buffers,
  78.                    fatal error has occured -- */
  79.             if (msgrcv (avail_qid, &mptr, MSG_SIZE,
  80.                         ANYTYPE, IPC_NOWAIT) == -1) {
  81.                printf ("Fatal queue problem. Terminating\n");
  82.                break;
  83.                }
  84.             }
  85.  
  86.         /* --- start the acquisition process --- */
  87.         msgsnd (read_qid, &mptr, MSG_SIZE, IPC_NOWAIT);
  88.         }
  89.    
  90.     /* --- terminate data acquisition --- */
  91.     close(trigger_fd);
  92.  
  93.     /* --- signal other processes to quit --- */
  94.     mptr.key = QUIT;
  95.     msgsnd (read_qid, &mptr, MSG_SIZE, NOFLAGS);
  96.  
  97.     /* --- wait for all messages to be placed
  98.             on available Q and retire --- */ 
  99.     for (i = 0; i < N_FRAMES; i++) 
  100.        msgrcv(avail_qid, &mptr, MSG_SIZE, ANYTYPE, NOFLAGS);
  101.  
  102.     /* --- and the terminate message --- */
  103.     msgrcv (avail_qid, &mptr, MSG_SIZE, ANYTYPE, NOFLAGS);
  104.  
  105.     /* --- delete queues --- */
  106.     msgctl (avail_qid, IPC_RMID, 0);
  107.     msgctl (read_qid, IPC_RMID, 0);
  108.     msgctl (write_qid, IPC_RMID, 0);
  109.     msgctl (graph_qid, IPC_RMID, 0);
  110.  
  111.     /* --- release semaphore --- */
  112.     sem_delete (ALIVE_KEY);
  113.  
  114.     /* --- delete shared memory segments --- */
  115.     shmctl (shm_seg_id, IPC_RMID, 0);
  116. }
  117.