实在是不知道怎么写回调函数的形参,报错是看懂了,解决方案想不出来
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> void process_a() { printf ( "process A is create.The Child process < %d > running...\n" , getpid()); sleep(2); exit (EXIT_SUCCESS); } void process_b() { printf ( "process B is create.The Child process < %d > running...\n" , getpid()); sleep(5); exit (EXIT_SUCCESS); } void create_process( void (*cp_funcp)(), void (*pp_funcp)()) { if (NULL == cp_funcp) return ; pid_t cpid = 0; cpid=fork(); if (cpid == -1) { perror ( "[Error]: process create faile " ); exit (EXIT_FAILURE); } else if (cpid == 0) // 子进程执行任务 { cp_funcp(); } else if (cpid > 0) // 父进程执行任务 { pid_t rpid = 0; int status = 0; if (NULL != pp_funcp) pp_funcp(); rpid = wait(&status); printf ( "The Child Process < %d > has exited,exit code < %d >.\n" ,rpid,WEXITSTATUS(status)); } return ; } int main( int argc, const char *argv[]) { create_process(process_a, create_process(process_b, NULL)); return 0; } |
提示错误:
1 2 | main.c:57:28: error: invalid use of void expression create_process(process_a, create_process(process_b, NULL)); |
只创建一个进程是可以的,只需要写create_process(process_b, NULL);即可
但是创建2个进程不行
37
收起
正在回答 回答被采纳积分+1
4回答
慕小白0101
2023-05-13 21:25:24
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | else if (cpid > 0) // 父进程执行任务 { pid_t rpid = 0; int status = 0; index+=1; if (NULL != processlist[index]) { create_process(processlist, index); } printf ( "wait a moment...\n" ); // 非阻塞继续执行父进程代码 if ((rpid = waitpid(-1,&status,WNOHANG)) == 0) { printf ( "waiting...\n" ); // 若子进程结束调用,该行代码不执行 } // 阻塞等待子进程, 父进程休眠 // 由父进程等待进程进程销毁回收资源 rpid = wait(&status); printf ( "The Child Process < %d > has exited,exit code < %d >.\n" ,rpid,WEXITSTATUS(status)); } |
无名老师,请问为什么这样写会导致“wait a moment..”.和"waiting..."每次都在上一个进程退出时才打印出来
顺序不对,a进程退出才打印“wait a moment..”.和"waiting..."
慕小白0101
2023-05-13 17:52:54
好像完成了这个功能,请老师看看这个代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> void process_a() { printf ( "process A is create.The Child process < %d > running...\n" , getpid()); sleep(2); exit (EXIT_SUCCESS); } void process_b() { printf ( "process B is create.The Child process < %d > running...\n" , getpid()); sleep(5); exit (EXIT_SUCCESS); } void process_c() { printf ( "process C is create.The Child process < %d > running...\n" , getpid()); sleep(5); exit (EXIT_SUCCESS); } void create_process( void (*processlist[4])(), int index) { if (NULL == processlist[index]) { printf ( "processlist[%d] is NULL.\n" , index); return ; } pid_t cpid = 0; cpid=fork(); if (cpid == -1) { perror ( "[Error]: process create faile " ); exit (EXIT_FAILURE); } else if (cpid == 0) // 子进程执行任务 { processlist[index](); } else if (cpid > 0) // 父进程执行任务 { pid_t rpid = 0; int status = 0; if (NULL != processlist[index]) { index+=1; create_process(processlist, index); } rpid = wait(&status); printf ( "The Child Process < %d > has exited,exit code < %d >.\n" ,rpid,WEXITSTATUS(status)); } return ; } int main( int argc, const char *argv[]) { void (*processlist[4])( void )={process_a, process_b, process_c, NULL}; create_process(processlist, 0); return 0; } |
慕小白0101
2023-05-13 16:31:04
改了一下,现在能创建两个进程了。那假如我要创建3个,4个,或者n个进程呢?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | void create_process( void (*cp_funcp)(), void (*pp_funcp)()) { if (NULL == cp_funcp) return ; pid_t cpid = 0; cpid=fork(); if (cpid == -1) { perror ( "[Error]: process create faile " ); exit (EXIT_FAILURE); } else if (cpid == 0) // 子进程执行任务 { cp_funcp(); } else if (cpid > 0) // 父进程执行任务 { pid_t rpid = 0; int status = 0; if (NULL != pp_funcp) create_process(pp_funcp, NULL); rpid = wait(&status); printf ( "The Child Process < %d > has exited,exit code < %d >.\n" ,rpid,WEXITSTATUS(status)); } return ; } int main( int argc, const char *argv[]) { create_process(process_a, process_b); return 0; } |
物联网/嵌入式工程师
- 参与学习 394 人
- 提交作业 23566 份
- 解答问题 1207 个
行业热门,政策风口,人才缺口极大,现在入场时机正好! 上千人检验,数轮迭代的硬核知识体系,软硬件通吃 保姆式教学+简历指导+1V1模拟面试+3次内推,助力轻松就业!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