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

typedef struct _my_date{

struct _pulse     pulse;

char  buf[255];

} my_dat;

int main(){

name_attach_t *attach;

struct _msg_info *info;

int   nbyte=255, rcvid, val;

char  buf[255];

my_dat dat;

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

printf("name attach fail\n");

return EXIT_FAILURE;

}

while(1){

rcvid = MsgReceive( attach->chid, &buf[0], sizeof(buf), NULL );

if( rcvid > 0 ){

//Message

printf("SERVER:\n  Serv get = %s\n", buf );

strcpy( buf, "Server pulse" );

MsgReply( rcvid, 0, &buf[0], sizeof( buf ) );

} else if( rcvid == 0 ){

//Pulse

printf("Server get pulse!\n" );

}

}//end while(1)

name_detach(attach, 0);

return 0;

}

Результаты:

$ gcc server.c -o server

$ gcc client.c -o client

$ ./server&

[1] 1380393

$ ./client

SERVER:

Serv get = Client pulse

CLIENT:

Client get = Server pulse

Server get pulse!

$

Сетевой вариант данной программы работает корректно, так же как и локальный.

               2.3.1.2 между родственными процессами,

client:

#include <sys/iofunc.h>

#include <sys/dispatch.h>

#include <stdlib.h>

#define PATH "channel_pulse_"

int main (int argc,char* argv[])

{

int   coid;

int   status;

int pid=getppid();

char  send_buf[25],

reply_buf[25];

printf("%d", pid );

int fd;

char  sbuf[255], rbuf[255];

fd = name_open( PATH, 0 );

if( fd == -1 ){

printf("Client name_open error\n" );

exit( EXIT_FAILURE );

}

strcpy( sbuf, "Client pulse" );

MsgSend( fd, &sbuf[0], sizeof(sbuf), &rbuf[0], sizeof(rbuf) );

printf("CLIENT:\n  Client get = %s \n", rbuf );

name_close( fd );

return 0;  

}

Server

#include <sys/iofunc.h>

#include <sys/dispatch.h>

#include <stdlib.h>

#include <stdio.h>

#include <stddef.h>

#include <process.h>

#define ATTACH_POINT "channel_pulse_"

typedef struct _my_date{

struct _pulse     pulse;

char  buf[255];

} my_dat;

int main (void)

{

int   rcvid, chid, pid, ppid;

char  receive_buf[25], reply_buf[25];

chid=ChannelCreate(0);

pid=getpid();

ppid=getppid();

printf("\n\n FATHER PARAM: pid=%d  ppid=%d\n", pid, ppid);

if(fork()==0)   execl("client", "client", NULL);

name_attach_t *attach;

struct _msg_info *info;

int   nbyte=255, val;

char  buf[255];

my_dat dat;

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

printf("name attach fail\n");

return EXIT_FAILURE;

}

while(1){

rcvid = MsgReceive( attach->chid, &buf[0], sizeof(buf), NULL );

if( rcvid > 0 ){

//Message

printf("SERVER:\n  Serv get = %s\n", buf );

strcpy( buf, "Server pulse" );

MsgReply( rcvid, 0, &buf[0], sizeof( buf ) );

} else if( rcvid == 0 ){

//Pulse

printf("Server get pulse!\n" );

}

}//end while(1)

name_detach(attach, 0);

return 0;

}

Результаты:

# ./server

FATHER PARAM: pid=1261605  ppid=602159

name attach fail

# SERVER:

Serv get = Client pulse

1CLIENT:

Client get = Server pulse

Server get pulse!

2.3.2 передача специальными функциями

2.3.2.1 между независимыми процессами

client:

#include <sys/neutrino.h>

#include <stdlib.h>

#include <stdio.h>

#include <sys/iofunc.h>

#include <sys/dispatch.h>

#define PATH "channel_msg_"

#define MY_PULSE1  _PULSE_CODE_MINAVAIL+1

#define MY_PULSE2  _PULSE_CODE_MINAVAIL+2

struct mes{

short type;

struct sigevent event;

};

int main(){

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 );

}