Unix Programming - Question about conflicts while using accept() and timer

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > January 2004 > Question about conflicts while using accept() and timer





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 Question about conflicts while using accept() and timer
Wang Tian News

2004-01-23, 5:01 pm

Hi,

I am writing a server program, I have to use accept() to get information from client, at the same time i set a timer (using setitimer()) to do some periodical jobs(just some data update), but every time the timer function is invoked, the accept() will return with error. This always happens when the timer is invoked the second time, no matter what interval I set to it. This still happens when I put accept() and timer setup in two threads.

Anyone met this problem before?? Who can help me?
Thanks

Tian

David Schwartz

2004-01-23, 5:01 pm

quote:

> "Wang Tian News" <wangtian@cs.unc.edu> wrote in message news:3fcb7b54
> $1_2@news.unc.edu...


quote:

> I am writing a server program, I have to use accept() to get information


from client,
quote:

> at the same time i set a timer (using setitimer()) to do some periodical


jobs(just some
quote:

> data update), but every time the timer function is invoked, the accept()


will return with
quote:

> error. This always happens when the timer is invoked the second time, no


matter what
quote:

> interval I set to it. This still happens when I put accept() and timer


setup in two threads.

When you use 'setitimer', it's supposed to interrupt a blocking
function. I'm not sure why this is a problem for you. When 'accept' returns
with an error, just do the periodic jobs and call 'accept' again.

If you're going to use threads, don't use 'setitimer' since there's
nothing to interrupt. If you need to delay, just use 'nanosleep', 'usleep',
'select', or whatever makes sense on your platform. Though a bit ugly,
'select(0, NULL, NULL,NULL, &tv);' works on every pthreads platform I know
of.

DS


David Schwartz

2004-01-23, 5:01 pm

quote:

> "Wang Tian News" <wangtian@cs.unc.edu> wrote in message news:3fcb7b54
> $1_2@news.unc.edu...


quote:

> I am writing a server program, I have to use accept() to get information


from client,
quote:

> at the same time i set a timer (using setitimer()) to do some periodical


jobs(just some
quote:

> data update), but every time the timer function is invoked, the accept()


will return with
quote:

> error. This always happens when the timer is invoked the second time, no


matter what
quote:

> interval I set to it. This still happens when I put accept() and timer


setup in two threads.

When you use 'setitimer', it's supposed to interrupt a blocking
function. I'm not sure why this is a problem for you. When 'accept' returns
with an error, just do the periodic jobs and call 'accept' again.

If you're going to use threads, don't use 'setitimer' since there's
nothing to interrupt. If you need to delay, just use 'nanosleep', 'usleep',
'select', or whatever makes sense on your platform. Though a bit ugly,
'select(0, NULL, NULL,NULL, &tv);' works on every pthreads platform I know
of.

DS


Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com