Сигналы. Сетевая доставка сигналов. Системный вызов. Отправка сигнала процессу на удаленном узле. Программа приема сигнала, страница 10

msg_queue_id = mq_open( QUEUE_NAME, O_RDWR | O_CREAT, PERM, &msg_queue_attr );

if( msg_queue_id < 0 ) {

printf( "Server: create/open queue error!!!\n" );

exit(1);

}

/* Устновка диспозиции для SIGINT */

signal( SIGINT, sigint_handler );

/* Устанавливаем режим уведомления о сообщении сигналом SIGUSR1 */

signal( SIGUSR1, sigusr1_handler );

SIGEV_SIGNAL_INIT( &notification, SIGUSR1 );

if( mq_notify( msg_queue_id, &notification ) < 0 ) {

printf( "Server: set notify mode queue error!!!\n" );

exit(2);

}

/* Ожидаем сообщений от клиента  */

while(1) {

// Ожидаем сигнала //

pause( );

}

return EXIT_SUCCESS;

}

Файл mysignal.c. Обработчики сигналов.

#include <stdlib.h>

#include <signal.h>

#include <stdio.h>

#include <mqueue.h>

#include "mysignal.h"

char   buff[ BUF_LEN ];

uint_t prio;

/* Обработчик сигнала SIGINT */

void sigint_handler( int signo )

{

extern mqd_t        msg_queue_id;

/* Закрываем очередь сообщений */

if ( mq_close( msg_queue_id ) < 0 ) {

printf( "Server: close queue error!!!\n" );

exit(3);

}

/* Удаление очереди сообщений */

if ( mq_unlink( QUEUE_NAME ) < 0 ) {

printf( "Server: delete queue error!!!\n" );

exit(4);

}

printf( "Server: terminate\n" );

exit(0);

}

/* Обработчки сигнала SIGUSR1 */

void sigusr1_handler( int signo )

{

extern mqd_t              msg_queue_id;

extern struct sigevent    notification;

/* Принимаем сообщение */

int n;

n = mq_receive( msg_queue_id, buff, BUF_LEN, &prio );

if ( n < 0 ) {

printf( "Server: message receive error!!!\n" );

return;

}

buff[n] = '\0';

printf( "Server: message received, length = %d\n", n );

printf( "Message: %s\n", buff );

printf( "Prio: %d\n", prio );

/* Отправляем клиенту уведомление */

strcpy( buff, "OK" );

buff[2] = '\0';

prio = 31;

if ( mq_send( msg_queue_id, buff, strlen(buff), prio ) < 0 ) {

printf( "Server: send message error!!!\n" );

return;

}

/* Восстанавливаем диспозицию */

signal( SIGUSR1, sigusr1_handler );

/* Перерегистрируемся */

if ( mq_notify( msg_queue_id, &notification ) < 0 ) {

printf( "Server: set notify mode queue error!!!\n" );

return;

}

}

Листинг 6.2. Программа сервера. 2_6_posix_msg_client.с.

/*

* Сообщения Posix

* Программа клиента

*/

#include <stdlib.h>

#include <stdio.h>

#include <unistd.h>

#include <string.h>

#include <mqueue.h>

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

{

mqd_t               msg_queue_id_rd,

msg_queue_id_wr;

struct mq_attr      msg_queue_attr;

int                       max_len;

char*               buff;

uint_t                    prio;

if (argc < 3) {

printf( "Client: command line args error!!!\n" );

exit(2);

}

// Получаем доступ к очереди //

msg_queue_id_rd = mq_open( argv[1], O_RDONLY );

msg_queue_id_wr = mq_open( argv[1], O_WRONLY );

if( msg_queue_id_rd < 0 || msg_queue_id_wr < 0 ) {

printf( "Client: open queue error!!!\n" );

exit(3);

}

prio = atoi( argv[2] );

if (mq_getattr( msg_queue_id_rd, &msg_queue_attr ) < 0) {

printf( "Client: get queue attr error!!!\n" );

exit(4);

}

max_len = msg_queue_attr.mq_msgsize;

buff = (char*)malloc( max_len );

printf( "Client: connect is OK.\n" );

printf( "Message: " );

scanf( "%s", buff );

// Оправка сообщения серверу //

if ( mq_send( msg_queue_id_wr, buff, strlen(buff), prio ) < 0 ) {

printf( "Client: send message error!!!\n" );

exit(5);

}

printf( "Client: send message is OK.\n" );

mq_close( msg_queue_id_wr );

// Прием ответа от сервера //

int n=0;