关于非阻塞模式下的sendmsg()

sunxphere posted @ 2012年9月13日 21:34 in network programming with tags linux net program , 7126 阅读

之前的服务程序使用了多进程模式,创建一对socketpair然后主进程用sendmsg向各个工作进程分发连接fd,工作进程中所有其他的连接都是非阻塞的,但由于传递套接字的消息很小且是单向的,就简单使用了阻塞模式,也没有处理异步读写事件。

最近要主进程和工作进程之间互相传递大量数据,需要把socketpair改成非阻塞的。于是问题就是,socketpair既要传递fd又要传输大量数据,在非阻塞模式下如果缓冲区满了,那么fd还能否传递失败的话会不会丢失;再如果缓冲又有可写空间,是否得重新发送fd。总之对sendmsg函数在非阻塞模式下的行为不甚了解,网上G了一圈也没有发现有说明此类问题的文章。

于是就试一下,代码如下:

#include "msgio.h"
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/socket.h>

static int setnonblocking( int fd )
{
    int opts;

    if( fd<=0 )
        return -1;

    opts = fcntl( fd, F_GETFL );
    if( opts<0 )
        return -1;

    if( fcntl( fd, F_SETFL, opts|O_NONBLOCK ) < 0 )
        return -1;

    return 0;
}

int main()
{
    int ret;
    int ss[2];
    int pid;

    ret = socketpair( AF_UNIX, SOCK_STREAM, 0, ss );
    if( ret != 0 )
    {
        perror( "create socket pair failed" );
        return 1;
    }

    pid = fork();
    switch( pid )
    {
        case -1:
            perror( "fork failed" );
            return 2;
        case 0:
            close( ss[0] );
            setnonblocking( ss[1] );
            recver( ss[1] );
            break;
        default:
            close( ss[1] );
            setnonblocking( ss[0] );
            sender( ss[0] );
            break;
    }

    if( pid )
    {
        sleep( 2 );
        kill( pid, SIGTERM );
        wait( &ret );
    }

    return 0;
}
#include "msgio.h"
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>

#define MSG_DATA "xyz"
#define MSG_LEN sizeof(MSG_DATA)

#define LOOP_TIMES 10000

