Proces w tle (demon) w C nie execvp () -ing
Próbuję więc uruchomić proces w tle i wykonać z niego execvp. Kiedy wprowadzam cp / path / file / var / tmp, proces nie kopiuje pliku.
Oto mój kod referencyjny:
void cmd_bg(char command[])
{
pid_t process_id = 0;
pid_t sid = 0;
char* argv[512];
getArgv(command,argv);
// Create child process
process_id = fork();
// Indication of fork() failure
if (process_id < 0)
{
printf("fork failed!\n");
// Return failure in exit status
exit(1);
}
// PARENT PROCESS. Need to kill it.
if (process_id > 0)
{
printf("process_id of child process %d \n", process_id);
// return success in exit status
exit(0);
}
//unmask the file mode
umask(0);
//set new session
sid = setsid();
if(sid < 0)
{
// Return failure
exit(1);
}
// Change the current working directory to root.
chdir("/");
// Close stdin. stdout and stderr
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
execvp(argv[0],argv);
}
}
void getArgv(char command[], char* argv[512])
{
char *token;
int count = 0;
int pid = 0;
int ex = 0;
char *absPath;
char pwdtemp[512];
strcpy(pwdtemp,pwd);
token = strtok(command, " ");
while(token!=NULL)
{
argv[count++] = token;
printf("%s\n",argv[count-1]);
token = strtok(NULL," ");
}
argv[count] = '\0';
}
Mam szczerą nadzieję, że ktoś może mi pomóc. Dzięki!
EDYTOWAĆ: Znalazłem rozwiązanie. I CANT SELF-ODPOWIEDŹ, PONIEWAŻ NIE MAM 100 REPS YET. W KAŻDYM MIEJSCU DLA LUDZI, KTÓRZY MOGĄ ZOBACZYĆ TĄ NICĘ W PRZYSZŁOŚCI
DOBRZE. Zrozumiałem problem.
Po pierwsze, komentowałem
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
chdir("/");
Następnie zamiast bezpośrednio wywoływać cmd_bg, utworzyłem:
void temp1(char command[])
{
int pid = fork();
if(pid==0)
cmd_bg(command);
else
waitpid(-1, NULL, 0);
}
Wydaje się, że teraz działa! Dziękuję za tonę dla twoich gości!