小编Lex*_*exa的帖子

为什么在fork之后关闭文件描述符会影响子进程?

我想通过按钮单击一个在linux中运行程序,因此我编写了一个函数execute:

void execute(const char* program_call, const char* param )
{
    pid_t child = vfork();

    if(child == 0) // child process
    {
        int child_pid = getpid();

        char *args[2]; // arguments for exec
        args[0] = (char*)program_call; // first argument is program_call
        args[1] = (char*)param;

        // close all opened file descriptors:
        const char* prefix = "/proc/";
        const char* suffix = "/fd/";
        char child_proc_dir[16]; 
        sprintf(child_proc_dir,"%s%d%s",prefix,child_pid, suffix);

        DIR *dir;
        struct dirent *ent;

        if ((dir = opendir (child_proc_dir)) != NULL) {
            // get files …
Run Code Online (Sandbox Code Playgroud)

c linux fork file-descriptor exec

5
推荐指数
1
解决办法
9146
查看次数

标签 统计

c ×1

exec ×1

file-descriptor ×1

fork ×1

linux ×1