简体   繁体   English

有没有最简单的方法在子进程中等待标志/事件更改?

[英]is there any simplest way to wait in a child process until a flag / event change?

this is my sample program but it work as flag does not affect in child process and even sem_wait , sem_trywait also wont work, apart from signals and fcntl is there any other way to wait till particular event triggered. 这是我的示例程序,但是它可以工作,因为标志不会影响子进程,甚至sem_wait,sem_trywait也不会工作,除了信号和fcntl之外,还有其他方法可以等待特定事件触发。

#include "header.h"
void main()
{
    int flag = 1;
    pid_t cpid;
    sem_t semobj;
    int value;
    cpid = fork() ;
    sem_init ( &semobj, 0, 2);
    if ( cpid == 0)
    {
        printf ("Chile Process id = %d, PPID= %d\n",getpid(),getppid());
        while ( FLAG ); // here i need to wait until parent process disables the flag
        //sem_trywait ( &semobj);
        //sem_getvalue ( &semobj, &value );
        printf ( "After Wait =%d\n", FLAG );
        sleep(10);
    }
    else
    {
        printf( "Parent Process id =%d\n",getpid() );
        sleep(3);
        printf( "Setting Flag value\n" );
        FLAG = 0; // here i need to set a flag
        sleep(7);

    }
}

I guess there's no the "simplest way". 我猜没有“最简单的方法”。

The thing is that you're dealing with two separate processes here. 问题是您在这里处理两个单独的过程 And if you want to communicate anything between the two you have to use some kind of IPC (Inter Process Communications) mechanism. 如果要在两者之间进行任何通信,则必须使用某种IPC(进程间通信)机制。 I'd suggest for you to read something like this on the subject and pick one. 我建议你阅读像这样的主题,并挑选一个。

You code doesn't work as you'd like it to because once you've fork() ed you have two address spaces each with its own set of variables (including semaphore variables). 您的代码无法按您希望的那样工作,因为一旦完成fork()您就会拥有两个地址空间,每个地址空间都具有自己的一组变量(包括信号量变量)。 So any action parent performs on those variables in its address space does not affect any of the variables in child's address space, thus no communication happens. 因此,父级对其地址空间中的那些变量执行的任何操作都不会影响子级地址空间中的任何变量,因此不会发生通信。

I recommend reading whats written in the section "Named Semaphores" on this page . 我建议阅读本页上“命名信号量”部分中写的内容。

Your problem is that you don't name your semaphores and therefore your semaphore gets copied for the child process. 您的问题是您没有给信号灯命名,因此您的信号灯会被复制到子进程中。 That means, as Igor already explained in his answer, that the semaphore variable is not the same for child and parent process. 就像Igor在他的回答中已经解释的那样,这意味着信号量变量对于子进程和父进程是不同的。

A named semaphore can be shared between two processes so your parent process would be able to signal your child process. 可以在两个进程之间共享一个已命名的信号量,因此您的父进程将能够向您的子进程发出信号。

Take a look at http://www.yendor.com/programming/unix/apue/ch8.html , the Synchronization Library section. 看一下“同步库”部分的http://www.yendor.com/programming/unix/apue/ch8.html

Basically you want the 基本上你想要

TELL_WAIT
TELL_PARENT
TELL_CHILD
WAIT_PARENT
WAIT_CHILD

functions. 职能。

The linked examples use signals to implement them, but you should also be able to implement them with pipes , shared memory + posix semaphores , or SysV semaphores . 链接的示例使用信号来实现它们,但是您也应该能够使用管道共享内存+ posix信号量SysV信号量来实现它们。

If you want to use Posix semaphores, the semaphore needs to be in shared memory (shm_open) and initialized to 0 before forking. 如果要使用Posix信号灯,则该信号灯需要位于共享内存(shm_open)中,并在分叉之前初始化为0。


Also, that is not how you use fork—take a look at my answer here (and subtract the exec part): How to start process on Linux OS in C, C++ 另外,这不是您使用fork的方式-在这里看看我的答案(并减去exec部分): 如何在C,C ++的Linux OS上启动进程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM