Board index » Visual Studio » select function call

select function call

Visual Studio365
Hi,

I have some problem with the select function call. I am trying to execute

the select loop for a time out period of 10 msec and it always returns after

11 milli sec. If I execute in a loop for 30 times more than 20 times it

returns after 11 milli sec and less than 10 times it returns after 10 mill

secs. I took an average for 1000 times and it came out to be 10.886... What

should I do to make the select funtion return after exactly 10milli sec. Here

is the code.



timeBeginPeriod(1);

//timeEndPeriod(1);



fd = socket(AF_INET,SOCK_DGRAM,0);



long int diff =0, before = 0, after = 0;

timeval t;

int result=-1;

int test = 1;



long duration = 0x0;

timeb timeStart, timeEnd;

float average = 0;



//while (1)

for (int i=0; i<loops; i++)

{

diff = 0;

fds = 0;

result=-1;

t.tv_sec = 0;



t.tv_usec = interval;

//t.tv_nsec = interval;



FD_ZERO(&excp);

FD_SET(fd,&excp);

fds++;

ftime(&timeStart);

before = timeGetTime();

result = select(fds,0x0,0x0,&excp,&t);

//Sleep(10);

result=0;

after = timeGetTime();

diff = after - before;

ftime(&timeEnd);

average += diff;

average /= 2;

if ( result == -1 )

cout << WSAGetLastError() << endl;

else if (result == 0 && flag)

{

printf("ftime Timeout occured : %d\n", timeEnd.time - timeStart.time);

printf("ftime Timeout occured : %d\n", timeEnd.millitm -

timeStart.millitm);

printf("MultiMedia Timeout occured : %d\n", diff);

}

}

printf("average MultiMedia Timeout occured : %f\n", average);


-
 

Re:select function call

Windows is not a real time system. You will never get "exactly 10 ms". It

will always be "more or less close to 10ms".



--

www.firestreamer.com">www.firestreamer.com - NTBACKUP to DVD and DV





"Sarbeswar" <Sarbeswar@discussions.microsoft.com>wrote in message

Quote
Hi,

I have some problem with the select function call. I am trying to execute

the select loop for a time out period of 10 msec and it always returns

after

11 milli sec. If I execute in a loop for 30 times more than 20 times it

returns after 11 milli sec and less than 10 times it returns after 10 mill

secs. I took an average for 1000 times and it came out to be 10.886...

What

should I do to make the select funtion return after exactly 10milli sec.

Here

is the code.



timeBeginPeriod(1);

//timeEndPeriod(1);



fd = socket(AF_INET,SOCK_DGRAM,0);



long int diff =0, before = 0, after = 0;

timeval t;

int result=-1;

int test = 1;



long duration = 0x0;

timeb timeStart, timeEnd;

float average = 0;



//while (1)

for (int i=0; i<loops; i++)

{

diff = 0;

fds = 0;

result=-1;

t.tv_sec = 0;



t.tv_usec = interval;

//t.tv_nsec = interval;



FD_ZERO(&excp);

FD_SET(fd,&excp);

fds++;

ftime(&timeStart);

before = timeGetTime();

result = select(fds,0x0,0x0,&excp,&t);

//Sleep(10);

result=0;

after = timeGetTime();

diff = after - before;

ftime(&timeEnd);

average += diff;

average /= 2;

if ( result == -1 )

cout << WSAGetLastError() << endl;

else if (result == 0 && flag)

{

printf("ftime Timeout occured : %d\n", timeEnd.time - timeStart.time);

printf("ftime Timeout occured : %d\n", timeEnd.millitm -

timeStart.millitm);

printf("MultiMedia Timeout occured : %d\n", diff);

}

}

printf("average MultiMedia Timeout occured : %f\n", average);





-

Re:select function call

Well, the thread is suspended at least 10 ms,not exactly 10ms.

The select function may work fine on a Real Time Operation System, such as

vxWorks,QNX,

RT-LINUX,RT-NT¡£





Quote
Hi,

I have some problem with the select function call. I am trying to execute

the select loop for a time out period of 10 msec and it always returns

after

11 milli sec. If I execute in a loop for 30 times more than 20 times it

returns after 11 milli sec and less than 10 times it returns after 10 mill

secs. I took an average for 1000 times and it came out to be 10.886...

What

should I do to make the select funtion return after exactly 10milli sec.

Here

is the code.



