Multiplex Select Poll Epoll

Source: Internet
Author: User
#include <stdio.h> #include <fcntl.h> #include <sys/select.h> #include <errno.h> #include <
    stdlib.h> #include <unistd.h> #include <string.h> #define BUF_SIZE 1024 int main () {int fds[2];
    Char Buf[buf_size];
    Bzero (buf, buf_size);
    int i, RC, maxfd;
    Fd_set Watchset;

    Fd_set inset; if ((fds[0] = open ("P1", o_rdonly|
        O_nonblock)) < 0) {perror ("open");
    Exit (1); } if ((Fds[1] = open ("P2", o_rdonly|
        O_nonblock)) < 0) {perror ("open");
    Exit (1);
    } fd_zero (&watchset);
    Fd_set (Fds[0], &watchset);

    Fd_set (Fds[1], &watchset);
    MAXFD = (Fds[0] > fds[1]? fds[0]: fds[1]);
            while (Fd_isset (Fds[0], &watchset) | |
        Fd_isset (Fds[1], &watchset)) {inset = Watchset;
            if (select (MAXFD, &inset, NULL, NULL, NULL) < 0) {perror ("select");
        Exit (1); for (i = 0; I < 2; i++) {if (Fd_isset (fds[i), &inset)) {rc = read (fds[i), buf, Buf_size)
                ;
                    if (RC < 0) {perror ("read");
                Exit (1);
                    else if (!RC) {close (fds[i]);
                FD_CLR (Fds[i],&watchset);
                    else {BUF[RC] = 0;
                printf ("read%s", buf);
return 0; }


#include <stdio.h> #include <unistd.h> #include <sys/poll.h> #include <fcntl.h> #include <
    stdlib.h> int main () {struct POLLFD fds[2];
    Char buf[4096];

    int I, RC, ret; if (FDS[0].FD = open ("P1", o_rdonly|
        O_nonblock)) < 0) {perror ("open");
    Exit (1); } if (fds[1].fd = open ("P2", o_rdonly|
        O_nonblock)) < 0) {perror ("open");
    Exit (1);
    } fds[0].events = Pollin;

    Fds[1].events = Pollin; while (fds[0].events | | fds[1].events) {if (ret = Poll (FDS, 2,-1)) < 0) {perror ("poll
            ");
        Exit (1);                
                for (i = 0; i < 2; i++) {if (fds[i].revents) {
                rc = Read (FDS[I].FD, buf, sizeof (BUF)-1);
                    if (RC < 0) {perror ("read");
                Exit (1);
    else if (rc = 0)            {fds[i].events = 0;
                    else {BUF[RC] = 0;
                printf ("read%s", buf);
 }
            }
        }
    }
}

#include <stdio.h> #include <fcntl.h> #include <unistd.h> #include <stdlib.h> #include <sys/

poll.h> #include <sys/epoll.h> void addevent (int, char *);
    int main (int argc, char * argv[]) {char buf[4096];
    int I, RC;
    int EPFD;
    struct Epoll_event events[2];
    int num;

    int Numfds;
    EPFD = Epoll_create (2);
        if (EPFD < 0) {perror ("epoll_create");
    Exit (1);
    } addevent (EPFD, "P1");

    Addevent (EPFD, "P2");
    Numfds = 2; 
            while (Numfds) {if (num = epoll_wait (EPFD, events, 2,-1)) < 0) {perror ("epoll_wait");
        Exit (1);
            for (i = 0; i < num i++) {rc = read (EVENTS[0].DATA.FD, buf, sizeof (BUF)-1);
                if (RC < 0) {perror ("read");
            Exit (1); else if (!RC) {if Epoll_ctl (EPFD, Epoll_ctl_del, Events[i].daTA.FD, &events[i])) {perror ("Epoll_ctl");
                Exit (1);
                } else {BUF[RC] = 0;

            printf ("read%s", buf);
}} close (EPFD);
    } void addevent (int epfd, char * filename) {int fd;
    struct Epoll_event event; if (FD = open (filename, o_rdonly|
        O_nonblock)) < 0) {perror ("error!");
    Exit (1);
    } event.events = Epollin;
    EVENT.DATA.FD = FD;
        if (Epoll_ctl (EPFD, Epoll_ctl_add, FD, &event)) {perror ("Epoll_ctl");
    Exit (1);
 }    
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.