Hintergrundprozess (Daemon) in C nicht execvp () -ing
Daher versuche ich, einen Hintergrundprozess auszuführen und daraus ein Execvp-Programm zu erstellen. Wenn ich cp / path / file / var / tmp eingebe, kopiert der Prozess die Datei nicht.
Hier ist mein Code als Referenz:
<code>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'; } </code>
Ich hoffe aufrichtig, dass mir jemand helfen kann. Vielen Dank!
BEARBEITEN: Ich habe die Lösung gefunden. Ich kann mich nicht selbst beantworten, weil ich noch keine 100 Antworten habe. JEDERWEG FÜR MENSCHEN, DIE DIESEN FADEN IN DER ZUKUNFT ANSEHEN KÖNNTEN:
OK. Ich habe das Problem herausgefunden.
Zuerst habe ich das auskommentiert
<code>close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO); chdir("/"); </code>
Anstatt cmd_bg direkt aufzurufen, habe ich Folgendes erstellt:
<code>void temp1(char command[]) { int pid = fork(); if(pid==0) cmd_bg(command); else waitpid(-1, NULL, 0); } </code>
Es scheint jetzt zu funktionieren! Vielen Dank für Ihre Beiträge!