select() syscall

select() syscall

Post by Peteris Krumin » Mon, 16 Jun 2003 09:18:53



I am confused with the following:

 I have 2 programs and two named pipes, let's name the pipes 'read' and
'write'.
 Program 1 opens 'read' for reading and 'write' for writing, the second
opens 'read' for writing and 'write' for reading.
 Program 1 writes to it's write, meanwhile program 2 has issued select()
on it's read (program1's write). Program 2 notices there is something to
read and writes to it's write (prorgam1's read) that it has rcved smth,
meanwhile program 1 has issued select on it's read.
 At this position select() never returns, tho i have written to
descriptor select() is waiting for.

 why? can i have two pipes to communicate between two programs?

P.Krumins
--

simple experiment sources:
first i run program1, which blocks until i run program2, which opens
named pipes vice-versa and issues select. then program1 writes, but
program2's select() never returns.
Tho if i remove select from program1, it works.

program 1:
--
    int ret;
    FILE *R,*W;
    fd_set r;

printf("opening..\n");
        R=fopen("/home/pkrumins/tests/fifos/read","r");
           /* no error checking to reduce lines of code in experiment */
        W=fopen("/home/pkrumins/tests/fifos/write","w");
printf("opened for reading and writing, writing\n");

    fwrite("test!",1,5,W);

    FD_ZERO(&r);
    FD_SET(fileno(R),&r);
    ret = select(fileno(R)+1,&r,NULL,NULL,NULL);

    printf("YEEEE\n");

--
program2:
--
    int ret;
    FILE *R,*W;
    fd_set r;

    W=fopen("/home/pkrumins/tests/fifos/read","w");
    R=fopen("/root/pkrumins/tests/fifos/write","r");
    FD_ZERO(&r);
    FD_SET(fileno(R),&r);

printf("waiting read\n");
    ret = select(fileno(R)+1,&r,NULL,NULL,NULL);

    if (ret)
        printf("YEEEE2\n");
    else
        printf("FAILED\n");

    /* now write to satisfy program 1 */
    fwrite("ace",1,3,write);

 
 
 

select() syscall

Post by David Schwart » Mon, 16 Jun 2003 10:16:47



Quote:>     fwrite("ace",1,3,write);

    That 'write' should be a 'W'.

    DS

 
 
 

select() syscall

Post by Peteris Krumin » Mon, 16 Jun 2003 19:05:24





>>     fwrite("ace",1,3,write);

>     That 'write' should be a 'W'.

It is W, i wrote that by hand.

P.Krumins

 
 
 

select() syscall

Post by Peteris Krumin » Mon, 16 Jun 2003 22:26:01




Quote:> I am confused with the following:

[...]

after a few hours of experimenting everything is now clear to me.

P.Krumins

 
 
 

1. Problem with the select syscall

Hello.

I have a problem to set up a basic client/server interface.
I hope that someone will help me with this because I cant figure out
what the problem is, though I have read the man hundreds of times.

Here's my problem :

In the server, to accept connections from the clients, I do a call
to the select system function.
When I launch the server, the select blocks until a have a connection,
alright.
Let's say I set the timeout length to 5 seconds. Well, if I launch my client
before the end of these 5 seconds,while select is blocking for the first
time,
then everything works fine and my client can send data to the server which
receive them.

The problem is that when I launch the client after the 5 seconds (when the
server has been launched for more than 5 seconds) then select function
always returns 0 and seems not to detect that a connection occured (though
the
client says that the connection is ok). From this, select always returns 0.

And, as I have to use a non-blocking select (timeout = 0) because of my
program's needs, select always returns 0.

Here's the source code that I use to set up the server. Please tell me where
my error is, and what I have misunderstood.

Thank you.

-------------------------------------------------------------------------

fd_set g_fdset; // the bits field
int g_fd_max; // the maximal value of file desciptor
int g_fd_server; // fd of the server socket (to accept new connections)

int  select_sockets_ready_to_read(void)
{
  struct timeval timeout;
  int   i;

  timeout.tv_sec = 0;
  timeout.tv_usec = 0;
  if ((i = select(g_fd_max + 1,
    &g_fdset, NULL, NULL, 0)) < 0)
    perror("Select ");
  return i;

void  accept_one_connection(void)
{
  struct sockaddr_in addr;
  int   addrlen;
  int   new_fd;

  addrlen = sizeof(addr);
  if (FD_ISSET(g_fd_server, &g_fdset))
    {
      new_fd = accept(g_fd_serveur, (struct sockaddr*) &addr, &addrlen);
      if (new_fd < 0)
 perror("Accept ");
      else

   if (g_fd_max < new_fd)
     g_fd_max = new_fd;
   FD_SET(new_fd, &g_fdset);
   fcntl(new_fd, F_SETFL, O_NONBLOCK | fcntl(new_fd, F_GETFL, 0));
 }
    }

void  main_loop(void)
{
  int  i;

  FD_ZERO(&g_fdset);
  FD_SET(g_fd_server, &g_fdset);
  while(1)
    {
      i = select_sockets_ready_to_read();
      if (i != 0)
 accept_one_connection();
      do_action(); // execute actions when FD_ISSET(x, &g_fdset) is true
    }

---------------
Fabien SPAGNOLO
Etudiant a l'EPITA/EPITECH
Promotion 2002

2. POST codes

3. How to use select syscall with curses?

4. bug report: loopback device

5. syscalls-by-name (aka dynamic syscalls / vsyscalls)

6. NIS Configuration

7. AIX 3.2 equivalent of syscall(2) and <syscall.h>?

8. RedHat 4.0 Users-Guide in A4 ?

9. Syscalls on behalf of another process (was Syscalls in kernel space)

10. Remote execution of syscalls (was Re: Syscall from Kernel Space)

11. if change date during select ( ), select is hang

12. How does select() function "select"?

13. select field n to last? select last field?