Reputation: 1309
I am not familar with linux C development.
my code:
....
if((pid=fork())==0){
//child process
//start a process, may be need to change execv to other call
execv (workdir , args);
}else if (pid<0){
...
}else{
...
}
What I want to do is to return immediately from started new process in child process.
Because in the currrent program, execv (workdir , args);
will not return. (I need to start a long running process).
What I want to do is start this long run process and return immediately in my C code, so that my C program can exit.
How can I do this? Maybe make my started new child process a daemon, how to do it by api call?
Upvotes: 0
Views: 294
Reputation: 43024
Something like this:
close(0); open("/dev/null", 0);
close(1);
if(open("/dev/null", O_WRONLY) < 0) {
perror("/dev/null");
exit(1);
}
switch(pid = fork()) {
case -1:
perror(argv[0]);
exit(1);
break;
case 0:
fflush(stdout);
close(2); dup(1);
setpgrp();
setsid();
execv(argv[0], argv);
execvp(argv[0], argv);
perror(argv[0]);
_exit(1);
break;
default:
exit(0);
break;
}
Will fork and detach a process, and exit. It "daemonizes" the program.
Upvotes: 3