1

I am writing my own shell as part of course assignment. So I need to support background jobs. I am maintaining data strutures for job id and background jobs. But I need to also tell the kernel that this is a background process, so that there is only one terminal foreground process. Until now I am handling background jobs at my program level.

What is the function call to register a background process?

Bart
  • 19,692
  • 7
  • 68
  • 77
avd
  • 13,993
  • 32
  • 78
  • 99
  • 2
    I was reading this page the other day: http://www.win.tue.nl/~aeb/linux/lk/lk-10.html i like its way of teaching it – Johannes Schaub - litb Oct 02 '09 at 15:57
  • Duplicate of http://stackoverflow.com/questions/1509352/can-we-start-a-background-process-using-exec-giving-as-an-argument – Jonathan Leffler Oct 02 '09 at 17:50
  • @Jonathan, that's quite different. This guy wants to do job control, while that other question wants to start daemons. The daemons are disconnected from terminals, and don't interact with the user at all. – Johannes Schaub - litb Oct 02 '09 at 20:50
  • @litb: I think not. aditya's wording is awkward, but simplistic job control does seem to be what is desired, not daemonization. – ephemient Oct 04 '09 at 03:24

3 Answers3

2

If you want a process to not be part of the terminal's controlling group, the simplest method is to simply give it a different group.

switch (fork()) {
    case 0:
        setpgid(getpid(), getpid());
        execvp(...);
ephemient
  • 198,619
  • 38
  • 280
  • 391
1

On Linux look at the daemon function:

 int daemon(int nochdir, int noclose);

If the daemon function doesn't exist on the system you're using you need to use setsid and fork instead.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
atomice
  • 3,062
  • 17
  • 23
  • I dont want a daemon process, but I just want to run a process in background. So I need a way to inform kernel. – avd Oct 03 '09 at 04:30
1

Sorry, misread your question. You need to use thetcsetpgrpfunction.

Read this section in the GNU C Library Manual for details:

http://www.gnu.org/s/libc/manual/html_node/Job-Control.html

atomice
  • 3,062
  • 17
  • 23