实在是不知道怎么写回调函数的形参,报错是看懂了,解决方案想不出来

实在是不知道怎么写回调函数的形参,报错是看懂了,解决方案想不出来

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个进程不行

正在回答 回答被采纳积分+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..."

https://img1.sycdn.imooc.com//climg/645f8fc10910fdef07320354.jpg

  • 提问者 慕小白0101 #1

    从创建第二个子进程开始,只有等a进程退出,打印了“The Child Process < 3400 > has exited,exit code < 0 >.”这句话才接着打印父进程的“wait a moment..”.和"waiting..."

    2023-05-13 21:32:16
  • 提问者 慕小白0101 #2

    而sleep函数则是用来让当前进程暂停执行指定的时间(以秒为单位),使得CPU可以去执行其它进程。在调用sleep时,进程会进入睡眠状态,直到指定的时间到达才会被唤醒继续执行。sleep函数适用于需要在指定的时间后执行某个操作的场景。


    我去,父进程因为sleep休眠了

    2023-05-13 21:36:23
  • 提问者 慕小白0101 #3

    那看来只能用alarm了

    2023-05-13 21:36:50
慕小白0101 提问者 2023-05-13 18:42:13
1
void (*processlist[4])(void)={process_a, process_b, process_c, NULL};


数组void (*processlist[4])(void),数组的形参好像填不填都没区别,把void 改 int也没报错

慕小白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 #1

    老师,我像问一下函数指针数组,每个数组元素,他的形参每个成员都要一样吗。

    比如,void (*processlist[4])(void)={process_a, process_b, process_c, NULL};

    假如void process_d(int a),void process_e(int a, int b),请问这两个函数能放进指针函数里吗

    2023-05-13 18:01:00
  • 提问者 慕小白0101 #2

    上面打错字了。。


    老师,我像问一下函数指针数组,每个数组元素,他的形参每个成员都要一样吗。

    比如,void (*processlist[4])(void)={process_a, process_b, process_c, NULL};

    假如void process_d(int a),void process_e(int a, int b),请问这两个函数能放进函数指针数组里吗


    2023-05-13 18:01:49
  • 无__名 回复 提问者 慕小白0101 #3

    每个数组元素对应的函数的参数个数和形参个数都是需要与函数指针定义的形式保持一致

    2023-05-13 19:35:34
慕小白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;
}


  • 提问者 慕小白0101 #1

    创建n个进程,需要一个函数指针数组,例如processlist[]={process_a,process_b,process_c...}。

    然后while循环遍历函数指针数组,知道NULL为止


    剩下的问题就是怎么递归create_process()函数了

    2023-05-13 16:36:27
  • 无__名 回复 提问者 慕小白0101 #2

    你的方案是可行的,有了递归,其实并不需要在使用 while 循环了, 只需要在子进程处理函数最后需要调用 exit 函数退出,不能让子进程在去做循环或者递归就可以了,至于进程处理函数的参数后续可以通过进程间通讯完成,

    2023-05-13 19:59:28
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码