Re: Switching between foreground and background

Markus Klotzbuecher
2003-09-22T11:55:51Z

On Monday 22 September 2003 12:48 pm, Silambu Chelvan wrote:
> Hi all,
>
> I have written some program with signal handler. Is
> this possible, if
> one of the signals registered with my program is
> raised, my program
> should goto background and should come to foreground
> when some other of the
> registered signal is raised. how to do it?
>
> Any function available to switch a process between
> foreground and
> background at runtime?

Hi

Switching a task between forground and background is nothing else as
suspending and restarting. What i would try to do is, when you get the signal
you want to suspend on, call the pause() function (see pause(2) ). When you
get the signal you want to restart on, just do nothing as this will let the
pause() function return and continue doing what happend before suspend.

Try something like this:

***************************************
#include <signal.h>
#include <stdio.h>

void suspend()
{
printf("Suspending...\n");
pause();
}

void wake()
{
printf("Woken up.\n");
}

int main()
{

signal(SIGUSR1, suspend);
signal(SIGUSR2, wake);

while(1) {
/* do whatever */
printf("doing whatever...\n");
sleep(1);
}
}

Cheers

Markus