Home > OS >  [for] Linux select block
[for] Linux select block

Time:11-24

As title, I use in the thread FD_SET, program or card in the select, consult everybody a great god! The code is as follows,


#include
#include
#include
#include
#include
#include
#include
#include //mkfifo
#include //mkfifo
#include


# define MAX_BUFFER_SIZE buffer size */1024/*
2/* # define IN_FILES multiplexing input file number */
# define MAX (a, b) ((a & gt; B)? (a), b))

Int the FDS [IN_FILES]={1, 2};
Fd_set an inset.

Void * testfunc (void * arg)
{
int i;
Sleep (1);
For (I=0; I & lt; IN_FILES; I++)
{
FD_SET (FDS, & amp; An inset);
}
}
Int main (void)
{
Char buf [MAX_BUFFER_SIZE];
Int I, res, maxfd;
Pthread_t _mthread;
Struct timeval TV;

FD_ZERO (& amp; An inset);
If (pthread_create (& amp; _mthread, NULL, (void *) testfunc, NULL).=0)
{
Fprintf (stderr, "thread create failed \ n");
return -1;
}

/* remove the two file descriptors supplied */
Maxfd=MAX (FDS [0], FDS [1]).
Res=select (maxfd + 1, & amp; An inset, NULL, NULL, NULL);
For (I=0; I & lt; IN_FILES; I++)
{
If (FD_ISSET (FDS, & amp; An inset))
Printf (" Select ok \ n ");
}
return 0;
}

CodePudding user response:

Block in the select, explain your fd no IO read events,
As for your fd why not IO events, to the client did not send me the data, in a program that is you have a question,
This code from your post, your fd didn't create, if you are using socket, must put the socket created, add into fdset

CodePudding user response:

TV all need not, will wait until the standard input and output data,

CodePudding user response:

The select function and parameter to monitor the file descriptor is wrong, not set timeout,

CodePudding user response:

For (I=0; I & lt; IN_FILES; I++)
{
FD_SET (FDS, & amp; An inset);/// write here has a problem, the first parameter is the fd, is an array, you want to change to FD_SET (FDS [I], & amp; An inset);
}

A again, you listen to the two fd fd=1, 2, which is the standard output (printf, or cout) and the error output (fprintf (stderr.. ) or cerr),
If there is no print, select is not returned

CodePudding user response:

Based on the 4th floor correction
Select the second parameter is a set of listening reading event descriptor the third parameter is listening writing event descriptor set;
You this two descriptors are 1 standard output 2 standard error;
You may write
Res=select (maxfd + 1, NULL, & amp; An inset, NULL, NULL);
Can trigger;
Because I understand the listening writing events is written for kernel buffer can now write notice, should now can write
Read the event is listening said the kernel buffer read data can be read, no data input, should not read,

CodePudding user response:

There is a obvious problem, you create this thread, the thread has not run out, is already a select, pthread_create isn't running thread then go down, more excessive internal thread also sleep for a second, more can't wait to
  • Related