For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > December 2004 > select behavior









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author select behavior
Kanti

2004-12-16, 3:57 am

>If you're not specifying any file descriptors, shouldn't the first
>argument be 0?



Hi,

I have tried giving 0 also in select but it is showing same behavior.
I don't have any problem to use sleep/usleep function but I want to
replace it with select function and get the same behavior.

struct timeval tm1;
tm1.tv_sec = 0;
tm1.tv_usec = 100;

select(0,NULL,NULL,NULL,&tm1);

> How do you know that select() isn't sleeping for the specified time?
> You've told it to sleep for 100 microseconds, or 0.0001 seconds,
> which isn't very long. Is your clock even precise enough to sleep
> for that short a time?


I have written a sample code in which two threads are running.one is
main thread and this thread creates one more thread. after creating
new thread main thread procees with printing on the screen. the new
thread created also does the same work of printing somthing one the
screen, and both main thread and sub thread sleeps (have used select
here) for some microseconds after printing one character on the
screen. Now I have specified a value of 10 micro seconds in main
thread (to sleep) and given a value of more than a millisecond in sub
thread (to sleep). but when I use select function sub thread is not
sleeping for specified time after printing one character. If it sleeps
for that period surely the main thread will get chance to get executed
and print values.
but result it is showing is one the control comes to the sub thread it
is not allowing the main thread (means its not sleeping after
printing) to run till it exits. I am pasting the sample code below.

#include <stdio.h>
#include <pthread.h>
#include <signal.h>
#include <unistd.h>
#include <sys/select.h>

struct p_thread_test
{
pthread_t tid;
int flag;
};

struct timeval delay;
void *thrd_func(void *arg);

int main()
{
int retval,i;
struct p_thread_test test_obj;
test_obj.flag = 0;

delay.tv_sec = 0;
delay.tv_usec = 10;

retval = pthread_create(&test_obj.tid, NULL, thrd_func, (void
*)&test_obj);
if (retval == 0) {
i = 0;
while(!test_obj.flag) {
printf("i = %d\n",i);
select(0,NULL,NULL,NULL,&delay);
//usleep(10);
i++;
}
printf("Flag set\n");
}
printf("test_obj.flag:%d Exiting from main\n",test_obj.flag);
return 0;
}



void *thrd_func(void *arg)
{
int count = 0;
p_thread_test *arg1;

arg1 = (p_thread_test *)arg;
delay.tv_sec = 0;
delay.tv_usec = 100000;

for (count = 0;count < 1000;count++) {


printf("Count:%d\n",count);
select(0,NULL,NULL,NULL,&delay);
//usleep(20000);

if(count == 950){
arg1->flag = 1;
pthread_exit(NULL);
}
}
}


Kanti
Michael Fuhr

2004-12-16, 3:57 am

kanti_14@rediffmail.com (Kanti) writes:

> when I use select function sub thread is not sleeping for specified
> time after printing one character.

[snip]
> delay.tv_sec = 0;
> delay.tv_usec = 100000;
>
> for (count = 0;count < 1000;count++) {
> printf("Count:%d\n",count);
> select(0,NULL,NULL,NULL,&delay);


select() is allowed to modify the timeout value, so if you don't
set it for each iteration then you might be sleeping for no time
at all. Move the code that sets the timeout to inside the loop,
here and in main().

You're also using a variable set in one thread to notify another
thread, but you're not using mutexes or condition variables to
coordinate access to that variable. That might work, but if it
does it's by luck and not by design. If threads need to communicate
with each other then it would be better to use the proper mechanisms
for doing so.

> If it sleeps for that period surely the main thread will get chance
> to get executed and print values.


Maybe, but assuming so is a mistake. If you need to coordinate
threads' actions then use the mechanisms designed for that purpose
instead of relying on luck.

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/
Andrei Voropaev

2004-12-16, 8:58 am

On 2004-12-16, Kanti <kanti_14@rediffmail.com> wrote:
[...]
>
> I have written a sample code in which two threads are running.one is
> main thread and this thread creates one more thread. after creating
> new thread main thread procees with printing on the screen. the new
> thread created also does the same work of printing somthing one the
> screen, and both main thread and sub thread sleeps (have used select
> here) for some microseconds after printing one character on the
> screen. Now I have specified a value of 10 micro seconds in main
> thread (to sleep) and given a value of more than a millisecond in sub
> thread (to sleep). but when I use select function sub thread is not
> sleeping for specified time after printing one character. If it sleeps
> for that period surely the main thread will get chance to get executed
> and print values.
> but result it is showing is one the control comes to the sub thread it
> is not allowing the main thread (means its not sleeping after
> printing) to run till it exits. I am pasting the sample code below.

[...]
>
> void *thrd_func(void *arg)
> {
> int count = 0;
> p_thread_test *arg1;
>
> arg1 = (p_thread_test *)arg;
> delay.tv_sec = 0;
> delay.tv_usec = 100000;
>
> for (count = 0;count < 1000;count++) {
>
>
> printf("Count:%d\n",count);
> select(0,NULL,NULL,NULL,&delay);
> //usleep(20000);
>
> if(count == 950){
> arg1->flag = 1;
> pthread_exit(NULL);
> }
> }
> }


If you are testing this on Linux, then you have to set 'delay' each time
before the select call. Probably on other systems this is also true.

One more thing to consider. Usually there're limits on how little a
process can sleep. For example linux before 2.6 on i386 in normal
configuration can't sleep less than 10ms.

Anyway, the above program with the fix I've mentioned runs as expected
on the linux 2.6

Andrei
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com