Skip to contentSkip to conversation

Switching between foreground and background

DISCUSSION Silambu Chelvan 8 participants 11 messages Sep 22, 2003 – Sep 24, 2003

lore
Share & tools
lore
#0 lore raw

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?

with regards,
M. SilambuChelvan

__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

#1 lore raw

I think part of the problem here might be conceptual.

If I understand what you want, you'd like, on a signal, for your program to go into the background (still running but no longer blocking the terminal input/output). On another signal you would like the program to come back to the foreground.

If this is the case, the problem you're facing is that the program being in the foreground and the background is actually part of the shell, not a property of the program running. Most Shells provide a way to switch programs from the foreground to the background and back.
BASH for example I could type

$>./myprogram
(cntr + Z) //suspends the current program and prings the prompt back $>bg (runs the program in the background)

The only way that I know of to force a program to run in the background in the code is to make it a daemon which is done by forking twice

int main( ) {

   if(fork()==0)  {
            if( fork() == 0 )  {
                    program code starts here
             }
            exit(0);
    }
    exit(0);
}

----- Original Message -----
From: "Silambu Chelvan" <[email protected]>
To: <[email protected]>
Sent: Monday, September 22, 2003 6:48 AM
Subject: Switching between foreground and background

Quoted 18 lines

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?

with regards,
M. SilambuChelvan

Quoted 9 lines

__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

#2 lore raw

Yes, You have understood what I want. Forcing the program to run in background is not a problem. I need to get the program back in foreground.

Do you have any idea?

with regards,
M. SilambuChelvan

Quoted 76 lines · --- "John T. Williams

I think part of the problem here might be
conceptual.

If I understand what you want, you'd like, on a signal, for your program to
go into the background (still running but no longer blocking the terminal
input/output). On another signal you would like the program to come back to
the foreground.

If this is the case, the problem you're facing is that the program being in
the foreground and the background is actually part of the shell, not a
property of the program running. Most Shells provide a way to switch
programs from the foreground to the background and back.
BASH for example I could type

$>./myprogram
(cntr + Z) //suspends the current program and prings the prompt back
$>bg (runs the program in the background)

The only way that I know of to force a program to
run in the background in
the code is to make it a daemon which is done by
forking twice

int main(  ) {
   if(fork()==0)  {
            if( fork() == 0 )  {
                    program code starts here
             }
            exit(0);
    }
    exit(0);
}

----- Original Message -----
From: "Silambu Chelvan" <[email protected]>
To: <[email protected]>
Sent: Monday, September 22, 2003 6:48 AM
Subject: Switching between foreground and background

Quoted 3 lines

Hi all,

I have written some program with signal handler.

Is

Quoted 4 lines

this possible, if
one of the signals registered with my program is
raised, my program
should goto background and should come to

foreground

Quoted 7 lines

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?

Quoted 7 lines

with regards,
M. SilambuChelvan

__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site

design software

Quoted 3 lines

http://sitebuilder.yahoo.com
-
To unsubscribe from this list: send the line

Quoted 7 lines

"unsubscribe linux-newbie" in

Quoted 2 lines

the body of a message to [email protected] More majordomo info at

http://vger.kernel.org/majordomo-info.html

Quoted 1 line

Please read the FAQ at

http://www.linux-learn.org/faqs

__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

#3 lore raw
In reply to John T. Williams
Quoted 21 lines · John T. Williams

I think part of the problem here might be conceptual.

If I understand what you want, you'd like, on a signal, for your program to go into the background (still running but no longer blocking the terminal input/output). On another signal you would like the program to come back to the foreground.

If this is the case, the problem you're facing is that the program being in the foreground and the background is actually part of the shell, not a property of the program running. Most Shells provide a way to switch programs from the foreground to the background and back.
BASH for example I could type

$>./myprogram
(cntr + Z) //suspends the current program and prings the prompt back $>bg (runs the program in the background)

The only way that I know of to force a program to run in the background in the code is to make it a daemon which is done by forking twice

int main( ) {

Quoted 10 lines
  if(fork()==0)  {
           if( fork() == 0 )  {
                   program code starts here
            }
           exit(0);

}
exit(0);
}

I don't feel it's required to fork() twice for making it a daemon, once is enough. Ya, but should use setpgrp() or setpid() and close stdin, out and err after the fork and go to infinite loop.

-Raghu

Quoted 40 lines

