Проектирование с использованием IDE (Отчет о выполнении научно-исследовательской работы), страница 9

Обмен пульсами между потоками:

#include <pthread.h>

#include <stdio.h>

#include <sys/iofunc.h>

#include <sys/dispatch.h>

#include <stdlib.h>

#include <stddef.h>

#include <process.h>

#define PATH "channel_msg_"

#define MY_PULSE1  _PULSE_CODE_MINAVAIL+1

#define MY_PULSE2  _PULSE_CODE_MINAVAIL+2

pthread_t t1, t2, t3;

void* thread1( void* );

void* thread2( void* );

void* thread3( void* );

int main()

{

    pthread_create( &t1, NULL, thread1, NULL );

    pthread_create( &t2, NULL, thread2, NULL );

    pthread_create( &t3, NULL, thread3, NULL );    

    pthread_join( t1, NULL );

    pthread_join( t2, NULL );

    pthread_join( t3, NULL );   

    return;

}

void *thread3( void *t )

{

      int   fd, rcvid, chid, coid;

      struct _pulse pulse;

      name_attach_t *attach;

      struct sigevent   event;

      chid = ChannelCreate( 0 );

      coid = ConnectAttach( 0, 0, chid, 0, 0 );

      fd = name_open( PATH, 0 );

      if( fd == -1 ){

            printf("Client name open error\n" );

            exit( EXIT_FAILURE );

      }

      SIGEV_PULSE_INIT( &event, coid, SIGEV_PULSE_PRIO_INHERIT,

                    MY_PULSE2, 0 );

      MsgSendPulse( fd, 10, MY_PULSE2, 8 );

      name_close( fd );

      return 0;  

      }

void *thread2( void *t )

{

      int   fd, rcvid, chid, coid;

      struct _pulse pulse;

      name_attach_t *attach;

      struct sigevent   event;

      chid = ChannelCreate( 0 );

      coid = ConnectAttach( 0, 0, chid, 0, 0 );

      fd = name_open( PATH, 0 );

      if( fd == -1 ){

            printf("Client name open error\n" );

            exit( EXIT_FAILURE );

      }

      MsgSendPulse( fd, 10, MY_PULSE1, 21 );  

      name_close( fd );

      return 0;  

      }

void *thread1( void *t )

{

  name_attach_t *attach;

      int   rcvid, val, coid;

      struct _pulse     pulse;

      struct sigevent   event;

      if ((attach = name_attach(NULL, PATH, 0)) == NULL) {

            printf("name attach fail\n");

      return EXIT_FAILURE;

      }

      coid = ConnectAttach( 0, 0, attach->chid, 0, 0 );

      while(1){

            rcvid = MsgReceivePulse( attach->chid, &pulse, sizeof( pulse ), NULL );

            if( pulse.code > 0 ){

            //User Pulse

                  switch( pulse.code ){

                        case MY_PULSE1:

                             printf("Get Pulse 1 : value = %d\n", pulse.value.sival_int );

                        break;

                        case MY_PULSE2:

                             printf("Get Pulse 2 : value = %d\n", pulse.value.sival_int );

                        break;

                  }//end switch

            } else{

                  printf("Get system pulse\n");

                  if( pulse.code == _PULSE_CODE_DISCONNECT )

                        printf("Client disconnect\n");

             }

      }//end while(1)

      name_detach(attach, 0);

      return 0;

}

Рис. 20

Рис. 21