- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在尝试对目录和子目录中的多个文件进行排序,并将排序后的文件输出到不同的指定目录。我应该对我遇到的每个子目录和每个实际排序使用 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/
manpage of reboot()说 Behavior inside PID namespaces Since Linux 3.4, if reboot() is called from a PI
我今天第一次在 Nvidia jetson Xavier 中安装和配置并使用了电视, 但重启后我遇到了这个问题: teamviewerd.service: 启动后无法打开 PID 文件/var/run
我在我的服务器上安装了 hhvm,在我重新启动服务器之前它一直运行良好。在 hhvm 的日志中,我看到了这个错误: Unable to read pid file /var/run/hhvm/pid
我正在尝试为我的应用程序精简,但随后无法生成 pid: $ thin -C /var/www/project_path/current/config/myproject.testing.yml sta
我正在从我的私有(private) git 存储库安装应用程序。我安装了所有依赖项并且我正在使用 Capistrano。我能够在我的本地计算机上成功运行应用程序。我正在使用 rails -v 3.2.
我已按照 DigitalOcean 指南中的步骤进行操作 here和 here使用 nginx 和 Unicorn 设置 Sinatra 服务器。我在倒数第二步: start the Unicorn
我在 C 程序中连续进行了 3 个 fork 。 1.它会以相同的顺序执行吗? (我的猜测是肯定的)。 2. 如果我做 pgrep myexecutable从 shell 中,它会按照启动的顺序给出进
我尝试通过FT_Prog更改FTDI芯片(R232R)中的PID。它可以工作,但之后我发现 Windows 7 自动重新安装 USB 设备的驱动程序,而不是 FTDI 设备。所以我想将PID改回默认值
第一次在这里发表 简单情况:在 PUTTY 中,我必须创建一个名为 admin.pid 的文件,当用户启动我正在创建的“应用程序”时,它会在其中存储 PID。我怎样才能做到这一点?谢谢 最佳答案 使用
我设法为每个单独的进程输出正确的进程 ID 顺序,但我的问题是我无法显示子进程的 PID。 我的程序能够打印 parent 的 PID 和孙子的 PID。我确实看到了 child 的 PID,但它显示
我正在从事一个项目,其中有许多 PID,我必须找出其中哪些是僵尸进程,然后终止它们的父进程以终止初始僵尸进程。我不确定是否有任何方法可以找出给定 PID 的 PPID 是什么。任何帮助将不胜感激。 最
我正在使用 htop,所以看看哪些进程占用了大量内存,以便我可以杀死它们。我有很多 tmux session 和很多类似的过程。如何检查 PID 所在的 tmux Pane ,以便确定我正在杀死我想杀
我正在通过运行跟踪应用程序: strace -f -y -qq -z -etrace=execve,... -o app.trace ./app 有没有办法确定哪个进程产生了哪个 child_proc
在我使用 exec 之后docker 容器内的命令我可以使用 exec inspect 获取 PID .问题是这个 ID 不是容器本地的,而是系统 PID。所以我会得到类似 22620 的东西,而 d
我有一个我开发的用于启动 Java 程序的 System V 初始化脚本。由于某种原因,无论何时创建 PID 文件,它都包含多个 PID 而不是一个。 下面是启动服务并写入PID文件的相关代码: da
我有一个变量 pidfile,它存储进程的 PID。 如何使用 Ruby 以编程方式终止 pidfile 中的 pid,假设我只知道文件名,而不是其中的实际 PID。 最佳答案 Process.kil
我读入了Beej's fork() primer当我调用 pid = fork(); 时,父进程获取子进程的 pid,而在子进程内部 pid = 0。 现在,由于子进程开始执行 在 fork() 语句
我正在尝试从另一个 Python 脚本运行一个 Python 脚本,并获取它的 pid 以便稍后可以终止它。 我尝试使用参数 shell=True' 的 subprocess.Popen(),但是pi
我有一个用 Cygwin 生成的进程shell 脚本,我无法用 kill 杀死它命令。即使与 Cygwin kill与 -f选项,我收到此消息: kill: couldn't open pid 123
我尝试在我的模型中为阀门构建一个 PID Controller ,我计划进行一些过程识别,获得系统对阶跃脉冲的响应和系统的传递函数,然后我可以设计 PID Controller 。但我不确定是否有用于
我是一名优秀的程序员,十分优秀!