LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-14-2009, 10:17 AM   #1
okeyla
Member
 
Registered: Nov 2004
Posts: 47

Rep: Reputation: 15
kill thread which is with block function... (fgets...)


I am writing a video program in C in embedded linux.
My main function creates 2 threads.
All are with while loop.
(1)main loop is in charge with video processing
(2)thread 1 loop is in charge with User Interface
(2)thread 2 loop is in charge with statistic of video

My question is ,
How do i terminate this program by others normally with
thread 1 is with blocking function?( fgets is always waiting for user input )
( Our design is with this program could be terminated by other program. )

PS1. I tried to add a SIGINT callback function in this program.But this thread 1 is still pending there waiting fo user input even if i pass SIGINT to it...

PS2. "kill -9" make my program crash occasionally.
( don't know why...maybe thread 1 do not release
resources normally... )

Any ideas for this?
 
Old 10-14-2009, 07:20 PM   #2
rigor
Member
 
Registered: Sep 2003
Location: 19th moon ................. ................Planet Covid ................Another Galaxy;............. ................Not Yours
Posts: 705

Rep: Reputation: Disabled
When you say "tried to add a SIGINT callback function", do you mean that you added a signal handler, and if so, did you use sigaction?
 
Old 10-14-2009, 07:32 PM   #3
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by okeyla View Post
I am writing a video program in C in embedded linux.
My main function creates 2 threads.
All are with while loop.
(1)main loop is in charge with video processing
(2)thread 1 loop is in charge with User Interface
(2)thread 2 loop is in charge with statistic of video

My question is ,
How do i terminate this program by others normally with
thread 1 is with blocking function?( fgets is always waiting for user input )
( Our design is with this program could be terminated by other program. )

PS1. I tried to add a SIGINT callback function in this program.But this thread 1 is still pending there waiting fo user input even if i pass SIGINT to it...

PS2. "kill -9" make my program crash occasionally.
( don't know why...maybe thread 1 do not release
resources normally... )

Any ideas for this?
The appropriate way to manage a multi-threaded application is to set up a run flag. When it's time for the application to exit, clear the run flag that the independent threads have been coded to read and quit if the flag is clear.

But don't expect to be able to force the threads to quit. Write your thread code so the threads only continue to execute by testing the run flag and continuing on that basis:

Code:
while(ok_to_run) {
   // busy work here
}
When it's time to exit, clear the "ok_to_run" flag, then join the relevant thread(s) so the main application won't exit before the threads do.
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
mysql thread kill jindalarpan Linux - Software 5 09-20-2009 03:26 PM
[C/C++] How to kill a thread in fgets hwoarang Programming 3 09-09-2008 02:59 PM
how to kill a thread in python shanenin Programming 2 12-13-2005 11:13 PM
How to kill a user thread in Linux.... Help me rajsun Programming 4 04-20-2005 04:13 AM
Please move my thread or kill it or something. :-( gplacek Slackware 1 08-27-2004 01:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:36 PM.

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