Sunday, 15 January 2012

c - Programmatically check if a process is being run in the background -



c - Programmatically check if a process is being run in the background -

2 questions:

1) there linux/posix api know if process has been invoked background process?

linux> myprogram &

can code myprogram observe has been invoked run in background (via &) ?

2) there linux/posix api create process run in background if has been started foreground process? i.e. somehow 'detach' shell @ runtime.. (either detach shell completely, or run background process of shell).

linux> myprogram **** starting myprogram background job **** linux>

the shell prompt should come right me since myprogram has detached shell , running in background

1) there 2 ways know whether process in background

have signal handler sigttin /sigttout , non-blocking read/write depending on signal handler(stdin/stdout).

check process-group , match terminals' getpgrp() == tcgetpgrp(stdout_fileno)

you need repeat check, process can foregrounded or backgrounded anytime.

2) there daemon function set process in background. advisable redirect application prints syslog or other file while daemonizing.

if (daemonize) { //redirect prints syslog or other logfile daemon(0, 0); }

where daemonize can arguement application whether go background or not.

c linux background-process

No comments:

Post a Comment