Linux에서 분기 된 자식 프로세스에서 즉시 반환하는 방법은 무엇입니까?
문제
나는 Linux C 개발에 대해 Familar가 아닙니다.
내 코드 :
....
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{
...
}
.
내가하고 싶은 것은 아동 프로세스의 새로운 프로세스에서 즉시 반환하는 것입니다.
current 프로그램에서 execv (workdir , args);
가 반환되지 않기 때문입니다.(긴 실행중인 프로세스를 시작해야합니다).
제가하고 싶은 것은이 긴 실행 프로세스를 시작하고 C 코드에서 즉시 반환하여 C 코드를 종료 할 수 있습니다.
어떻게해야합니까? 어쩌면 시작된 새 자식을 사용하여 데몬을 처리합니다. API 호출로 수행하는 방법은 무엇입니까?
해결책
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.
제휴하지 않습니다 StackOverflow