简体   繁体   English

在C中使用wait()vs waitpid()

[英]Using wait() vs waitpid() in c

So I'm trying to traverse a directory(and subdirectories) and create new processes to sort files and traverse subdirectories. 因此,我试图遍历目录(和子目录)并创建新的过程来对文件进行排序并遍历子目录。 However, I'm having a little trouble understanding how useful my code will be. 但是,我在理解我的代码的实用性方面遇到了一些麻烦。 To my understanding, wait() will sleep the parent process until the child process terminates. 据我了解,wait()将使父进程休眠,直到子进程终止。 Currently my recursive function is 目前我的递归函数是

void iterate_dir(char* name){

    DIR *dd = opendir(name);
    struct dirent *curr;

    while((curr = readdir(dd))!=NULL){

        if((strcmp(curr->d_name,".")==0 || strcmp(curr->d_name,"..")==0) && curr->d_type==DT_DIR){

            continue;

        }

        int status = -1;
        int pid = fork();

        if(pid==0){

            //child process
            if(curr->d_type==DT_DIR){

                printf("DIRECTORY:\t%s\n", curr->d_name);
                char new_path[strlen(curr->d_name)+strlen(name)+2];
                sprintf(new_path,"%s/%s",name,curr->d_name);
                //recurse, iterate sub directory
                iterate_dir(new_path);
                _exit(getpid());


            }else{

                printf("FILE:\t%s\n", curr->d_name);
                //sort the file
                _exit(getpid());

            }


        }

        wait(&status);

    }

    closedir(dd);

}

given and initial directory, it works, but im concerned with the wait() function. 给定和初始目录,它可以工作,但是我关注的是wait()函数。 I would like the code to continue traversing the directory while the child processes are executing, and currently wait prevents this from happening. 我希望代码在子进程执行时继续遍历目录,目前正在等待以防止这种情况的发生。 But I still need to prevent zombie children. 但是我仍然需要防止僵尸儿童。 Would using waitpid() instead allow me to have this functionality(ie. allow the loop to continue through the rest of the directory while the child process executes), can I just use 1 wait in main which will prevent all processes created from becoming zombies, or is there a different approach I should take? 而是使用waitpid()允许我具有此功能(即在子进程执行时允许循环继续通过目录的其余部分),我可以只在main中使用1个wait来防止创建的所有进程变成僵尸,还是我应该采取其他方法? This is for a school project and I'm required to use fork (not exec) to create a new process for traversing sub directories and a new process for sorting each file. 这是针对学校项目的,我需要使用fork(而不是exec)来创建用于遍历子目录的新过程以及用于对每个文件进行排序的新过程。

No, changing to waitpid() won't solve the problem, since it will still suspend the loop until the child exits. 不,更改为waitpid()不会解决问题,因为它仍将暂停循环直到子项退出。

Take the wait() out of the main loop and do it at the end: 从主循环中取出wait()并在最后完成:

while (wait() > 0) {
}

wait() will wait for any child processes, and return -1 if there are no children to wait for. wait()将等待任何子进程,如果没有子进程等待,则返回-1

For that code to be really useful, there can only be one process running each time, else the output of several simultaneously running processes will mix in the standard output. 为了使该代码真正有用,每次只能运行一个进程,否则,几个同时运行的进程的输出将混入标准输出中。

So wait() is a good choice, since you want each process to wait for the child to end before launching a new child process or end its execution. 因此,wait()是一个不错的选择,因为您希望每个进程在启动新的子进程或结束其执行之前先等待子进程结束。

In a different case where you want different children processes to be run at the same time you can use waitpid(-1, &status, WNOHANG) , that will (not) wait for a child process to end, ie it will not hang if no child process has finished but it will prevent any finished child process from becoming a zombie. 在不同的情况下,如果您希望同时运行不同的子进程,则可以使用waitpid(-1, &status, WNOHANG) ,它将(不)等待子进程结束,即,如果没有,子进程将不会挂起子进程已完成,但将阻止所有已完成的子进程成为僵尸。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM