Solved

Signal handling in unix

Posted on 2000-05-08
7
739 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
ID: 2788020
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
ID: 2789096
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
ID: 2789668
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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 2

Expert Comment

by:fremsley
ID: 2791857
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
ID: 2792144
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
ID: 2792519
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
ID: 2843589
Comment accepted as answer
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

803 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