[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Signal handling in unix

Posted on 2000-05-08
7
Medium Priority
?
756 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 800 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

649 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