C语言多命令批量执行程序中子进程与对应参数匹配问题求解
问题根因
- 你当前的实现没有建立子进程PID和对应命令/参数下标的映射关系,
wait4(-1)会捕获任意一个最先退出的子进程,但你按创建顺序传入参数下标n,输出的自然是顺序对应的命令而非实际退出的子进程对应的命令。 - 你示例中cd是shell内置命令,不存在独立可执行文件,execlp执行会失败,不存在的fake命令也会执行失败,两个子进程几乎同时退出,先退出的fake子进程的状态被第一个循环(对应下标1的cd)捕获,所以出现了错误匹配。
修复方案
核心思路是:fork时记录每个子进程的PID和对应命令的关联关系,wait到退出进程的PID后,查表找到对应命令再输出。
依赖头文件补充(原有代码缺失的需新增)
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <signal.h> #include <sys/wait.h> #include <sys/resource.h>
修改后的nfork函数
// 修改nfork返回子进程PID pid_t nfork(int n, char *argv[]) { pid_t pid = fork(); if (pid == 0) { printf("Process with id:%d created for the command: %s\n", getpid(), argv[n]); fflush(stdout); if (execlp(argv[n], argv[n], NULL) == -1) { printf("\nexeclp: %s\n", strerror(errno)); exit(255); // 对应你原有启动失败的判断逻辑 } } return pid; }
修改后的主函数
// 定义进程信息结构体,存储PID和对应命令的映射 typedef struct { pid_t pid; char *cmd; } ProcInfo; int main(int argc, char *argv[]) { if (argc < 2) { printf("Usage: %s [command1] [command2] ...\n", argv[0]); return 1; } // 原有信号处理逻辑,注意SIG_ERR不是合法信号,可删除该行 signal(SIGINT, sig_hand1); ProcInfo procs[argc - 1]; // 创建子进程并记录映射 for (int f = 1; f < argc; f++) { procs[f - 1].cmd = argv[f]; procs[f - 1].pid = nfork(f, argv); } // 循环等待所有子进程退出 for (int i = 0; i < argc - 1; i++) { int status; struct rusage usage; pid_t exited_pid = wait4(-1, &status, 0, &usage); // 查找退出进程对应的命令 ProcInfo *target = NULL; for (int j = 0; j < argc - 1; j++) { if (procs[j].pid == exited_pid) { target = &procs[j]; break; } } if (!target) continue; // 输出结果 if (WIFSIGNALED(status)) { char *str = strdup(signame[WTERMSIG(status)]); printf("\nThe command \"%s\" is interrupted by the %s\n", target->cmd, str); free(str); // 释放strdup申请的内存,避免内存泄漏 } else if (WEXITSTATUS(status) == 255) { printf("The program experienced an error in starting the command: %s\n", target->cmd); } else { printf("\nThe command \"%s\" terminated with returned status code = %d\n", target->cmd, WEXITSTATUS(status)); } } return 0; }
如果需要处理大量命令,可以用哈希表存储PID到命令的映射提升查找效率,少量命令直接遍历数组完全够用。
内容的提问来源于stack exchange,提问作者happy happy
相关产品推荐
相关产品推荐

