LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 04-07-2010, 10:54 PM   #1
golmschenk
Member
 
Registered: Nov 2009
Posts: 144

Rep: Reputation: 15
C - Why signals? Example? Explanation?


I was wondering if someone could explain well what good signals are for (particularly what they can do other things can't) and give a full, straight forward, example. I don't mind someone just redirecting me somewhere else that they know there's a good explanation or example. Thanks!
 
Old 04-08-2010, 01:13 AM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Signals are a great way for one process to, well, "signal" another process.

Signals are also how you can "notify" a running program that something "interesting" has occurred: you can signal a "Ctl-C event" (SIGINT), a memory access error (SIGSEGV), floating point error, or some other "condition" that occurs "asynchronously" to the program. Some condition where the program might need to take some *immediate* action as a result.

'Hope that helps .. PSM
 
Old 04-08-2010, 07:05 AM   #3
CoderMan
Member
 
Registered: Jan 2009
Location: Gemini Capsule 25164
Distribution: Gentoo
Posts: 375
Blog Entries: 24

Rep: Reputation: 43
Quote:
Originally Posted by golmschenk View Post
I was wondering if someone could explain well what good signals are for (particularly what they can do other things can't) and give a full, straight forward, example. I don't mind someone just redirecting me somewhere else that they know there's a good explanation or example. Thanks!
Well, under Linux you can use the SIGSTOP signal to "pause" a process. That's what happens when you press CTRL-z. Or you can do it manually:

Code:
kill -SIGSTOP <pid>
and then to resume

Code:
kill -SIGCONT <same-pid>
If you're really concerned about your clock cycles, I guess it could be used to put busy processes to bed during the off hours.
 
Old 04-08-2010, 08:08 AM   #4
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Signals are a way for one process to tell another, "Something has happened. Drop what you're doing and take care of it, and then go back to what you were doing." A person writing code in, say, C, will write a signal handling function that causes the program to take care of the situation. The programmer will set up his program to ask the operating system (in your situation, probably Linux) to transfer control to this signal handling function upon receiving a particular kind of signal. After the signal handling function returns control, ordinarily the program will resume where it left off.

In addition to some of the examples mentioned in previous posts in this thread, when you run a text editor in an xterm window and then you resize that window, a signal gets sent to the text editing process which notifies it that the window size has changed. The text editor then finds out what the new window size is and rearranges the text in the window.

One of the examples mentioned above is rarely handled by the program itself: SIGSEGV. This is because SIGSEGV is usually generated by a bug in the program, and if there's a bug in the program, the signal handling function ordinarily should not rely on any data at all being usable. The signal is still valuable, however. You can make use of it not by writing a signal handling function for that signal, but by running your program under a debugger such as gdb. If SIGSEGV happens to your program under that case, gdb will notice it and allow you to see which functions were active at the time your program blew up, and what values were currently stored in variables.

A brief introduction to signals may be found here.

Hope this helps.
 
1 members found this post helpful.
Old 04-08-2010, 08:49 AM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Signals solve the problem of responding to events that happen asynchronously. Instead of periodically testing whether a specific event has occurred (such as data arriving from some source like a serial interface), a program can trap the event, and only respond to it when it actually occurs.
--- rod.
 
Old 04-08-2010, 12:01 PM   #6
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
So golmschenk - question answered?

"Signals", just FYI, are one example of "Interprocess Communications" (IPC). A subject brilliantly discussed in the most excellent Beej's Guide to IPC

http://docs.hfbk.net/beej.us/bgipc/

Enjoy .. PSM
 
Old 04-08-2010, 04:18 PM   #7
golmschenk
Member
 
Registered: Nov 2009
Posts: 144

Original Poster
Rep: Reputation: 15
Yes! Thank you much all!
 
  


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
Log explanation tekmann33 Linux - Newbie 2 07-25-2008 02:26 PM
Need explanation concerning Beagle. Lola Kews SUSE / openSUSE 7 12-12-2007 08:13 AM
Code explanation ... ? dbee Linux - Software 1 12-17-2005 01:06 AM
Further explanation on symlink munichtexan Linux - Newbie 4 12-01-2005 10:52 AM
I Could Use An Explanation winger Linux - General 3 04-13-2002 10:32 PM

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

All times are GMT -5. The time now is 06:48 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