#define print( fmt, ... ) \
    fprintf( stderr, fmt "\n", ##__VA_ARGS__ )


int sender( int fd )
{
    int ret;
    int i;
    int xfd;

    struct msghdr msghdr;
    struct iovec iov[4];
    union {
        struct cmsghdr cm;
        char data[CMSG_SPACE(sizeof(int))];
    } cmsg;

    xfd = open( "/dev/zero", O_RDONLY );
    if( xfd < 0 )
    {
        perror( "open xfd failed" );
        return -1;
    }

    cmsg.cm.cmsg_len = CMSG_LEN(sizeof(int));
    cmsg.cm.cmsg_level = SOL_SOCKET;
    cmsg.cm.cmsg_type = SCM_RIGHTS;
    *(int*)CMSG_DATA(&(cmsg.cm)) = xfd;

    iov[0].iov_base = MSG_DATA;
    iov[0].iov_len = MSG_LEN;
    iov[1].iov_base = MSG_DATA;
    iov[1].iov_len = MSG_LEN;
    iov[2].iov_base = MSG_DATA;
    iov[2].iov_len = MSG_LEN;
    iov[3].iov_base = MSG_DATA;
    iov[3].iov_len = MSG_LEN;

    msghdr.msg_name = NULL;
    msghdr.msg_namelen = 0;
    msghdr.msg_iov = iov;
    msghdr.msg_iovlen = 4;
    msghdr.msg_control = (caddr_t)&cmsg;
    msghdr.msg_controllen = sizeof(cmsg);

    print( "to send %d", MSG_LEN );

    for( i=0; i<2; i++ )
    {
        ret = sendmsg( fd, &msghdr, MSG_DONTWAIT );
        if( ret < 0 )
        {
            perror( "sendmsg failed" );
            continue;
        }

        print( "sendmsg %5d sent %d, fd %d", i, ret, xfd );
    }

    return 0;
}


int recver( int fd )
{
    int ret;
    int i;
    unsigned char buf[10];
    int xfd;

    struct msghdr msghdr;
    struct iovec iov[1];
    union {
        struct cmsghdr cm;
        char data[CMSG_SPACE(sizeof(int))];
    } cmsg;

    iov[0].iov_base = buf;
    iov[0].iov_len = sizeof(buf);

    msghdr.msg_name = NULL;
    msghdr.msg_namelen = 0;
    msghdr.msg_iov = iov;
    msghdr.msg_iovlen = 1;
    msghdr.msg_control = (caddr_t)&cmsg;
    msghdr.msg_controllen = sizeof(cmsg);

    sleep(1);

    for( i=0; i<4; i++ )
    {
        ret = recvmsg( fd, &msghdr, 0 );
        if( ret < 0 )
        {
            perror( "recvmsg failed" );
            continue;
        }

        if( cmsg.cm.cmsg_len < CMSG_LEN(sizeof(int)) )
        {
            print( "msg control data len %u", cmsg.cm.cmsg_len );
            continue;
        }
        if( cmsg.cm.cmsg_level != SOL_SOCKET || cmsg.cm.cmsg_type != SCM_RIGHTS )
        {
            print( "msg control level %d, type %d", cmsg.cm.cmsg_level, cmsg.cm.cmsg_type );
            continue;
        }

        xfd = *(int*)CMSG_DATA( &(cmsg.cm) );
        print( "recvmsg %5d recv %d, fd %d", i, ret, xfd );
        //close( xfd );
    }

    return 0;
}

测试结果为:

to send 4
sendmsg     0 sent 16, fd 4
sendmsg     1 sent 16, fd 4
recvmsg     0 recv 10, fd 3
recvmsg     1 recv 10, fd 5
recvmsg     2 recv 10, fd 5
recvmsg     3 recv 2, fd 5

父进程分两个发送了32字节数据,每次附带一个fd。子进程分四次接收,数据接收完整,也只收到了两个fd。

特别是第二次的接收,数据中包含父进程两次消息中的数据,但fd为第二个消息的fd。随后的接收到的都是同一个fd。

由此可以猜测,fd的传递和数据类似于两个通道。由于一个进程中的fd是唯一的,子进程在收到fd后可以判断出是否是新的fd。

将recver()函数中的buf大小改为 10*MSG_LEN 进行测试,结果为:

to send 4
sendmsg     0 sent 16, fd 4
sendmsg     1 sent 16, fd 4
recvmsg     0 recv 16, fd 3
recvmsg     1 recv 16, fd 5
recvmsg failed: Resource temporarily unavailable
recvmsg failed: Resource temporarily unavailable
可见两次发送的消息没有合并,猜测内核对此进行了处理,只允许一个消息中包含一个fd。

over

f88tw 说:
2021年4月09日 14:43

敬启者:个人小网站希望大家多多支持 感谢您对我们热心的支持 f88tw┃华歌尔┃I appreciate your kind assistance. f88tw| 粗工| 粗工内容 | 粗工| 粗工内容 |墓园|捡骨流程|捡骨费用|捡骨时间|禁忌|捡骨颜色|捡骨师|新竹|时间|台北|桃园|苗栗|头份|https://mypaper.m.pchome.com.tw/f88tw

seo service UK 说:
2024年1月16日 17:00

Great post, and great website. Thanks for the information

메이저토토 说:
2024年2月18日 15:06

It's my first time here. I found this board and found it really helpful and it helped me out a lot. I want to give something back and help others like you helped me.

안전토토사이트 说:
2024年2月18日 15:06

I’d also like to state that most of those that find themselves without the need of health insurance are normally students, self-employed and those that are jobless. More than half from the uninsured are under the age of Thirty five. They do not sense they are in need of health insurance since they are young along with healthy. Their income is normally spent on real estate, food, and also entertainment. Some people that do represent the working class either whole or not professional are not given insurance by means of their jobs so they go without because of the rising valuation on health insurance in america. Thanks for the tips you discuss through your blog.

안전토토사이트 说:
2024年2月18日 15:07

Driving forward Through You In Normal Need To Partake In The Standard Expansions Of Having A Standard Presence Destruction Then You Truly Need Not Go Out Conventionally. .You Can Other Than Help The Relationship Of Our Escorts Who Is Available to Driving Forward And In All Pieces Of The Country.

먹튀폴리스보증업체 说:
2024年2月18日 15:13

Thank you for sharing this useful information. I am very happy after reading this blog port because it is written in a good manner and on a good topic.

온라인카지노추천 说:
2024年2月18日 15:13

I spend time people’s writing. It has the excellent to consider you demonstrate around thoughts together with the intellect and also res while doing this very important topic is sometimes successfully deemed.

토토사이트추천 说:
2024年2月18日 15:14

These are all really solid tips. I can’t tell you how often I see a comment on one of the blogs I write for that amounts to “nice post” and nothing more. I’ve even worked at a couple of sites that delete comments that don’t add to the discussion!

토토사이트 说:
2024年2月18日 15:14

Your post is very helpful to get some effective tips to reduce weight properly. You have shared various nice photos of the same. I would like to thank you for sharing these tips. Surely I will try this at home. Keep updating more simple tips like this.

먹튀타운 说:
2024年2月18日 15:14

It's my first time here. I found this board and found it really helpful and it helped me out a lot. I want to give something back and help others like you helped me.

먹튀검증사이트 说:
2024年2月18日 15:15

For every one of your requirements,New Delhi Escorts will fulfill you in a most proficient way. They promise you incredible client support, a decent get, a no problem at all transportation and the best of diversion Our place Escorts from a legitimate agency in New Delhi to give you the best of New Delhi Escorts.

토랜드 说:
2024年2月18日 15:15

The Offscreen Film Festival 2018 promises to be an unforgettable cinematic experience. With a lineup of thought-provoking and boundary-pushing films, this festival is set to captivate audiences from all walks of life.

안전놀이터가입 说:
2024年2月18日 15:52

The Offscreen Film Festival 2018 promises to be an unforgettable cinematic experience. With a lineup of thought-provoking and boundary-pushing films, this festival is set to captivate audiences from all walks of life.

먹튀사이트 说:
2024年2月18日 15:52

Your blog is very helpful for beginners who want to learn Data science. I am also a Data science developer. I had done Data Science course from TGC India. They offer a variety of tutorials covering everything from the processes of Data Science to how to get started with Data Science.

파워볼 说:
2024年2月18日 15:53

Very impressive and interesting blog found to be well written in a simple manner that everyone will understand and gain the enough knowledge from your blog being more informative is an added advantage for the users who are going through it. Once again nice blog keep it up.

토토사이트 说:
2024年2月18日 15:55

Thanks for sharing very Nice Information for Us. Otherwise if anyone want to learn Python, Data Sciences and Python Frameworks. Expert Trainers, Project based Training, Placement assistance makes us different from others

카디즈 说:
2024年2月18日 15:56

Success is often dictated over the experience through the staffing firm you end up picking. The growing requirement of temporary staffing from the medical place has furnished locum tenens practitioners the luxury of working consistently and making use of their understanding along with experience in the appropriate process.

카지노커뮤니티 说:
2024年2月18日 16:52

Awesome blog! Do you have any tips and hints for aspiring writers? I’m planning to start my own website soon but I’m a little lost on everything. Would you propose starting with a free platform like WordPress or go for a paid option? There are so many choices out there that I’m totally overwhelmed .. Any recommendations? Many thanks! 

카지노사이트 说:
2024年2月18日 16:52

Very impressive and interesting blog found to be well written in a simple manner that everyone will understand and gain the enough knowledge from your blog being more informative is an added advantage for the users who are going through it. Once again nice blog keep it up.

카지노가입쿠폰 说:
2024年2月18日 16:53

Your post is very helpful to get some effective tips to reduce weight properly. You have shared various nice photos of the same. I would like to thank you for sharing these tips. Surely I will try this at home. Keep updating more simple tips like this.

안전토토사이트 说:
2024年2月18日 17:11

Your post is very helpful to get some effective tips to reduce weight properly. You have shared various nice photos of the same. I would like to thank you for sharing these tips. Surely I will try this at home. Keep updating more simple tips like this.

먹튀사이트조회 说:
2024年2月18日 17:11

good day i am so thrilled I placed your blog, I truly located you via mistake, even as i was looking on google for something else, anyways i am right here now and will just like to say thank for a splendid post and a all round unique website. Please do preserve up the exceptional paintings.

먹튀검증사이트 说:
2024年2月18日 17:12

Very impressive and interesting blog found to be well written in a simple manner that everyone will understand and gain the enough knowledge from your blog being more informative is an added advantage for the users who are going through it. Once again nice blog keep it up.

메이저사이트 说:
2024年2月18日 17:12

They Have Been In This Requiring Some Undertaking. In this way, They Respect The Attitude And Need Of Their Clients In a Clear Manner. . These Call Young women Respect That Different Clients Are Ensured Going To Have Different Systems.

메이저토토 说:
2024年2月18日 17:12

Success is often dictated over the experience through the staffing firm you end up picking. The growing requirement of temporary staffing from the medical place has furnished locum tenens practitioners the luxury of working consistently and making use of their understanding along with experience in the appropriate process.

메이저토토 说:
2024年2月18日 18:01

For every one of your requirements,New Delhi Escorts will fulfill you in a most proficient way. They promise you incredible client support, a decent get, a no problem at all transportation and the best of diversion Our place Escorts from a legitimate agency in New Delhi to give you the best of New Delhi Escorts.

메이저사이트 说:
2024年2月18日 18:02

Thank you for another informative blog. Where else could I get that kind of info written in such an ideal way? I’ve a project that I am just now working on, and I have been on the look out for such information.

온라인카지노순위 说:
2024年2月18日 19:05

For every one of your requirements,New Delhi Escorts will fulfill you in a most proficient way. They promise you incredible client support, a decent get, a no problem at all transportation and the best of diversion Our place Escorts from a legitimate agency in New Delhi to give you the best of New Delhi Escorts.

안전카지노사이트 说:
2024年2月18日 19:17

Definitely believe that which you said. Your favorite justification appeared to be on the net the simplest thing to be aware of. I say to you, I certainly get annoyed while people consider worries that they plainly don’t know about. You managed to hit the nail upon the top and defined out the whole thing without having side-effects , people can take a signal. 

토토사이트추천 说:
2024年2月18日 19:37

I love this project (Create an animal/plant) but I am not able to open the document/image up to print or even display a clear copy to use in the classroom. Is there any way you could send me a copy of this? Thank you for your time.

soccer friends 说:
2024年2月18日 19:58

The use of Avios for upgrades by British Airways, one of the first airlines to use a currency-based loyalty programme, is a key component of the airline’s dedication to patron happiness and loyalty.

먹튀사이트 说:
2024年2月18日 19:58

I am constantly surprised by the amount of information accessible on this subject. What you presented was well researched and well written to get your stand on this over to all your readers.

모두의토토 说:
2024年2月18日 21:06

I am constantly surprised by the amount of information accessible on this subject. What you presented was well researched and well written to get your stand on this over to all your readers.

먹튀검증 说:
2024年2月18日 21:06

Nice to be visiting your blog once more, it has been months for me. Well this article that ive been waited for therefore long. i want this article to finish my assignment within the faculty, and it has same topic together with your article. Thanks, nice share.

안전토토사이트 说:
2024年2月18日 21:06

I spend time people’s writing. It has the excellent to consider you demonstrate around thoughts together with the intellect and also res while doing this very important topic is sometimes successfully deemed.

메이저사이트 说:
2024年2月18日 21:13

Your post is very helpful to get some effective tips to reduce weight properly. You have shared various nice photos of the same. I would like to thank you for sharing these tips. Surely I will try this at home. Keep updating more simple tips like this.

먹튀검증사이트 说:
2024年2月18日 21:13

The best alternative to Backpage is out there and it is ebackpage.com. It has turned out to be the best Backpage replacement after its seizure. It has amassed the most traffic from the Backpage demise. This has been the best alternative to both the customers and consumers in the market.

seo service UK 说:
2024年2月24日 16:32

Excellent, this article is really helpful. Mes doubles dans autres produits


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter