LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1324|回复: 3

等待消息队列怎么设置超时呢?

[复制链接]
发表于 2006-7-15 18:08:54 | 显示全部楼层 |阅读模式
想用select  但运行不起  大家帮我看看好麻?
void *function1( void *ptr );
void *function2( void *ptr );
int readable_time (long fd, int sec);

struct msg_send
{
        long mtype; /* 消息类型 */  
        int fd;//相关套接口
        char mtext[350]; /* 消息的文本 */
}msend, mrecv;
key_t sendkey = 0x1212;

main( )
{
        long msgid;
        msgid = msgget(sendkey, IPC_CREAT | 0666);
        if (msgid < 0)
                perror ("msgget_send:");
        printf ("MSGID_SEND is %ld\n", msgid);

    pthread_t thread1;
    pthread_create( &thread1, NULL, function1, NULL);
        while (true)
        {
                sleep (1);
                msend.mtype = 2;
                msend.fd = 1;
        printf ("11111111111111111\n");
                memcpy(msend.mtext, "12345423", sizeof ("12345423"));
                if (!msgsnd (msgid, &msend, sizeof (msend.mtext), IPC_NOWAIT) == 0){
                        perror ("msgsnd:\n");
                }
                sleep (10);

        }
    exit( 0 ) ;
}
   
void *function1( void *ptr )
{
        int sec = 20, a;
        long msgid;
        msgid = msgget(sendkey, IPC_CREAT | 0666);
        long fd = msgid;
        while (1)
        {
                a = readable_time (fd, sec);
                switch (a)
                {
                        case 0:
                                printf ("select overtime! \n");
                                break;
                        case -1:
                                printf ("select return an error\n");
                                break;
                        default:
                                printf ("there an msg come!\n");
                                if (msgrcv(msgid, &mrecv,sizeof (mrecv.mtext), 0, MSG_NOERROR) == -1){
                                        perror ("msgrcv\n");
                                }
                                struct msg_send msg;
                                msg.mtype = mrecv.mtype;
                                msg.fd = mrecv.fd;
                                memcpy (msg.mtext, mrecv.mtext, sizeof(mrecv.mtext));
                                printf ("type = %d, fd = %d, mtext = %s\n", msg.mtype, msg.fd, msg.mtext);
                }// endof swhitch
        }//endof while(1)
}
   
int readable_time (long fd, int sec)
{
        fd_set fdrset;
        struct timeval tv;
        FD_ZERO (&fdrset);
        FD_SET (fd, &fdrset);
        tv.tv_sec = sec;
        tv.tv_usec = 0;
        return (select(fd + 1, &fdrset, NULL, NULL, &tv));
}
执行到        FD_SET (fd, &fdrset);时候出现段错误  是否select不能用来设置消息队列超时? 如果这样 怎么达到这个目的呢?
 楼主| 发表于 2006-7-18 21:03:58 | 显示全部楼层
奇怪 在32位机器上就正常  我机器64就不能用  各位大侠 这是怎么回事 怎么解决???
回复 支持 反对

使用道具 举报

发表于 2006-7-19 00:03:54 | 显示全部楼层
不用线程, 用进程试试
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-7-19 10:51:41 | 显示全部楼层
Post by JBug
不用线程, 用进程试试

用进程好像也不行
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表