【问题标题】:C How to share informations between processes?C 如何在进程之间共享信息?
【发布时间】:2019-11-15 05:10:36
【问题描述】:

我需要一些 C 代码方面的帮助。 基本上我有n 执行一些代码的进程。一旦几乎完成,我希望“管理器进程”(这是主要功能)向每个n 进程发送一个int 变量,每个进程可能不同。 我的想法是在所有进程开始后signal(handler_function, SIGALRM)。当进程几乎完成时,它使用kill(getpid(), SIGSTOP) 以等待Manager Process。 在SIM_TIME 秒过去后,handler_functionMessage Queue 上发送int 变量,然后使用kill(process_pid, SIGCONT) 来唤醒等待进程。这些进程在被唤醒后应该​​从Message Queue 接收到int 变量,打印它并简单地终止,让Manager Process 再次获得控制权。

这里有一些代码:

 /**
 * Child Process creation using fork() system call
 * Parent Process allocates and initializes necessary variables in shared memory
 * Child Process executes Student Process code defined in childProcess function
 */
pid_t runChild(int index, int (*func)(int index))
{
    pid_t pid;
    pid = fork();
    if (pid == -1)
    {
        printf(RED "Fork ERROR!\n" RESET);
        exit(EXIT_FAILURE);
    }
    else if (pid == 0)
    {
        int res = func(index);
        return getpid();
    }
    else
    {
        /*INSIGNIFICANT CODE*/
        currentStudent = createStudent(pid);
        currentStudent->status = FREE;
        students[index] = *currentStudent;
        currentGroup = createGroup(index);
        addMember(currentStudent, currentGroup);
        currentGroup->closed = FALSE;
        groups[index] = *currentGroup;
        return pid;
    }
}

每个进程执行的代码

 /**
     * Student Process Code
     * Each Student executes this code
     */
    int childProcess(int index)
    {
     /*NOTICE: showing only relevant part of code*/
        printf("Process Index %d has almost done, waiting for manager!\n",          index);
    /* PROGRAM GETS STUCK HERE!*/
    kill(getpid(), SIGSTOP);
    /* mex variable is already defines, it's a struct implementing Message Queue message struct*/
    receiveMessage(mexId, mex, getpid());
    printf(GREEN "Student %d has received variable %d\n" RESET,  getpid(), mex->variable);
}

处理函数:

* Handler function
 * Will be launched when SIM_TIME is reached
 */
void end_handler(int sig)
{
    if (sig == SIGALRM)
    {
        usleep(150000);
        printf(RED "Time's UP!\n" RESET);
        printGroups();
        for(int i = 0; i < POP_SIZE; i++){
            mex->mtype = childPids[i];
            mex->variable = generateInt(18, 30);
            sendMessage(mexId, mex);
            //childPids is an array containing PIDs of all previously launched processes
            kill(childPids[i], SIGCONT);
        }
}

我希望我的代码是可以理解的。 我有一个问题,使用提供的代码,整个程序卡在kill(getpid(), SIGSTOP) 系统调用。 我还尝试在终端中启动ps,但未检测到活动进程。 我认为handler_function 出于某种原因没有发送kill(childPids[i], SIGCONT) 系统调用。 知道如何解决这个问题吗? 谢谢

【问题讨论】:

  • 父级不能初始化一个大小为n的int数组(每个子进程一个位置)发送整数,然后每个子进程从这个数组中获取值吗?
  • 我的目标是确保管理器进程可以与任何单个进程通信。您建议的解决方案无济于事。

标签: c fork message-queue


【解决方案1】:

您可能希望从阅读 mq_overview 的手册页开始(man mq_overview)。它在进程之间提供了一种可移植且灵活的通信机制,允许同步和异步机制进行通信。

在您的方法中,存在“一个进程如何知道另一个进程是否在等待”的普遍问题。如果进程没有自行停止,则忽略 SIGCONT,当它随后暂停自己时,没有人会继续它。

相比之下,两者之间基于消息的交流可以被视为一种小语言。对于简单的交换(例如您的),可以很容易地手动检查语法的完整性。对于更复杂的情况,可以构建状态机甚至嵌套状态机来分析它们的行为。

【讨论】:

    猜你喜欢
    • 2012-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-11
    • 1970-01-01
    • 2011-06-10
    • 1970-01-01
    相关资源
    最近更新 更多