【问题标题】:printing child pids to stdout in C在 C 中将子 pid 打印到标准输出
【发布时间】:2018-10-15 14:45:32
【问题描述】:

我目前正在尝试对目录和子目录中的多个文件进行排序,并将排序后的文件输出到不同的指定目录。我应该为遇到的每个子目录和每个实际排序使用分叉进程。我还应该跟踪我创建了多少子进程并打印每个子进程的 PID(进程 ID)。

void iterate_dir(char* current_path, char* new_dir, char* column_name){

DIR *dd = opendir(current_path);
struct dirent *curr;
int status = -1;

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

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

        continue;

    }
    //Just formating stuff, I'm only supposed to sort files ending with .csv
    if(curr->d_type!=DT_DIR){

        if(strlen(curr->d_name)<4){

            continue;

        }

        char* extension = strrchr(curr->d_name,'.');
        if(extension==NULL){

            continue;

        }

        if(strcmp(extension,".csv")!=0){

            continue;

        }

    }

    int pid = fork();

    if(pid==0){


        (*num_of_child_procs)++;

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

            char new_path[strlen(curr->d_name)+strlen(current_path)+2];
            sprintf(new_path,"%s/%s",current_path,curr->d_name);
            iterate_dir(new_path, new_dir, column_name);
            exit(getpid());


        }else{

            //more formatting stuff, I'm supposed to output a new sorted file to the specified output directory
            char file_name[strlen(curr->d_name)-3];
            memcpy(file_name,curr->d_name,strlen(curr->d_name)-4);
            file_name[strlen(curr->d_name)-4] = '\0';
            char new_path[strlen(file_name)+strlen(column_name)+strlen(new_dir)+12+1+1];
            sprintf(new_path,"%s/%s%s%s%s",new_dir,file_name,"-sorted-",column_name,".csv");
            char old_path[strlen(curr->d_name)+strlen(current_path)+2];
            sprintf(old_path,"%s/%s",current_path,curr->d_name);
            int output = open(new_path, O_WRONLY|O_CREAT, 0666);
            int input = open(old_path, O_RDONLY);
            sort(input,output)

            if(ret==-1){

                _exit(-1);

            }

            if(ret==-2){

                remove(new_path);

            }

            exit(getpid());

        }


    }


}


//This is where I print the pids for each process, by using the return status from each child process exit()
while(wait(&status)>0){

    printf("%d,",status);

}

closedir(dd);

}

在我的 main 我这样调用函数

printf("Initial PID:%d\n",getpid());

printf("PIDS of all child processes:");

//mmap is only used for obtaining the number of processes, not printing their pids
num_of_child_procs = mmap(NULL, sizeof *num_of_child_procs, PROT_READ | 
PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, -1, 0);
*num_of_child_procs = 0;
iterate_dir(dir_name, new_dir_name, column_name);
printf("\nTotal number of processes:%d\n", *num_of_child_procs);
munmap(num_of_child_procs, sizeof *num_of_child_procs);

除了输出之外,一切都很好。在包含 2 个 .csv 文件的目录、包含 2 个 .csv 文件的子目录和包含 1 个 .csv 文件的子目录的子目录(总共 7 个进程)上运行代码,打印出

Initial PID:9773
PIDS of all child processes:PIDS of all child processes:PIDS of all child 
processes:PIDS of all child processes:PIDS of all child processes:PIDS of 
all child processes:13312,PIDS of all child processes:13056,12800,12544,PIDS 
of all child processes:12032,12288,11776,
Total number of processes:7

如您所见,由于某种原因,它会多次打印“所有子进程的 PIDS:”,尽管只在我的主进程中调用它。谁能解释它为什么这样做?

【问题讨论】:

  • "我使用每个子进程 exit() 的返回状态打印每个进程的 pids" 您阅读了 wait() 的哪些文档?

标签: c printing process pid


【解决方案1】:

printf("PIDS of all child processes:"); 没有换行符,并且可能没有被刷新,而只是位于标准输出缓冲区中。这个缓冲区被每个子进程继承;每个孩子都有自己的副本,并在终止时将其刷新。

解决方案:fflush(stdout) 在分叉子代之前明确。

【讨论】:

  • 非常感谢,从来不知道这个功能很高兴你能解释一下
猜你喜欢
  • 2013-05-01
  • 1970-01-01
  • 2012-09-29
  • 1970-01-01
  • 1970-01-01
  • 2017-09-08
  • 2015-04-08
  • 1970-01-01
  • 2015-08-31
相关资源
最近更新 更多