linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Mariano Moreyra" <moremari@aca.org.ar>
To: "'John T. Williams'" <jtwilliams@vt.edu>, rvk@gsecone.com
Cc: 'Silambu Chelvan' <silambu_mdu@yahoo.com>,
	linux-newbie@vger.kernel.org,
	linux-c-programming@vger.kernel.org
Subject: RE: Switching between foreground and background
Date: Tue, 23 Sep 2003 14:24:05 -0300	[thread overview]
Message-ID: <000301c381f7$7d7487c0$0b04a8c0@aca.org.ar> (raw)
In-Reply-To: <003e01c381ec$c76473a0$ed64a8c0@descartes>

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: linux-c-programming-owner@vger.kernel.org
[mailto:linux-c-programming-owner@vger.kernel.org]En nombre de John T.
Williams
Enviado el: Martes, 23 de Septiembre de 2003 13:07
Para: rvk@gsecone.com; John T. Williams
CC: Silambu Chelvan; linux-newbie@vger.kernel.org;
linux-c-programming@vger.kernel.org
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" <rvk@gsecone.com>
To: "John T. Williams" <jtwilliams@vt.edu>
Cc: "Silambu Chelvan" <silambu_mdu@yahoo.com>;
<linux-newbie@vger.kernel.org>
Sent: Tuesday, September 23, 2003 7:01 AM
Subject: Re: Switching between foreground and background


> John T. Williams wrote:
>
> >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);
> >}
> >
> >
> 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
>
> >
> >----- Original Message -----
> >From: "Silambu Chelvan" <silambu_mdu@yahoo.com>
> >To: <linux-newbie@vger.kernel.org>
> >Sent: Monday, September 22, 2003 6:48 AM
> >Subject: Switching between foreground and background
> >
> >
> >
> >
> >>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 majordomo@vger.kernel.org
> >>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 majordomo@vger.kernel.org
> >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 majordomo@vger.kernel.org
> 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 majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


  parent reply	other threads:[~2003-09-23 17:24 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20030922104825.81134.qmail@web20002.mail.yahoo.com>
     [not found] ` <003501c3811f$610395a0$ed64a8c0@descartes>
     [not found]   ` <3F702813.8030200@gsecone.com>
2003-09-23 16:07     ` Switching between foreground and background John T. Williams
2003-09-23 16:19       ` guide to inline assembly palash kasodhan
2003-09-23 17:57         ` Progga
2003-09-23 17:24       ` Mariano Moreyra [this message]
2003-09-24 14:36       ` gcc : cpp Memory Exhausted Matthew Studley
2003-09-24 15:12         ` Matthew Studley
2003-09-24 22:23           ` Luciano Miguel Ferreira Rocha

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='000301c381f7$7d7487c0$0b04a8c0@aca.org.ar' \
    --to=moremari@aca.org.ar \
    --cc=jtwilliams@vt.edu \
    --cc=linux-c-programming@vger.kernel.org \
    --cc=linux-newbie@vger.kernel.org \
    --cc=mariano_moreyra@aca.org.ar \
    --cc=rvk@gsecone.com \
    --cc=silambu_mdu@yahoo.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).