----- Original Message -----
From: "Silambu Chelvan" <[email protected]>
To: <[email protected]>
Sent: Monday, September 22, 2003 6:48 AM
Subject: Switching between foreground and background

Quoted 18 lines

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?

with regards,
M. SilambuChelvan

Quoted 11 lines

__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

Quoted 8 lines

-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

#4 lore raw

I have to admit that I don't know why, but every resource I've ever read about causing programs to gain daemon behavior has involved forking 2 times and then starting the program code. If anyone here does know why, I'd would be happy to hear it.

----- Original Message -----
From: "Raghuveer" <[email protected]>
To: "John T. Williams" <[email protected]>
Cc: "Silambu Chelvan" <[email protected]>; <[email protected]>
Sent: Tuesday, September 23, 2003 7:01 AM
Subject: Re: Switching between foreground and background

Quoted 5 lines
Quoted 3 lines · John T. Williams

I think part of the problem here might be conceptual.

If I understand what you want, you'd like, on a signal, for your program

to

Quoted 2 lines
Quoted 2 lines

go into the background (still running but no longer blocking the terminal input/output). On another signal you would like the program to come back

to

Quoted 3 lines
Quoted 3 lines

the foreground.

If this is the case, the problem you're facing is that the program being

in

Quoted 10 lines
Quoted 10 lines

the foreground and the background is actually part of the shell, not a property of the program running. Most Shells provide a way to switch programs from the foreground to the background and back. BASH for example I could type

$>./myprogram
(cntr + Z) //suspends the current program and prings the prompt back $>bg (runs the program in the background)

The only way that I know of to force a program to run in the background

in

Quoted 13 lines
Quoted 13 lines

the code is to make it a daemon which is done by forking twice

int main( ) {

  if(fork()==0)  {
           if( fork() == 0 )  {
                   program code starts here
            }
           exit(0);

}
exit(0);
}

Quoted 40 lines

I don't feel it's required to fork() twice for making it a daemon, once is enough. Ya, but should use setpgrp() or setpid() and close stdin, out and err after the fork and go to infinite loop.

-Raghu

Quoted 34 lines

----- Original Message -----
From: "Silambu Chelvan" <[email protected]>
To: <[email protected]>
Sent: Monday, September 22, 2003 6:48 AM
Subject: Switching between foreground and background

Quoted 24 lines

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?

with regards,
M. SilambuChelvan

__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie"

in

Quoted 8 lines
Quoted 8 lines
Quoted 5 lines

the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

-
To unsubscribe from this list: send the line "unsubscribe linux-newbie"

in

Quoted 13 lines
Quoted 6 lines

the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

#5 lore raw

guide to inline assembly

Hi
I was looking for a tutorial / guide about mixing C and assembly
code. If some one can point a good guide , that would be great.

palash

#6 lore raw

Re: guide to inline assembly

Quoted 2 lines · palash kasodhan

I was looking for a tutorial / guide about mixing C and assembly
code. If some one can point a good guide , that would be great.

http://linuxassembly.org/

There's exists a [email protected] mailing list too.

Khoda Hafez
Progga

#7 lore raw
In reply to John T. Williams

I don't know if that's the way to gain daemon behavior. If you fork twice, your process stills attached to the controlling terminal.
I guess you could finish that terminal session without problem. But I think that a real daemon has to detach him self from the controlling tty. You can do that, like Raghu said, calling setpgrp and setsid and closing stdout an err.
There is also a function in unistd that does all of this for you.

int daemon(int nochdir,int noclose); (see "man daemon")

Don't know if exists in all unix or linux distros. But it seems to work. Saludos.

Manuk

-----Mensaje original-----
De: [email protected] [mailto:[email protected]]En nombre de John T. Williams
Enviado el: Martes, 23 de Septiembre de 2003 13:07
Para: [email protected]; John T. Williams
CC: Silambu Chelvan; [email protected]; [email protected]
Asunto: Re: Switching between foreground and background

I have to admit that I don't know why, but every resource I've ever read about causing programs to gain daemon behavior has involved forking 2 times and then starting the program code. If anyone here does know why, I'd would be happy to hear it.

----- Original Message -----
From: "Raghuveer" <[email protected]>
To: "John T. Williams" <[email protected]>
Cc: "Silambu Chelvan" <[email protected]>; <[email protected]>
Sent: Tuesday, September 23, 2003 7:01 AM
Subject: Re: Switching between foreground and background

Quoted 5 lines
Quoted 3 lines · John T. Williams

