亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 2150 | 回復(fù): 0
打印 上一主題 下一主題

[C] sysv 消息隊列 能否用select設(shè)置超時 ? [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2008-03-25 22:30 |只看該作者 |倒序瀏覽
想實現(xiàn)一個獲取一個消息的例子,功能大概是這樣, msg_post 往消息隊列send 一個msg, 在另外一個線程中,讀該消息隊列,如果如果在限定的時間內(nèi), 消息隊列描述符可讀,則返回該消息,否則, 超時返回,但實際上,我發(fā)現(xiàn), 這樣做,總是超時返回. 不曉得是何緣故? 難道這樣實現(xiàn)本身就錯了?


以下是源代碼


struct MSGqueue_t *msg_get ( )
{
    struct timeval tv;
    fd_set  rset;
    int             ret;
    struct msgbuf   buf;
    struct MSGqueue_t       *msg;

try_select:
    tv.tv_sec = 5;
    tv.tv_usec = 0;

    FD_ZERO(&rset);
    FD_SET(msgfd, &rset);
    if ((ret = select (msgfd + 1, &rset, NULL, NULL, &tv) ) < 0)
    {
            if (errno == EINTR)     goto try_select;
            else    return NULL;
    }
    else if (ret == 0)    return NULL;

    if (msgrcv(msgfd, &buf, sizeof (struct msgbuf), 0, IPC_NOWAIT) < 0)
    {
            logfile (LOG_PRINT, "[%s,%d]recv msg failed, err = %s\n",
                  __FILE__, __LINE__, strerror (errno) );
            return NULL;
    }

    msg = (struct MSGqueue_t *) malloc (sizeof (struct MSGqueue_t) );
    if (!msg)
    {
            logfile (LOG_PRINT, "[%s,%d]alloc mem failed\n",
                  __FILE__, __LINE__ );
            return NULL;
    }

    memset(msg, 0, sizeof(struct MSGqueue_t));
    memcpy(msg, buf.mtext, sizeof(struct MSGqueue_t));
   
    return msg;
}

亟盼大俠幫忙.

[ 本帖最后由 gczh1006 于 2008-3-25 23:18 編輯 ]
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP