Wednesday 28 September 2011

Difference between select and poll

/*
@file name : poll.c
@brief: poll for data in to stdin
*/

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/poll.h>

#define MAX 100
#define TO_MS(val) (val*1000)

int main()
{
        char      line[MAX];
        int       ret;
        struct pollfd   fd[1];

        while(1)
        {

                fd[0].fd = 0;
                fd[0].events = POLLIN | POLLPRI | POLLERR | POLLHUP | POLLNVAL ;

                ret = poll(fd,1,TO_MS(6));
                printf("poll ret[%d]\n",ret);
                if ( ret > 0 && fd[0].revents)
                {
                        printf("event occur\n");
                }
                if(ret < 0 )
                {
                        printf("error [#%d,%s]\n",errno,strerror(errno));
                        return -1;
                }
                if(ret == 0)
                {
                        printf("time out\n");
                        return 0;
                }
                fgets(line,MAX,stdin);
                printf("%s",line);
        }
        return 0;
}



/*
@file name : select.c
@brief: select for data in to stdin
*/


#include <stdio.h>
#include <errno.h>
#include <string.h>

#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>


#define MAX 100
#define TO_MS(val) (val*1000)

int main()
{
        char      line[MAX];
        int       ret,fd=0;
        fd_set readfds;
        struct timeval timeout;

        while(1)
        {
                /* set the time out */
                timeout.tv_sec = 2;
                timeout.tv_usec = 0 ;

                /* initialize the fd set*/
                FD_ZERO(&readfds);

                /*set the action to the fd - here stdin*/
                FD_SET(0, &readfds);


                ret = select(fd + 1, &readfds, NULL, NULL, &timeout);
                if (ret == 0)
                {
                  /* timed out, return */
                        printf("Time out happend.!!\n");
                        break ;
                }
                else if (ret < 0)
                {
                        /* there is an error, return */
                        printf("Error Occurs Err[#%d,%s]\n",errno,strerror(errno));
                        break ;
                }
                else
                {
                        if (FD_ISSET (0, &readfds))
                        {
                            printf("data in to read\n");
                        }
                        else
                        {
                          printf("Strange..!! No Data available after fd set\n");
                          continue;
                        }
                }

                fgets(line,MAX,stdin);
                printf("%s",line);
        }

      return 0;
}
                                                                    

2 comments:

  1. poll : it polls for N number of file descriptors which adds into it with respect to the event specified for them.
    select: select is similar to poll,it couldn't have array of fd's to perform select operation.

    ReplyDelete
  2. Another difference is select is blocking call, it just blocks the fd set to it and wait for event to be occurred until event or time elapsed select will not unlock . Hence poll takes much CPU as i poll continues, whereas select not since select is blocking call.

    ReplyDelete