The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Fork and Send child to background
Discuss Fork and Send child to background in the C Programming forum on Dev Shed. Fork and Send child to background C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

April 13th, 2003, 09:27 PM
|
|
Contributing User
|
|
Join Date: Mar 2003
Posts: 81
Time spent in forums: 4 m 41 sec
Reputation Power: 11
|
|
|
Fork and Send child to background
Hi I want to send a child process to the background, how do I instruct it to? Thanks
|

April 14th, 2003, 10:11 AM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
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.
|

April 14th, 2003, 12:24 PM
|
|
Contributing User
|
|
Join Date: Mar 2003
Posts: 81
Time spent in forums: 4 m 41 sec
Reputation Power: 11
|
|
|
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!
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|