Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

select behavior
>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

Report this thread to moderator Post Follow-up to this message
Old Post
Kanti
12-16-04 08:57 AM


Re: select behavior
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/

Report this thread to moderator Post Follow-up to this message
Old Post
Michael Fuhr
12-16-04 08:57 AM


Re: select behavior
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

Report this thread to moderator Post Follow-up to this message
Old Post
Andrei Voropaev
12-16-04 01:58 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Unix Programming archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:32 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.