Sponsored Content
Full Discussion: Infinite thread
Top Forums Programming Infinite thread Post 302482805 by Loic Domaigne on Wednesday 22nd of December 2010 02:52:58 PM
Old 12-22-2010
Good evening,

Quote:
Originally Posted by satish@123
I created a thread which pings a machine for every 15 seconds. I made my thread function in infinite loop. Main process also in infinite loop and will run for years. I wonder the thread will continue as long as main process continuous or will thread terminates at some point? Is there any life period for the thread created, any performance degrade because of infinite thread ?
Theoretically, there is no any life time period imposed on a created thread. Practically, the performance may degrade over the time or your process terminates because of programming mistakes, like:
  1. your thread has a memory leak, a consume more and more heap or stack. This will cause performance degradation over the time (e.g. because swap is then used), or even termination (e.g. on Linux, the infamous oom killer kills your process).
  2. your program has some bugs that triggers e.g a SIGSEGV,
  3. your program receives an extern signal that it does not catch and whose default action is to terminate the process.
  4. ...
But if your program is rock solid, then it will go on and on. Until the hardware where it runs breaks (and like death, you know this will happen at some point Smilie )

Quote:
Originally Posted by Praveen_218
1) You haven't handled any signal/interrupts; so don't expect it to run for years. In other words, its a no robust code. Hence, the moment main() is gone, your thread would go.
This is slightly incorrect. A uncaught signal won't necessarily cause main() to go away. What happens actually, is that the signal is delivered to a particular thread in the process. If the signal is not blocked for this thread, then the signal handler installed shall run in the context of the thread that "received" the signal. If no handler is installed, then the default action shall take place. This default action may cause the process to terminate.

Quote:
Originally Posted by Praveen_218
2) main() is in the busy wait; most of the OS kernel scheduler would detect this easily and (untill explictly elevated in terms of priority) would run off with a low priority in scheduling
--happens in Linux (trick to overcome in busy wait is to put a few miliseconds of sleep --but can't generalize this for other OS).
Instead why don't you just blocked wait in pthread_join() and do that function call in another seperate pthread?
How can you say that without knowing what getrequest()/processrequest() is doing? getrequest() may for instance involve some blocking syscall...

Quote:
Originally Posted by satish@123
In main I can create a new thread for every 15 secs to ping the machine and detach the thread. Instead of creating for every 15 seconds , I had it in one thread.
This is IMHO better to have one thread that runs for ever, as shown in your snippet.

HTH, Loïc
This User Gave Thanks to Loic Domaigne For This Post:
 

5 More Discussions You Might Find Interesting

1. Programming

How to cancel a thread safely from the initial thread?

how about asynchronous canceling? or with signal? if with signal whether it effects the process? my english so badly :( :( (1 Reply)
Discussion started by: alan.zhao
1 Replies

2. Programming

high priority thread contains an infinite loop

Hi, Assume there are 3 threads N1, N2, N3. in N1 { .... while(1) { } } when the thread N1 got the time slice, it started executing the infinite loop.. Note:there is no condition inside the while(1) to end the infinite loop I heard that some RTOS will solve this problem... (0 Replies)
Discussion started by: rvan
0 Replies

3. Shell Programming and Scripting

Infinite while loop

what is the difference between while:,while true and while false? (6 Replies)
Discussion started by: proactiveaditya
6 Replies

4. Homework & Coursework Questions

Help with infinite loop problem

1. The problem statement, all variables and given/known data: My problem is an infinite loop when i press any other key other then Y or y in the while loop. what i want it to do is return to the normal script outside of it if pressing N or n or keep asking the same question if its any other... (4 Replies)
Discussion started by: Ren_kun
4 Replies

5. Shell Programming and Scripting

How to stop infinite loop

Im unable to stop the below infinite loop (bash script). Can someone tell me why this isnt responding to signals eg: ctrl+c (SIGINT) or ctrl+z c=0 test_loop() { c=$(($c+1)) echo "count value is : $c " sleep 1 test_loop } Im using: SunOS 5.10 PS: If run this as... (13 Replies)
Discussion started by: Arun_Linux
13 Replies
All times are GMT -4. The time now is 04:02 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy