IPC микроядра на примере QNX Neutrino. Передача сообщений (функции send, receive, reply), страница 5

SIGEV_PULSE_INIT( &event, coid, SIGEV_PULSE_PRIO_INHERIT,

MY_PULSE2, 0 );

MsgSendPulse( fd, 10, MY_PULSE1, 21 );  

MsgSendPulse( fd, 10, MY_PULSE2, 8 );

name_close( fd );

return 0;  

}

Server

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <errno.h>

#include <sys/neutrino.h>

#include <sys/iomsg.h>

#include <sys/iofunc.h>

#include <sys/dispatch.h>

#define ATTACH_POINT "channel_msg_"

#define MY_PULSE1  _PULSE_CODE_MINAVAIL+1

#define MY_PULSE2  _PULSE_CODE_MINAVAIL+2

int main(){

name_attach_t *attach;

int   rcvid, val, coid;

struct _pulse     pulse;

struct sigevent   event;

if ((attach = name_attach(NULL, ATTACH_POINT, 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;

}

Результаты:

$ gcc client.c -o client

$ gcc server.c -o server

$ ./server&

[2] 1556522

$ ./client

Get Pulse 1 : value = 21

Get system pulse

Client disconnect

Get Pulse 2 : value = 8

$

2.3.2.2 потоками одного процесса,

#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