使用 fork 显示循环中的进程数
Display number of processes in loop with fork
如何显示创建的进程数?
(不使用公式)
for (i=0; i<3; i++)
fork();
count = count + 1;
printf("%d",count);
只打印一次计数值是比较容易的部分。因为可以在for循环之前拿到进程pid。然后在 for 循环之后再次获取 pid,并且仅在 pid 匹配时打印。对于计数部分,这取决于您的 child 进程是否退出。如果他们退出,解决方案会更容易。如果 child 进程退出,下面的代码演示了一种可能的解决方案(为简洁起见,没有进行完整的错误检查)。这个想法是每个 child 进程计算自己的 children。 Parent 等待每个 child 完成并添加其计数。还没有时间完全 test/debug 该程序,因此可能存在一些错误。但希望能给你一个大概的想法。
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main(void)
{
pid_t before_pid, after_pid;
pid_t forked_pid;
int count;
int i;
int status;
before_pid = getpid();
count = 1; /* count self */
for (i = 0; i < 3; i++) {
forked_pid = fork();
if (forked_pid > 0) {
waitpid(forked_pid, &status, 0);
/* parent process - count child and descendents */
count += WEXITSTATUS(status);
} else {
/* Child process - init with self count */
count = 1;
}
}
after_pid = getpid();
if (after_pid == before_pid) {
printf("%d processes created\n", count);
}
return (count);
}
有很多方法可以做到这一点,一个好的方法是让每个子进程将一个字节写入原始进程可以读取的文件描述符。请注意,为简洁起见,以下代码绝对不包含任何错误检查。此外,我们仅报告生成的进程数 (7),而不是计算原始进程数以获得 8 的计数:
int main(void) {
int fd[2];
int depth = 0; /* keep track of number of generations from original */
int i;
pipe(fd); /* create a pipe which will be inherited by all children */
for(i=0; i<3; i++) {
if(fork() == 0) { /* fork returns 0 in the child */
write(fd[1], &i, 1); /* write one byte into the pipe */
depth += 1;
}
}
close(fd[1]); /* exercise for the reader to learn why this is needed */
if( depth == 0 ) { /* original process */
i=0;
while(read(fd[0],&depth,1) != 0)
i += 1;
printf( "%d total processes spawned", i);
}
return 0;
}
如何显示创建的进程数?
(不使用公式)
for (i=0; i<3; i++)
fork();
count = count + 1;
printf("%d",count);
只打印一次计数值是比较容易的部分。因为可以在for循环之前拿到进程pid。然后在 for 循环之后再次获取 pid,并且仅在 pid 匹配时打印。对于计数部分,这取决于您的 child 进程是否退出。如果他们退出,解决方案会更容易。如果 child 进程退出,下面的代码演示了一种可能的解决方案(为简洁起见,没有进行完整的错误检查)。这个想法是每个 child 进程计算自己的 children。 Parent 等待每个 child 完成并添加其计数。还没有时间完全 test/debug 该程序,因此可能存在一些错误。但希望能给你一个大概的想法。
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main(void)
{
pid_t before_pid, after_pid;
pid_t forked_pid;
int count;
int i;
int status;
before_pid = getpid();
count = 1; /* count self */
for (i = 0; i < 3; i++) {
forked_pid = fork();
if (forked_pid > 0) {
waitpid(forked_pid, &status, 0);
/* parent process - count child and descendents */
count += WEXITSTATUS(status);
} else {
/* Child process - init with self count */
count = 1;
}
}
after_pid = getpid();
if (after_pid == before_pid) {
printf("%d processes created\n", count);
}
return (count);
}
有很多方法可以做到这一点,一个好的方法是让每个子进程将一个字节写入原始进程可以读取的文件描述符。请注意,为简洁起见,以下代码绝对不包含任何错误检查。此外,我们仅报告生成的进程数 (7),而不是计算原始进程数以获得 8 的计数:
int main(void) {
int fd[2];
int depth = 0; /* keep track of number of generations from original */
int i;
pipe(fd); /* create a pipe which will be inherited by all children */
for(i=0; i<3; i++) {
if(fork() == 0) { /* fork returns 0 in the child */
write(fd[1], &i, 1); /* write one byte into the pipe */
depth += 1;
}
}
close(fd[1]); /* exercise for the reader to learn why this is needed */
if( depth == 0 ) { /* original process */
i=0;
while(read(fd[0],&depth,1) != 0)
i += 1;
printf( "%d total processes spawned", i);
}
return 0;
}