timeBeginPeriod(1);

//timeEndPeriod(1);



fd = socket(AF_INET,SOCK_DGRAM,0);



long int diff =0, before = 0, after = 0;

timeval t;

int result=-1;

int test = 1;



long duration = 0x0;

timeb timeStart, timeEnd;

float average = 0;



//while (1)

for (int i=0; i<loops; i++)

{

diff = 0;

fds = 0;

result=-1;

t.tv_sec = 0;



t.tv_usec = interval;

//t.tv_nsec = interval;



FD_ZERO(&excp);

FD_SET(fd,&excp);

fds++;

ftime(&timeStart);

before = timeGetTime();

result = select(fds,0x0,0x0,&excp,&t);

//Sleep(10);

result=0;

after = timeGetTime();

diff = after - before;

ftime(&timeEnd);

average += diff;

average /= 2;

if ( result == -1 )

cout << WSAGetLastError() << endl;

else if (result == 0 && flag)

{

printf("ftime Timeout occured : %d\n", timeEnd.time - timeStart.time);

printf("ftime Timeout occured : %d\n", timeEnd.millitm -

timeStart.millitm);

printf("MultiMedia Timeout occured : %d\n", diff);

}

}

printf("average MultiMedia Timeout occured : %f\n", average);





-

Re:select function call

On Thu, 18 Nov 2004 18:25:02 -0800, Sarbeswar

<Sarbeswar@discussions.microsoft.com>wrote:



Quote
Hi,

I have some problem with the select function call. I am trying to execute

the select loop for a time out period of 10 msec and it always returns after

11 milli sec. If I execute in a loop for 30 times more than 20 times it

returns after 11 milli sec and less than 10 times it returns after 10 mill

secs. I took an average for 1000 times and it came out to be 10.886... What

should I do to make the select funtion return after exactly 10milli sec. Here

is the code.



If you need exact timings, you need a realtime OS. However, even then

you won't get exactly 10ms, since the time will depend on the

underlying kernel "tick" period, which may be as long as 1ms or more

(giving timeouts between 10 and 11ms for your select).



You could try some realtime Windows extensions, such as

www.vci.com/products/windows_embedded/rtx.asp,">www.vci.com/products/windows_embedded/rtx.asp, or you could try

WinCE, which is more "realtime" than ordinary Windows.



Under Windows you can generally improve timings by boosting your

process and thread to the highest priority, but even then the timings

won't be guaranteed, and are at the mercy of many kernel based drivers

and their ISRs. Under desktop versions of Windows, all wakeup time

errors are essentially unbounded, so as well as your 11ms times, you

might validly see 50ms or even 1s times!



Tom

-

Re:select function call

Thanks to all of them who has spent time and expressed their views. I can

understand in this case I could use the real time os. But my concern is, the

sample program works perfect in some machines with windows2000 and windows

xp, but in some machines it does not work, only differences I found was, the

chipset and ip controller. Do you think this muight cause the problem.



Thanks again for your help.



"Sarbeswar" wrote:



Quote
Hi,

I have some problem with the select function call. I am trying to execute

the select loop for a time out period of 10 msec and it always returns after

11 milli sec. If I execute in a loop for 30 times more than 20 times it

returns after 11 milli sec and less than 10 times it returns after 10 mill

secs. I took an average for 1000 times and it came out to be 10.886... What

should I do to make the select funtion return after exactly 10milli sec. Here

is the code.



timeBeginPeriod(1);

//timeEndPeriod(1);



fd = socket(AF_INET,SOCK_DGRAM,0);



long int diff =0, before = 0, after = 0;

timeval t;

int result=-1;

int test = 1;



long duration = 0x0;

timeb timeStart, timeEnd;

float average = 0;



//while (1)

for (int i=0; i<loops; i++)

{

diff = 0;

fds = 0;

result=-1;

t.tv_sec = 0;



t.tv_usec = interval;

//t.tv_nsec = interval;



FD_ZERO(&excp);

FD_SET(fd,&excp);

fds++;

ftime(&timeStart);

before = timeGetTime();

result = select(fds,0x0,0x0,&excp,&t);

//Sleep(10);

result=0;

after = timeGetTime();

diff = after - before;

ftime(&timeEnd);

average += diff;

average /= 2;

if ( result == -1 )

cout << WSAGetLastError() << endl;

else if (result == 0 && flag)

{

printf("ftime Timeout occured : %d\n", timeEnd.time - timeStart.time);

printf("ftime Timeout occured : %d\n", timeEnd.millitm -

timeStart.millitm);

printf("MultiMedia Timeout occured : %d\n", diff);

}

}

