gpt4 book ai didi

c - 在 C 中将子 pid 打印到标准输出

转载 作者:行者123 更新时间:2023-11-30 16:27:11 25 4
gpt4 key购买 nike

我目前正在尝试对目录和子目录中的多个文件进行排序,并将排序后的文件输出到不同的指定目录。我应该对我遇到的每个子目录和每个实际排序使用 fork 进程。我还应该跟踪我创建了多少个子进程并打印每个子进程的 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:”,尽管只在我的主进程中调用它。谁能解释一下为什么要这样做?

最佳答案

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

解决方案:在 fork 子项之前显式fflush(stdout)

关于c - 在 C 中将子 pid 打印到标准输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52819315/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com