#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    81
    Rep Power
    12

    Fork and Send child to background


    Hi I want to send a child process to the background, how do I instruct it to? Thanks
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,155
    Rep Power
    2222
    I'm trying to understand what you are trying to do. The only way I've heard of for sending a process to the background is from the command line with a "&" , which makes it a job. The only difference that makes, AFAIK, is that a job does not have access to stdin, stdout, or stderr, but it's still in the parent's process group and in the parent's session group -- and its parent is the shell. In other words, sending a process to the background seems to be a shell thing.

    However, in doing a Google search I found several hits refering to daemons as "background processes" or "running in the background". Are you trying to make the child process a daemon? Or give it some daemon-like qualities?

    In case they cover your problem, here are my notes from the daemon chapter of Kurt Wall's "Linux Programming by Example":

    Chapter 9 Daemons (pp 191-200)
    1. almost always run with superuser privileges because they provide system services to user programs
    2. do not have a controlling terminal (are non-interactive)
    3. generally are process-group and session leaders
    4. often serve as the process in their group and session
    5. its parent is init ($$==1)

    Creating a daemon (pp 192-196):
    1. fork and make the parent exit
    2. create a new session by calling setsid -- gets rid of the controlling terminal
    3. make the root directory the working directory (to prevent filesystem problems)
    4. set umask to 0, giving daemon free reign to set umask to whatever it wants to
    5. close any file descriptors that the child inherited and does not need (eg stdin/stdout/stderr)

    example on pp 194-195

    NOTE: this way, cannot run the daemon as a user
    in order to run it as a user, need to use system logs

    Handling Errors (pp 196-200):
    need to log errors, etc, to a system log:
    <syslog.h>
    void openlog(char *ident, int option, int facility);
    void closelog(void);
    void syslog(int priority, char *format, ...);

    OR options for openlog:
    LOG_CONS
    LOG_NDELAY
    LOG_PERROR
    LOG_PID

    facility is one of:
    LOG_AUTHPRIV, LOG_CRON, LOG_DAEMON, LOG_KERN, LOG_LOCAL[0-7], LOG_LPR,
    LOG_MAIL, LOG_NEWS, LOG_SYSLOG, LOG_USER, LOG_UUCP

    priority is one of:
    LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG

    example on pp 198-199
    Last edited by dwise1_aol; April 14th, 2003 at 10:13 AM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    81
    Rep Power
    12

    Write your own shell


    I am writing my own shell. I have forked the process, and have used the setsid, so I guess that leaves only changing the stdin and stout and sterror. Thanks!

IMN logo majestic logo threadwatch logo seochat tools logo