Start a process in the background in Linux with C -


I'm trying to do something strange here. I need to start a process from a daemon, lockat, which will run in the background and print on the terminal to not take control of the studin. This is to logging so that the ideal logic will print the log message, while still allowing the user to input standard commands and start the program from a shell. Even I have a code for the daemon. The program, logcat, starts and displays log messages, but I can not enter any command in stdin because it appears that the program took control of stdin is.

int main (int argc, char ** argv, char ** env) {int fd; If ((FD = Open ("/ dev / console", O_RDWR)) <0} {fd = open ("/ dev / null", O_RDWR);} Printf ("This is an exam \ n"); Dup2 1, FD); Dup2 (2, FD); Pid_t childpid = fork; if (childpid == -1) {perror ("failed to fork, do not start lockout"); return 1;} If (childpid = = 0) {// This is a child, exec logcat setid (); int execReturn = execl ("/ system / bin / logcat", "logcat", (four *) 0);} Else {// This parent Nothing close (FD); return 0;} closed (FD); return 0;}

thanks

This command appears to be for Android development - the ones who earn The important task that you need to fix is ​​to make sure that you close your current standard input (terminal) and / dev / null / for input device:

  close (0); if ((FD = open ("/ dev / null", O_RDONLY)) = 0) ... error - Failed to open / dev / null!  

This means that nothing will be read from your demuted child processing terminal.


I think what you want to do:

  1. To run your launcher program from a command line, which includes standard input, standard output and ' Terminal 'will be the standard error associated with it.
  2. Inside your program, you want to change the standard input, so it comes from / dev / null .
  3. You only want to leave the standard output - you want the logs to be written in the current standard output.
  4. You probably also want to leave the standard error alone.

At some point in the proceedings, you properly (borrow the link from the answer of bstpierre), make sure that the terminal which is connected to you is not your controller terminal, so that the terminal Interrupts and hangouts sent on do not affect your daemon The pipeline is simpler than you set up - you should deal with standard input and change the standard output and standard error (instead of changing the output and leaving the input unchanged).

Now, you want the output to go to / dev / console ; If so, then this code is appropriate to open / dev / console . However, if / dev / console can not open, then returning to / dev / null is not appropriate; Your program should report an error and fail (because there is no way to write on / dev / null !). Make sure that you open the console with the O_NOCTTY flag, it does not have a controller terminal for the daemon.

Last comment I do:

  • Do you really want to show random text on your terminal or console when it is in use for other things?

I do not like this when this happens. / P>


Also see:


Comments

Popular posts from this blog

Eclipse CDT variable colors in editor -

AJAX doesn't send POST query -

wpf - Custom Message Box Advice -