I think part of the problem here might be conceptual.

If I understand what you want, you'd like, on a signal, for your program

to

Quoted 2 lines
Quoted 2 lines

go into the background (still running but no longer blocking the terminal input/output). On another signal you would like the program to come back

to

Quoted 3 lines
Quoted 3 lines

the foreground.

If this is the case, the problem you're facing is that the program being

in

Quoted 10 lines
Quoted 10 lines

the foreground and the background is actually part of the shell, not a property of the program running. Most Shells provide a way to switch programs from the foreground to the background and back. BASH for example I could type

$>./myprogram
(cntr + Z) //suspends the current program and prings the prompt back $>bg (runs the program in the background)

The only way that I know of to force a program to run in the background

in

Quoted 13 lines
Quoted 13 lines

the code is to make it a daemon which is done by forking twice

int main( ) {

  if(fork()==0)  {
           if( fork() == 0 )  {
                   program code starts here
            }
           exit(0);

}
exit(0);
}

Quoted 40 lines

I don't feel it's required to fork() twice for making it a daemon, once is enough. Ya, but should use setpgrp() or setpid() and close stdin, out and err after the fork and go to infinite loop.

-Raghu

Quoted 34 lines

----- Original Message -----
From: "Silambu Chelvan" <[email protected]>
To: <[email protected]>
Sent: Monday, September 22, 2003 6:48 AM
Subject: Switching between foreground and background

Quoted 24 lines

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?

with regards,
M. SilambuChelvan

__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie"

in

Quoted 8 lines
Quoted 8 lines
Quoted 5 lines

the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

-
To unsubscribe from this list: send the line "unsubscribe linux-newbie"

in

Quoted 8 lines
Quoted 6 lines

the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

Quoted 5 lines

-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#8 lore raw

gcc : cpp Memory Exhausted

In reply to John T. Williams

Hi - I wonder whether anybody can help me?

I'm compiling some code on a robot (Slack 7.0, P 1). I've compiled it many times before on this platform with no problem. Now when I make I get the error:

cpp: Memory exhausted

...and the process fails
Anybody got any ideas?

regards

Matt

==============================================
Matthew Studley

The Intelligent Computer Systems Centre
Faculty of Computing, Engineering
and Mathematical Sciences
Coldharbour Lane, Frenchay
BS16 1QY
United Kingdom

tel : 07092 306548
fax : 07092 306548

#9 lore raw

Re: gcc : cpp Memory Exhausted

I have 'solved' the problem; I was just compiling one of the source files, I deleted all compiled output and recompiled all the source. No problem!

This is bizarre!

I have no idea what was causing this though, and would really appreciate any ideas!

regards

Matt

----- Original Message -----
From: Matthew Studley <[email protected]>
To: <[email protected]>
Sent: Wednesday, September 24, 2003 3:36 PM
Subject: gcc : cpp Memory Exhausted

Quoted 3 lines

Hi - I wonder whether anybody can help me?

I'm compiling some code on a robot (Slack 7.0, P 1). I've compiled it

many

Quoted 22 lines

times before on this platform with no problem. Now when I make I get the error:

cpp: Memory exhausted

...and the process fails
Anybody got any ideas?

regards

Matt

==============================================
Matthew Studley

The Intelligent Computer Systems Centre Faculty of Computing, Engineering
and Mathematical Sciences
Coldharbour Lane, Frenchay
BS16 1QY
United Kingdom

Quoted 8 lines

tel : 07092 306548
fax : 07092 306548

-
To unsubscribe from this list: send the line "unsubscribe

linux-c-programming" in

Quoted 5 lines

the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

This incoming email to UWE has been independently scanned for viruses and

any virus detected has been removed using McAfee anti-virus software

Quoted 3 lines
#10 lore raw

Re: gcc : cpp Memory Exhausted

Quoted 7 lines · Matthew Studley

I have 'solved' the problem; I was just compiling one of the source files, I deleted all compiled output and recompiled all the source. No problem!

This is bizarre!

I have no idea what was causing this though, and would really appreciate any ideas!

I don't know the exact reason, but I'l give some suggestions:

1. recursive #include, as when a.h #includes "b.h", and b.h #includes "a.h" or just a.h #includes "a.h"...

2. memory corruption. the compilation of all sources could had caused the corrupted cache to be discarded.

3. corruption of disk transfers
(by means of an invalid selected DMA or ilegal cable)

Regards,
Luciano Rocha