LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-24-2006, 11:49 PM   #1
zhubaining
LQ Newbie
 
Registered: Aug 2005
Posts: 12

Rep: Reputation: 0
HELP: alarm() didn't work!


I am writing a simple testing program these days, which has the following structure:
================================================
main() {
...
// create a working thread
iRet = pthread_create(&thread_id, NULL, &ThreadFunc, pti);
...
signal(SIGALRM, sig_alrm);
alarm(1);

while(1) {
nanosleep(&tm, NULL); // sleep for 1 millisecond.
}
}

//signal handler for SIGALRM
static void sig_alrm(int signo) {
signal(SIGALRM, sig_alrm);
//now only print the current time
// other codes that do real work are commented
alarm(1);
}

// working thread
void* ThreadFunc (void* arg) {
while(1) {
socket();
bind();
connect();
send();
recv(); // *****
close();// close the socket
}
}
================================================
The problem is that the function sig_alrm() is not called in each second as expected. The time between two consecutive calls to sig_alrm() varies, always several seconds. And I find that sig_alrm() is always invoked when recv() is being called. I am wondering whether calling of recv() within a dedicated thread affects alarm(). Someone here told me that since recv() is a time-consuming function compared to other functions in the thread, as a result, it seems that sig_alrm() is always called when recv() is being called.
Does anyone here know what's wrong with my poor program?

What I want to do is creating some threads to do sth while doing a statistical work periodically(precisely) in the main thread. If alarm() is really not safe for doing this, does anyone have a better solution?

Say thanks in advance.

Last edited by zhubaining; 10-26-2006 at 08:38 AM.
 
Old 10-26-2006, 03:42 AM   #2
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
There is a programming section that's probably better suited for your question.
Report your post (click the report button) and ask a moderator to move it.
 
Old 10-26-2006, 04:03 AM   #3
zhubaining
LQ Newbie
 
Registered: Aug 2005
Posts: 12

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Wim Sturkenboom
There is a programming section that's probably better suited for your question.
Report your post (click the report button) and ask a moderator to move it.
Thanks for telling me this. In fact, I have seen the "Programming" section before, however, it is under the sub forum "Non-*NIX Forums" whose name may suggest that it is a place for problems regarding other platforms, such as Windows. I failed to find any other more suitable places, so I posted it here.
Now I am embarrassed by having not found the "Report" button that you mentioned . As a result, I still don't know how to move my post to the right place. I am new here. Would you please help more?

Last edited by zhubaining; 10-26-2006 at 04:07 AM.
 
Old 10-26-2006, 08:32 AM   #4
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
The report button is at the bottom of each post. So there are currently three (and after I've posted this, there are four).
 
Old 10-27-2006, 05:02 AM   #5
zhubaining
LQ Newbie
 
Registered: Aug 2005
Posts: 12

Original Poster
Rep: Reputation: 0
I still cannot find the button you mentioned. :-(

However, I have manually "moved"(modified and pasted) it to "Forums > Non-*NIX Forums > Programming". And after that, I got a reply quickly. It indicates that post must be placed in right place in order to get answers quickly.

Thanks, anyway.
 
Old 10-28-2006, 08:05 PM   #6
zhubaining
LQ Newbie
 
Registered: Aug 2005
Posts: 12

Original Poster
Rep: Reputation: 0
Oh, my god! This time when I view this post, the "Report" buttons finally show up...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how should i make work power-on by alarm? grybautojas Linux - Hardware 1 11-07-2005 02:35 PM
alarm Tarsis Linux - Newbie 1 08-10-2005 06:55 AM
alarm() call socket9001 Programming 3 04-26-2004 03:03 PM
hp-alarm-mgr tommytomato Linux - General 4 11-27-2003 07:27 PM
I need some kind of alarm carrja99 Linux - Software 3 02-16-2003 01:05 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration