Background Process (Daemon) in C not execvp() -ing

给你一囗甜甜゛ 提交于 2019-12-31 01:57:09

问题


So, I am trying to run a background process and execvp from it. When i enter cp /path/file /var/tmp, the process is not copying the file.

Here is my code for reference:

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';
}

I sincerely hope someone can help me. Thanks!

EDIT: I FOUND THE SOLUTION. I CANT SELF-ANSWER BECAUSE I DONT HAVE 100 REPS YET. ANYWAYS FOR PEOPLE WHO MIGHT VIEW THIS THREAD IN THE FUTURE:

OK. I figured out the problem.

Firstly I Commented out the

close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
chdir("/");

Next, instead of calling the cmd_bg directly, I created:

void temp1(char command[])
{
    int pid = fork();
    if(pid==0)
        cmd_bg(command);

    else
        waitpid(-1, NULL, 0);
}

It seems to work now! Thanks a tonne for your input guys!


回答1:


Did you try without closing standard input, standard output, standard error




回答2:


I would check to see if execvp returns (it only returns on error), and see what errno it produces. That may help you track down the issue.




回答3:


any chance that you called your program cp ? if this is the case you execvp() will call the program over and over again, instead of /bin/cp. It also would be helpful to get the complete code.



来源:https://stackoverflow.com/questions/10315240/background-process-daemon-in-c-not-execvp-ing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!