printf("average MultiMedia Timeout occured : %f\n", average);

-

Re:select function call

Sarbeswar wrote:

Quote
Thanks to all of them who has spent time and expressed their views. I

can understand in this case I could use the real time os. But my

concern is, the sample program works perfect in some machines with

windows2000 and windows xp, but in some machines it does not work,

only differences I found was, the chipset and ip controller. Do you

think this muight cause the problem.



Every case you describe is "working perfectly" according to the guarantees

that Windows makes. Anything at all could account for minor differences in

timing. If you run your experiment again on the machines where you observed

what you wanted to observe, you may now observe something different.



Bottom line - you're simply expecting something that you can't reasonably

expect.



-cd





-

Re:select function call

I have found that the multimedia timer works great with only 100 usec of

jitter (worst case). Since it uses a callback the code needs to be

refactored to use it.

www.microsoft.com/whdc/system/CEC/mm-timer.mspx">www.microsoft.com/whdc/system/CEC/mm-timer.mspx



Also the high resolution timers are good but can take a lot of CPU to a 10

msec delay.

www.sysinternals.com/ntw2k/info/timer.shtml">www.sysinternals.com/ntw2k/info/timer.shtml



"Sarbeswar" <Sarbeswar@discussions.microsoft.com>wrote in message

Quote
Hi,

I have some problem with the select function call. I am trying to execute

the select loop for a time out period of 10 msec and it always returns

after

11 milli sec. If I execute in a loop for 30 times more than 20 times it

returns after 11 milli sec and less than 10 times it returns after 10 mill

secs. I took an average for 1000 times and it came out to be 10.886...

What

should I do to make the select funtion return after exactly 10milli sec.

Here

is the code.



timeBeginPeriod(1);

//timeEndPeriod(1);



fd = socket(AF_INET,SOCK_DGRAM,0);



long int diff =0, before = 0, after = 0;

timeval t;

int result=-1;

int test = 1;



long duration = 0x0;

timeb timeStart, timeEnd;

float average = 0;



//while (1)

for (int i=0; i<loops; i++)

{

diff = 0;

fds = 0;

result=-1;

t.tv_sec = 0;



t.tv_usec = interval;

//t.tv_nsec = interval;



FD_ZERO(&excp);

FD_SET(fd,&excp);

fds++;

ftime(&timeStart);

before = timeGetTime();

result = select(fds,0x0,0x0,&excp,&t);

//Sleep(10);

result=0;

after = timeGetTime();

diff = after - before;

ftime(&timeEnd);

average += diff;

average /= 2;

if ( result == -1 )

cout << WSAGetLastError() << endl;

else if (result == 0 && flag)

{

printf("ftime Timeout occured : %d\n", timeEnd.time - timeStart.time);

printf("ftime Timeout occured : %d\n", timeEnd.millitm -

timeStart.millitm);

printf("MultiMedia Timeout occured : %d\n", diff);

}

}

printf("average MultiMedia Timeout occured : %f\n", average);





-

Re:select function call

mef526 wrote:

Quote
I have found that the multimedia timer works great with only 100 usec

of jitter (worst case).



Worst case that you observed, you mean. The worst case jitter is still

infinity with multimedia timers, just as it is with normal sleeps (or

select, as the OPs case was).



-cd





-

Re:select function call

That is indeed true, and the work I did was about 8 years ago. I can't say

that things aren't different post WinNt, so if you use these timer you

should try them out to see if they are OK for your use.



The work I did was for HP to create a frequency generator as part of an

lightwave polarization meter, their first PC based instrument (developed in

1993 on Win 3.1).



As part of the project I used several WinNt 3.51 P1 233MHz computers to port

the code and the 100 usec jitter was pretty typical.



"Carl Daniel [VC++ MVP]" <cpdaniel_remove_this_and_nospam@mvps.org.nospam>

wrote in message

Quote
mef526 wrote:

>I have found that the multimedia timer works great with only 100 usec

>of jitter (worst case).



Worst case that you observed, you mean. The worst case jitter is still

infinity with multimedia timers, just as it is with normal sleeps (or

select, as the OPs case was).



-cd









-