0

Suppose I have a parent that forks a child. The forked child then uses exec() to change it's process image. Both child and parent are now doing active data exchange using a pipe. I want to synchonize this data exchange using a semaphore. From what I understand I will create the semaphore in the parent before fork. However will the child, after exec() be able to access this semaphore? If not, is there some other way I can use a semaphore to establish synchonization?

4

1 に答える 1

1

exec の man ページから:
[SEM] Any named semaphores open in the calling process shall be closed as if by appropriate calls to sem_close().
したがって、exec を呼び出した後、親で開いているセメフォにアクセスできません。

もちろん、それについてまったく心配する必要はありません。2 つのプロセスでセマフォを共有したい場合は、posix 名前付きセマフォを使用するだけです。

アイデアは単純です。プロセス内にセマフォを作成し、それに名前を付けます。他のプロセスは名前を持っているだけで、そのセマフォを開くことができます。

于 2013-04-01T18:14:48.917 に答える