Solved

Signal handling in unix

Posted on 2000-05-08
7
735 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maze travler 6 51
How to convert MFC APP to Win32 APP. 19 68
operator overload: incompatible type and too many params 5 62
Safe conversion? 4 54
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

932 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

12 Experts available now in Live!

Get 1:1 Help Now