PDA

View Full Version : delivering signals



maverick84
05-21-2008, 07:24 AM
Hi,
Does anyone know how to prevent from delivering signal to process even after that signal was unblocked?

Elkvis
05-21-2008, 11:08 AM
you could catch it and do nothing with it



signal(SIGINT, my_sigint_handler);
...
void my_sigint_handler(int sig)
{
/* just return. do nothing here */
return; /* not really necessary, but just for clarity */
}

brewbuck
05-21-2008, 01:04 PM
Hi,
Does anyone know how to prevent from delivering signal to process even after that signal was unblocked?

The way you prevent delivery of signals is by blocking them. If you're unwilling to do that, you can't stop the signal.

maverick84
05-26-2008, 04:04 AM
Thanks for your reply,

of course, I can block the signal, but this signal can't be blocked all the time. After definited time it should be unblocked.

brewbuck
05-26-2008, 11:49 AM
Thanks for your reply,

of course, I can block the signal, but this signal can't be blocked all the time. After definited time it should be unblocked.

Your requirements are not clear enough to suggest any specific solution.

If you want to check pending signals every X seconds, for instance, then you can set up an alarm to go off after X seconds, then in the signal handler for this alarm, unblock the signals of interest. If signals are pending, they will immediately be delivered. Once they are delivered, you can block them again and set up a new timer.

But without knowing more about your specific requirements that's as far as I can go.

maverick84
05-27-2008, 12:47 AM
I have to stop application by sending signal to it. During application is stopped user shouldn't be able to sent any other signals to that application (other signals must be blocked). User can resume program by sending another signal to it. When program is running all signals must be unblocked. Signals which was sent when application was stopped can't be delivered when application is running

Elkvis
05-27-2008, 07:17 AM
I have to stop application by sending signal to it. During application is stopped user shouldn't be able to sent any other signals to that application (other signals must be blocked). User can resume program by sending another signal to it. When program is running all signals must be unblocked. Signals which was sent when application was stopped can't be delivered when application is running

It sounds to me like you're talking about inter-process communication (IPC), rather than signal handling. look into pipes and message queues, as those mechanisms would be better suited for the application you're describing.

maverick84
05-27-2008, 07:28 AM
No, in this task I have to use signals rather than IPC...

brewbuck
05-27-2008, 11:58 AM
I have to stop application by sending signal to it. During application is stopped user shouldn't be able to sent any other signals to that application (other signals must be blocked). User can resume program by sending another signal to it. When program is running all signals must be unblocked. Signals which was sent when application was stopped can't be delivered when application is running

This sounds like when the app is in the "stopped" state, it's not really stopped, just doing nothing and ignoring all signals. If the application is truly stopped, there is no way to cause the signals which are delivered to it to be ignored -- they are simply deferred until the application starts again.

So when you enter your stop state, you should:

1. Unblock all signals and set their handlers to the ignore handler (SIG_IGN)
2. Enter an infinite loop calling pause()

Then, when the application is "unstopped:"

1. Break the infinite loop
2. Restore all signal handlers and masks to their original state

You can't make a signal just "vanish." It is either blocked (and will be delivered once unblocked), or ignored.

Elkvis
05-27-2008, 02:00 PM
No, in this task I have to use signals rather than IPC...

is there a specific reason why IPC is not an viable option? is this a school assignment of some sort where you are supposed to be learning about unix signals? I would think that IPC would be ideal for your application, as it allows programs to communicate with, or "signal" one another.

maverick84
06-06-2008, 02:29 AM
OK this problem is solved, but i have another one. How can I clear stdin buffer?
I know that i can use function like this:

void dump_line( FILE * fp )
{
int ch;
while( (ch = fgetc(fp)) != EOF && ch != '\n' )
/* null body */;
}

but it works in case when in buffer is only one line. How to clean in when it's more than one line?