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

client_log = fopen( file_name, "w" );

if( client_log == NULL ) {

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

exit(2);

}

// Отключаем буферизацию вывода //

setbuf( stdout, NULL );

// Открытие канала //

connect_id = name_open( ATTACH_POINT, flag );

if( connect_id == -1 )

{

fprintf( client_log, "Client: name open error!\n" );

exit(3);

}

fprintf( client_log, "Client: started. PID=%d.\n", getpid() );

signal( SIGINT, sigint_handler );

int i=0;

while(1)

{

// printf( "Message: ");

// scanf( "%s", send_msg.msg_buff );

sprintf( send_msg.msg_buff, "hello msg %d, client PID=%d", ++i, getpid() );

send_msg.type = CLIENT_REQUEST;

fprintTime( );

fprintf( client_log, "SEND MSG:\n%s\n", send_msg.msg_buff );

if ( MsgSend( connect_id, &send_msg, sizeof(send_msg), reply_buff, sizeof(reply_buff) ) == -1 )

{

fprintTime( );

fprintf( client_log, "Client: send msg error!\n" );

break;

}

fprintTime( );

fprintf( client_log, "REPLY MSG:\n%s\n", reply_buff );

sleep( 2 );

fflush( stdout );

}

// Закрытие локального канала //

name_close( connect_id );

return EXIT_SUCCESS;

}

void sigint_handler( int signo )

{

// Закрытие канала //

name_close( connect_id);

fprintf( client_log, "Terminate.\n" );

fclose( client_log );

exit(0);

}

void fprintTime( )

{

time_t currentTime;

struct tm *timm;

currentTime = time( NULL );

timm = localtime( &currentTime );

fprintf( client_log, "%2.2d:%2.2d:%2.2d -  ", timm->tm_hour, timm->tm_min, timm->tm_sec );

}

Для фиксации результатов работы сервер и все клиенты и агенты ведут лог-файлы. По которым можно посмотреть, запросы какого клиента обрабатывал конкретный агент, события типа постановки клиента в очередь и т. д.

Результаты работы программы. Программы тестировались в лаборатории. Сервер запускался и порождал агентов, клиенты запускались в фоновом режиме.

Часть лог-файла сервера для конфигурации 1 сервер, 3 агента и 7 клиентов.

Листинг 7.5. Server_log.txt

21:40:34 - Server: started. 21:40:34 - Server: agent 1 started. 21:40:34 - Server: agent 2 started. 21:40:34 - Server: agent 3 started. 21:40:34 - Server: register agent 2: RECV_ID=3. 21:40:34 - Server: register agent 3: RECV_ID=4. 21:40:34 - Server: register agent 4: RECV_ID=5. 21:40:42 - Server: request client ID=6 transfer to agent ID=3 21:40:43 - Server: request client ID=7 transfer to agent ID=4 21:40:44 - Server: request client ID=8 transfer to agent ID=5 21:40:44 - Server: not enough free agents. Client ID=9 push queue. 21:40:44 - Server: Agent ID=3 reply Clien ID=6 21:40:44 - Sever: Agent ID=3 get client in queue ID=9 21:40:45 - Server: not enough free agents. Client ID=10 push queue. 21:40:45 - Server: Agent ID=4 reply Clien ID=7 21:40:45 - Sever: Agent ID=4 get client in queue ID=10 21:40:45 - Server: not enough free agents. Client ID=11 push queue. 21:40:46 - Server: Agent ID=5 reply Clien ID=8 21:40:46 - Sever: Agent ID=5 get client in queue ID=11 21:40:46 - Server: not enough free agents. Client ID=12 push queue. 21:40:46 - Server: not enough free agents. Client ID=6 push queue. 21:40:46 - Server: Agent ID=3 reply Clien ID=9 21:40:46 - Sever: Agent ID=3 get client in queue ID=12 21:40:47 - Server: Agent ID=4 reply Clien ID=10 21:40:47 - Sever: Agent ID=4 get client in queue ID=6 21:40:47 - Server: not enough free agents. Client ID=7 push queue. 21:40:48 - Server: not enough free agents. Client ID=8 push queue. 21:40:48 - Server: Agent ID=5 reply Clien ID=11 21:40:48 - Sever: Agent ID=5 get client in queue ID=7 21:40:48 - Server: Agent ID=3 reply Clien ID=12 21:40:48 - Sever: Agent ID=3 get client in queue ID=8 21:40:48 - Server: not enough free agents. Client ID=9 push queue. 21:40:49 - Server: Agent ID=4 reply Clien ID=6 21:40:49 - Sever: Agent ID=4 get client in queue ID=9 21:40:49 - Server: not enough free agents. Client ID=10 push queue. 21:40:50 - Server: not enough free agents. Client ID=11 push queue. 21:40:50 - Server: Agent ID=5 reply Clien ID=7 21:40:50 - Sever: Agent ID=5 get client in queue ID=10 21:40:50 - Server: Agent ID=3 reply Clien ID=8 21:40:50 - Sever: Agent ID=3 get client in queue ID=11 21:40:50 - Server: not enough free agents. Client ID=12 push queue.