1

I am trying handle 4 different named unix pipes in a single listener process.

I tried to use the select to handle the file descriptor of the pipes.I opened all the named pipes in a non blocking mode

I am having a issue, select is not at all sleeping. Continuously running in a loop.

I dont know where is the problem in my code. I pasted my code below.

Always retaining the last file descriptor in select call eventhough it doesnt have a content in pipe.

Please suggest what is wrong in code?

Code

Pipe Open call(Invoked in Constructor)

diag_fd = open(DIAG_PIPE , O_RDONLY | O_NONBLOCK );

Main Loop

while(1)
    {

   FD_ZERO(&fds);
   FD_SET(cp_fd, &fds);
    FD_SET(diag_fd, &fds);
    FD_SET(err_fd, &fds);
    FD_SET(perf_fd, &fds);

    if (select(cp_fd+1, &fds, NULL, NULL, &tv) < 0)
    {
       perror("select");
       return ;
    }

    for (int fd = diag_fd; fd <= cp_fd; fd++)
    {

    if (FD_ISSET(fd, &fds))
    {

        if (fd == diag_fd)
        {               
            ProcessDiagLogs();
        }
        else if (fd == err_fd)
        {
            ProcessErrLogs();
        }
        else if (fd == perf_fd)
        {
            ProcessPerfLogs();
        }
            else if (fd == cp_fd)
        {
           ProcessCPLogs();
        }
     }
}

Read call in One File Descriptor:

ProcessDiagLogs()

do
{

    if ((num = read(diag_fd, s, BUF_LENGTH)) == -1)
        perror("read");
    else {
        s[num] = '\0';
        fputs(s, filed);
        fflush(filed);
         }
} while (num > 0);
Deanie
  • 2,316
  • 2
  • 19
  • 35
  • 1
    Which language is that supposed to be? It can't be two, decide for one! Also, can you extract a minimal example? See https://stackoverflow.com/help/how-to-ask – Ulrich Eckhardt May 14 '15 at 10:07
  • `nonblocking` and `select` logically exclude each other - as long as you arent writing a high-performance scheduler with very small slices – specializt May 14 '15 at 10:17
  • 2
    How and when is `tv` set? Also in terms of how the time-out is handled `select()` behaves differently depending on the platform. Could you narrow down the `unix` tag? – alk May 14 '15 at 10:38
  • Hi Ulrich , I am using c++ and my system in Solaris. – user2248680 May 15 '15 at 05:23
  • Hi alk. struct timeval tv = {1400, 0}; tv value is defined before the while loop call. – user2248680 May 15 '15 at 05:58
  • Your code ignores `read` returning zero. This [answer](http://stackoverflow.com/a/18176516/4880112) may be relevant. –  May 17 '15 at 04:15

1 Answers1

4

select() allows you to monitor one or more file descriptors, waiting until one of them becomes "ready", i.e. data is available for.

The const struct timespec *timeout, tv, in your case specifies the timeout period, or how log he select waits for data until it returns ( this behaves like a sleep ). if the timeout is 0, the select return immediately, if it's NULL it can block indefinitely.

You don't show in your code how you initialized tv, but I'm going to guess it's zero, hence the behavior you are seeing.

Try initializing the timeout before you call select and see if that helps:

tv.tv_sec = 1;//or any other value you see fit
tv.tv_usec= 0;
Pandrei
  • 4,843
  • 3
  • 27
  • 44
  • I tried struct timeval tv = {1400, 0}; before the while loop . But it doesnt help me . select is not at all sleeping . I dont know why? – user2248680 May 15 '15 at 06:45
  • how do you know whether it's "sleeping" or not? maybe we should have started with this question... – Pandrei May 15 '15 at 10:16