2 Star 1 Fork 0

陈孝松/blog

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
fork.c 1.21 KB
一键复制 编辑 原始数据 按行查看 历史
ChenXiaoSong 提交于 2025-02-21 11:50 +08:00 . rename to course/kernel/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <dirent.h>
static void read_dir(void) {
DIR *dir;
struct dirent *entry;
dir = opendir("/mnt/");
if (dir == NULL) {
perror("opendir");
exit(EXIT_FAILURE);
}
// 只读取目录下的第一个文件
entry = readdir(dir);
printf("%s\n", entry->d_name);
// 关闭目录
closedir(dir);
}
int main() {
int num_processes = 3; // 你可以修改这个值来创建不同数量的子进程
pid_t pid;
for (int i = 0; i < num_processes; i++) {
pid = fork();
if (pid < 0) {
// fork失败
perror("fork failed");
exit(1);
} else if (pid == 0) {
// 子进程
printf("child process %d, pid %d, ppid %d\n", i + 1, getpid(), getppid());
while (1)
;
exit(0); // 子进程结束
} else {
// 父进程
printf("parent process pid %d, created child %d with pid %d\n", getpid(), i + 1, pid);
}
}
read_dir();
// 等待所有子进程完成
for (int i = 0; i < num_processes; i++) {
wait(NULL);
}
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/chenxiaosonggitee/blog.git
git@gitee.com:chenxiaosonggitee/blog.git
chenxiaosonggitee
blog
blog
master

搜索帮助