简体   繁体   English

使用信号量的for循环在进程之间进行同步

[英]Synchronization between processes with for loops using semaphores

Context: 内容:

  • I am trying to have two processes writing to the same array in the shared memory space. 我试图让两个进程写入共享内存空间中的同一数组。

  • Each process will write half of the array with a for loop. 每个进程将使用for循环写入数组的一半。

  • The first element of the array will always store the index of the next element to be written. 数组的第一个元素将始终存储要写入的下一个元素的索引。

  • IPC is done through a semaphore in the shared memory. IPC通过共享内存中的信号量完成。

Preconditions: 前提条件:

  1. Both the array and semaphore are properly set up in the shared memory. 数组和信号量均已在共享内存中正确设置。
  2. The programme works fine if I wait sem_wait and sem_post outside of the for loop, meaning make the whole process atomic. 如果我在for循环之外等待sem_wait和sem_post,程序运行正常,这意味着使整个过程原子化。 (This is also the reason why I believe the semaphore and array has been set up properly) (这也是我认为信号量和数组已正确设置的原因)

Problem 问题

However, when I try to reduce the critical region by putting sem_wait and sem_post into the for loop. 但是,当我尝试通过将sem_wait和sem_post放入for循环来减小关键区域时。 It is not sync as there are part of the array which are not written. 由于数组的某些部分未写入,因此无法同步。 But the two processes finished their loops where the total loop counts should be equal to the array length. 但是这两个进程完成了它们的循环,其中循环总数应等于数组长度。

Many thanks to suggestions why this happens??? 非常感谢建议为什么会发生这种情况???


UPDATE 更新

On OS X, sem_init() is not working as expected. 在OS X上,sem_init()无法正常工作。 Used sem_open() to solve the problem. 使用sem_open()解决此问题。 Reference: http://lists.apple.com/archives/darwin-dev//2008/Oct/msg00044.html 参考: http : //lists.apple.com/archives/darwin-dev//2008/Oct/msg00044.html

In psuedo code for problem case, the parent thread requires semaphore as soon as it is released by itself. 在针对问题情况的伪代码中,父线程在自己释放后就需要信号量。 Now since, the semaphore is available, parent will go on executing. 现在,由于信号灯可用,因此父级将继续执行。 When its time slice expires, the kernel may switch to child process, but it is waiting for semaphore. 当其时间片到期时,内核可能会切换到子进程,但它正在等待信号量。 The semaphore is acquired by parent process. 信号量是通过父进程获取的。 So again parent will continue to execute. 因此,父级将继续执行。

In this scenario, parent executed twice and child did not execute. 在这种情况下,父执行两次,子执行不。

This might have resulted in out of sync execution. 这可能导致不同步执行。

Looking at your actual code I think the problem is with how you are creating your semaphore. 查看您的实际代码,我认为问题在于您如何创建信号量。 If you read the man page on sem_init: 如果您在sem_init上阅读了手册页:

If pshared is nonzero, then the semaphore is shared between processes, and should be located in a region of shared memory (see shm_open(3), mmap(2), and shmget(2)). 如果pshared为非零,则信号量在进程之间共享, 并且应位于共享内存的区域中 (请参见shm_open(3),mmap(2)和shmget(2))。 (Since a child created by fork(2) inherits its parent's memory mappings, it can also access the semaphore.) Any process that can access the shared memory region can operate on the semaphore using sem_post(3), sem_wait(3), and so on. (由于fork(2)创建的子级继承了其父级的内存映射,因此它也可以访问该信号量。)任何可以访问共享内存区域的进程都可以使用sem_post(3),sem_wait(3)和该信号量进行操作。以此类推。

The key here is the text in bold - your semaphore isn't in shared memory so it's not really shared between the two processes hence the contention you are seeing. 这里的关键是粗体文本-您的信号量不在共享内存中,因此在两个进程之间并未真正共享,因此您正在看到争用。

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

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