Solved

Signal handling in unix

Posted on 2000-05-08
7
732 Views
Last Modified: 2010-05-18
I have TCPSocket class which offers method connect, close, read
and write (probably no need to explain them :). I have been using roquewave to implement this class.

Now I would like to add a signal handler to my class, which notifies the user when SIGPIPE is received. I have been looking into man pages,
but can't figure out how to do it.

So what I would like is a simple example (maybe a class) which has
a signal handler and some explanation how it works.
0
Comment
Question by:tonitop
7 Comments
 
LVL 5

Expert Comment

by:proskig
Comment Utility
Hi,
It is not hard at all. First of all check man pages for signal. I have them in 3b,3c and 5 sections. You have to have your signal handler, let's say
void sig_handler(int signum);
Then just call
signal(SIGPIPE,sig_handler);
to install your handler.
0
 
LVL 1

Accepted Solution

by:
bcoleman earned 200 total points
Comment Utility
int flag;
struct sigaction act, oact;

void ding()
{
   flag = true;
   return;
}
void main()
{
   act.sa_handler = ding;
   act.flags = 0;
   if ( sigaction(SIGPIPE, &act, &oact) != 0) PANIC; // set new SIGPIPE handler
   flag = FALSE;
   sigaction(SIGPIPE,&oact,NULL); //restore old SIGPIPE handler
}
0
 

Expert Comment

by:cbark
Comment Utility
man sigprocmask (or sigthreadmask if you're using multiple threads) should get you started.

My suggestion however would be to use errno (include errno.h) most system calls set errno when a system call fails.  This will make your TCPSocket class reusable (signal handling is for an entire process, anyone who is already using signal handling won't want to use your object because it will replace their signal handling with yours).  Making a call to strerror with the errno will give you the error text equivalent.

sample code:

  #include <errno.h>

  ...

  if((fd = open("test.txt","r",0)) == -1)
    {
       cout << "error: " << strerror(errno) << endl;
    }
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 2

Expert Comment

by:fremsley
Comment Utility
One thing to keep in mind using proskig's suggestion: if you want to make a class' method to handle the signal it has to be a static one, as signals are global to a process.
0
 

Author Comment

by:tonitop
Comment Utility
Ok, the signal handling part works, but how come I don't get the signal right away? I connect to server and write something. Then I close the server. After that I write something and it goes ok and I receive the signal when I try to write second time. I have been looking into select() function, but can't figure out how I know that the connection has been lost.
0
 

Author Comment

by:tonitop
Comment Utility
So if I wan to handle signals inside my class can it be done?
Isn't static method class method. i.e. they can be used even if no objects have been created.

I would need to somehow set a flag inside my object when it receives signal, but I can not do that with static methods.
0
 

Author Comment

by:tonitop
Comment Utility
Comment accepted as answer
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now