- 论坛徽章:
- 0
|
如何用 select()来 从多个消息队列读取消息
#include <sys/time.h>;
#include <sys/select.h>;
#include <sys/types.h>;
#include "crtl.h"
main() {
char file_name[50];
char s_para[50];
int snd_key = 0;
int rcv_key = 0;
int msgsnd_id = 0;
int msgrcv_id = 0;
int maxfd;
int FdReady;
fd_set readset;
memset( s_para, 0, sizeof(s_para) );
FD_ZERO( &readset );
memset( file_name, 0, sizeof(file_name) );
snprintf( file_name, 49, "%s/confpara/para.ini", getenv("SCHE_PATH" );
/*--------读取发送消息队列的键值----------------------------------*/
memset( s_para, 0, sizeof(s_para) );
if( read_para( file_name, "MSGSENDKEY", s_para) == 0 )
return(-1);
snd_key = StrToInt16WithSize(s_para, sizeof(s_para) );
/*--------读取接收消息队列的键值----------------------------------*/
memset( s_para, 0, sizeof(s_para) );
if( read_para( file_name, "MSGRECVKEY", s_para) == 0 )
return(-1);
rcv_key = StrToInt16WithSize(s_para, sizeof(s_para) );
if( (msgsnd_id = msg_accessing( snd_key ) ) < 0 )
return( -1 );
if( (msgrcv_id = msg_accessing( rcv_key ) ) < 0 ) {
return( -1 );
}
printf(" snd_id = %d, rcv_id= %d\n", msgsnd_id, msgrcv_id);
if( msgsnd_id >; msgrcv_id ) {
maxfd = msgsnd_id;
} else {
maxfd = msgrcv_id;
}
/*上面是连接消息队列*/
FD_ZERO( &readset);
FD_SET( msgsnd_id, &readset );/*51行*/ FD_SET( msgrcv_id, &readset );
FdReady = select( maxfd+1, &readset, 0, 0, 0 );
printf( "fd %d is ready.\n", FdReady );
}
运行后出core了,dbx core的信息是:Segmentation fault in main at line 51 in file "select.c" |
|