如何使用从子命名信号量 [英] How to use named semaphore from child

查看:209
本文介绍了如何使用从子命名信号量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以基本上我想暂停了一下孩子的过程是创造后,只是让家长prepare在共享内存为它的一些数据。我试图用一个信号,如下建议:<一href=\"http://stackoverflow.com/questions/8359322/how-to-share-semaphores-between-processes-using-shared-memory\">How要使用共享内存进程之间共享信号灯。

问题NR 1 :孩子不能打开信号灯

问题NR 2 :字符串错误返回一个int,而人显然strerror的说,它返回的char *

要避免出现您尝试过哪些

  SEM = sem_open(/信号,O_CREAT,0644,0);对于(i = 0; I&LT;民;我++)
{
    PID =叉();    如果(PID == 0)
    {
        SEM = sem_open(/旗语,0);
        如果(SEM == SEM_FAILED)
        {
            的printf(错误:%d个\\ N,字符串错误(错误));
            出口(0);
        }
        sem_wait(SEM);
        出口(0);
    }
}prepare_data_for_child_processes();对于(i = 0; I&LT; mpi_comm_world-&GT; NP;我++)
    sem_post(SEM);sem_close(SEM);
sem_unlink(/信号量);


解决方案

您不需要孩子叫 sem_open()在所有 - 他们可以简单地 sem_wait()他们继承 sem_t 处理。

您可能想信号灯限制只是你的工作的船员。在这种情况下,家长应打开权限受限制的信号只(O_EXCL),然后断开链接它的时候了。这将prevent诚实的错误损坏您信号灯的状态(但不会防止恶意程序):

  ...
SEM = sem_open(/旗语,O_CREAT | O_EXCL,0644,0); / *注意* O_EXCL /
sem_unlink(/信号量);对于(i = 0; I&LT;民;我++){
  如果(叉()== 0){
    sem_wait(SEM);
    /* 做一些工作 */
    出口(0);
  }
}prepare_data_for_child_processes();对于(i = 0; I&LT; mpi_comm_world-&GT; NP;我++)
    sem_post(SEM);sem_close(SEM);

现在,如果你的实现支持它,您应该 sem_init(1,0)共享内存。这会给你只限于你的工作的船员一个真正的匿名的信号。

(是的,问题#2是一​​个缺少包括。)

So basically I want to suspend a bit the child process after it's creation, just so the parent prepare some data for it in a shared memory. I'm trying to use a semaphore, as suggested here: How to share semaphores between processes using shared memory.

Problem nr 1: the child can't open the semaphore.

Problem nr 2: strerror returns an int, but man strerror clearly says it returns an char *.

To avoid "what have you tried":

sem = sem_open("/semaphore", O_CREAT, 0644, 0);    

for (i = 0; i < num; i++)                                        
{   
    pid = fork();                                                               

    if (pid == 0)                                                               
    {   
        sem = sem_open("/semaphore", 0);                                        
        if (sem == SEM_FAILED)                                                  
        {   
            printf( "Error : %d\n", strerror(errno ));
            exit(0);                                                            
        }
        sem_wait(sem);   
        exit(0);                                                                
    }                                                                                                                       
}

prepare_data_for_child_processes();

for (i = 0; i < mpi_comm_world->np; i++)
    sem_post(sem);

sem_close(sem);
sem_unlink("/semaphore");

解决方案

You don't need the children to call sem_open() at all — they can simply sem_wait() on their inherited sem_t handle.

You probably want to restrict the semaphore to just your "work crew". In that case, the parent should open the semaphore exclusively (O_EXCL) with restrictive permissions, and then unlink it right away. This will prevent honest mistakes from corrupting your semaphore's state (but won't protect against hostile programs):

...
sem = sem_open("/semaphore", O_CREAT|O_EXCL, 0644, 0); /* Note O_EXCL */
sem_unlink("/semaphore");

for (i = 0; i < num; i++) {
  if (fork() == 0) {
    sem_wait(sem);
    /* Do some work */
    exit(0);
  }
}

prepare_data_for_child_processes();

for (i = 0; i < mpi_comm_world->np; i++)
    sem_post(sem);

sem_close(sem);

Now, if your implementation supports it, you should instead sem_init(1, 0) in shared memory. That will give you a truly anonymous semaphore restricted just to your work crew.

(And, yes, problem #2 is a missing include.)

这篇关于如何使用从子命名信号量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