Рис. 8
Для сравнения проанализируем результаты выполнения программы, выполненной средствами POSIX.
Листинги исходных кодов
posix_client.c
#include <mqueue.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/neutrino.h>
#include <errno.h>
#define PROGNAME "posix_client: "
int main(int argc, char *argv[]) {
char *smsg = "HELLO! IT IS CLIENT"; // A pointer to a buffer that contains the message that we want to send
mqd_t mq_des; // The message-queue descriptor, returned by mq_open()
struct mq_attr mq_stat; // Structure to store the attributes of the message queue
sleep( 2 );
// Open a message queue
if( (mq_des = mq_open("/serv_mq", O_WRONLY | O_CREAT, 0777, NULL)) == -1){
perror( PROGNAME "mq_open" );
exit( EXIT_FAILURE );
}
// Get a message queue's attributes
mq_getattr( mq_des, // The message-queue descriptor, returned by mq_open()
&mq_stat); // Structure to store the attributes of the message queue
// Send a message to a queue
if( mq_send ( mq_des, // The ID of a channel that we established by calling ChannelCreate()
smsg, // A pointer to a buffer where the function can store the received data
strlen( smsg )+ 1, // The size of the buffer
NULL ) == -1 ){
perror( PROGNAME "mq_send" );
exit( EXIT_FAILURE );
}
// printf ( "Message send\n " );
mq_close( mq_des );
return EXIT_SUCCESS;
}
posix_server.c
#include <mqueue.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/neutrino.h>
#include <errno.h>
#define PROGNAME "posix_server: "
int main(int argc, char *argv[]) {
mqd_t mq_des; // The message-queue descriptor, returned by mq_open()
struct mq_attr mq_stat; // Structure to store the attributes of the message queue
char *msg [ 512 ]; // A pointer to a buffer where the function can store the received data
// Open a message queue
if( (mq_des = mq_open("/serv_mq", O_RDONLY | O_CREAT, 0777, NULL)) == -1){
perror( PROGNAME "mq_open" );
exit( EXIT_FAILURE );
}
// Get a message queue's attributes
mq_getattr( mq_des, // The message-queue descriptor, returned by mq_open()
&mq_stat); // Structure to store the attributes of the message queue
while ( 1 ){
// Receive a message from a queue
if( mq_receive ( mq_des, // The ID of a channel that we established by calling ChannelCreate()
msg, // A pointer to a buffer where the function can store the received data
mq_stat.mq_msgsize, // The size of the buffer
NULL ) == -1 ){
perror( PROGNAME "mq_receive" );
exit( EXIT_FAILURE );
}
// printf ( "Receive message: = %s \n ", msg );
}
mq_close(mq_des);
mq_unlink("/serv_mq");
return EXIT_SUCCESS;
}
Результаты при последовательном запуске двух клиентов:
Уважаемый посетитель!
Чтобы распечатать файл, скачайте его (в формате Word).
Ссылка на скачивание - внизу страницы.