Thread: A fifo file and select() problem

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    2

    Exclamation A fifo file and select() problem

    Basically I have a server running an almost infinite, select controlled, mini webserver. I have created a fifo file with mkfifo called "pipe" which is opened in read only mode. My select is suppose to wait for something to happen in that file (which talks to a java GUI using JNI but not important) or if a client connects asking for a website. The GUI has pause, unpause and shutdown options so I figured I would have a variable "paused" in my server which if paused would stop the "FD_SET(s_sock, &fs)" with an if statement. The server will wait perfectly until I send it something along the pipe (the first time) then the select picks it out my "readGUI()" function reads the message and returns; 1, if there was nothing in the pipe or an unpaused call; 0, if there was a pause call; and -1, if there was a shutdown call. My problem is that after it goes back and initializes the select variables again the select automatically calls my readGUI again, says theres nothing in there, returns a 1 and continues this loop indefinetly.

    This would have to be a problem with my select I would assume because it proves my pipe is empty.

    Heres the code:

    Code:
    ...
    shutter_down = 1;
    paused = 1;
    
    while(shutter_down)
    {
         T.tv_sec = 1;
         T.tv_usec = 0;
         FD_ZERO(&fs);
         FD_SET(fd, &fs);
         if (paused)
              FD_SET(s_sock, &fs);
    
         r = select(FD_SETSIZE, &fs, NULL, NULL, &T);
    
         if (r < 0)
              exit (-1)
         else if (r > 0)
         {
              if (FD_ISSET(fd, &fs))
              {
                   paused = readGUI();
                   if (paused == -1)
                        shutter_down = 0;
              }
              else if (FD_ISSET(s_sock, &fs))
              {
                   /*Accept incoming socket and do stuff*/
              }
         }
    }
    
    int readGUI()
    {
         char buf[100];
         int ret_val;
    
         ret_val = read(fd, buf, sizeof(buf));
    
         if (ret_val < 1)
              return 1;  /*Nothings there, big problem*/
    
         buf[ret_val] = '\0';
    
         printf ("buf: %s\n", buf); /*This only gets printed the first time*/
                                                 /*Its how I know theres nothing in the pipe*/
    
         if (!(strcasecmp(buf, "1")))
              return 1;
         if (!(strcasecmp(buf, "0")))
              return 0;
         if (!(strcasecmp(buf, "-1")))
              return -1;
    
         return 1;
    }
    Let me know if you need more code. Only posted the problem.
    Thanks in advance.
    Last edited by itsdafoetime; 03-08-2009 at 02:22 AM.

  2. #2
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    This might be your problem

    Code:
    ...
    shutter_down = 1;
    paused = 1;
    
    while(shutter_down)
    {
         T.tv_sec = 1;
         T.tv_usec = 0;
         FD_ZERO(&fs);
         FD_SET(fd, &fs);
         if (paused)
              FD_SET(s_sock, &fs);
    
         r = select(FD_SETSIZE, &fs, NULL, NULL, &T);
    
         if (r < 0)
              exit (-1)
         else if (r > 0)
         {
              if (FD_ISSET(fd, &fs))
              {
                   paused = readGUI();
                   if (paused == -1)
                        shutter_down = 0;
              }
              else if (FD_ISSET(s_sock, &fs))
              {
                   /*Accept incoming socket and do stuff*/
              }
              else
              {
                  /* Does Nothing */
              }
          } /* you didn't close the "else if (r >0)" so there isn't a closing brace on the while statement*/
    
    int readGUI()
    {
         char buf[100];
         int ret_val;
    
         ret_val = read(fd, buf, sizeof(buf));
    
         if (ret_val < 1)
              return 1;  /*Nothings there, big problem*/
    
         buf[ret_val] = '\0';
    
         printf ("buf: %s\n", buf); /*This only gets printed the first time*/
                                                 /*Its how I know theres nothing in the pipe*/
    
         if (!(strcasecmp(buf, "1")))
              return 1;
         if (!(strcasecmp(buf, "0")))
              return 0;
         if (!(strcasecmp(buf, "-1")))
              return -1;
    
         return 1;
    }
    If you're not going to use the else statement then I don't see any reason in putting it there
    Last edited by ಠ_ಠ; 03-08-2009 at 01:02 AM.
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    2
    I only posted snippets of code by hand cause I was to lazy to copy it over from my laptop.. sorry for the mistake its fixed but didnt have anything to do with the problem.. besides which the compiler would have spewed some stuff at me anyways..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simultaneously waiting for data on FIFO and UDP using select call
    By yogesh3073 in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-05-2007, 09:53 AM
  2. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  3. file select
    By EllieProgrammer in forum C Programming
    Replies: 6
    Last Post: 04-11-2005, 11:00 AM
  4. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

Tags for this Thread