简体   繁体   English

将mutexes / semaphores与进程一起使用

[英]Using mutexes/semaphores with processes

Almost all the code and tutorials that I have read online so far involve using mutexes and semaphores for synchronisation amongst threads. 到目前为止,我在线阅读的几乎所有代码和教程都涉及使用互斥锁和信号量来实现线程之间的同步。 Can they be used to synchronise amongst processes? 它们可以用于在进程之间进行同步吗?

I'd like to write code that looks like this: 我想编写看起来像这样的代码:

void compute_and_print() {
   // acquire mutex
   // critical section
   // release mutex
}

void main() {
int pid = fork();
if ( pid == 0 ) {
  // do something
  compute_and_print();
}
else {
  // do something
  compute_and_print();
}
}
  • Could someone point me towards similar code that does this? 有人能指出我这样做的类似代码吗?
  • I understand that different processes have different address spaces, but I wonder if the above would be different address spaces, however, wouldn't the mutex refer to the same kernel object? 我知道不同的进程有不同的地址空间,但我想知道上面是不同的地址空间,但是,互斥量不会引用相同的内核对象吗?

Yes, it is possible. 对的,这是可能的。 There are many ways to synchronize different processes. 有许多方法可以同步不同的进程。 Perhaps the most popular solutions for mutual exclusion in this field are System V IPC semaphores and atomic operations on shared memory . 也许这个领域中最常用的互斥解决方案是System V IPC信号量共享内存上的原子操作 I recommend you read chapter 5 of David A Ruslin's book called Interprocess Communication Mechanisms , or better yet - the whole book. 我建议你阅读David A Ruslin的书“ Interprocess Communication Mechanisms”的第5章,或者更好的是 - 整本书。

As for your second question, most modern operating systems on commodity hardware would place processes in different address spaces, though it is also possible for processes to share the same address space (see Virtual Memory , Memory Protection ). 至于你的第二个问题,商品硬件上的大多数现代操作系统会将进程放在不同的地址空间中,尽管进程也可以共享相同的地址空间(参见虚拟内存内存保护 )。 Either way, if IPC mechanism is handled by the kernel, then two processes would refer to the same "kernel object", as you said. 无论哪种方式,如果IPC机制由内核处理,那么两个进程将引用相同的“内核对象”,如您所述。 In cases where mutual exclusion is implemented (almost) without the kernel (like spin locks of some sort that use "shared memory"), both processes would refer to the same physical memory even though their virtual addresses for that memory might be different. 如果在没有内核的情况下(几乎)实现互斥(如某种使用“共享内存”的自旋锁),则两个进程都会引用相同的物理内存,即使它们的内存虚拟地址可能不同。

Hope it helps. 希望能帮助到你。 Good Luck! 祝好运!

Just to be clear, POSIX (and linux) support two separate families of semaphores that have two different interfaces and methods of usage. 需要明确的是,POSIX(和linux)支持两个独立的信号量系列,它们具有两种不同的接口和使用方法。

There is the older SysV semaphores consisting of semget , semop , semctl and (somewhat optionally) ftok . 有较旧的SysV信号量,包括semgetsemopsemctl和(有些可选) ftok

The more modern "posix" semaphores consist of sem_open/sem_init , sem_wait , sem_post , sem_close and sem_unlink . 更现代的“posix”信号量包括sem_open/sem_initsem_waitsem_postsem_closesem_unlink

The setup/usage regimes and capabilities are different enough that it is worth familiarizing yourself with both to see what is better for your use case. 设置/使用制度和功能是不同的,值得熟悉两者,看看什么是更好的用例。

You can also use process shared mutexes from the pthreads package. 您还可以使用pthreads包中的进程共享互斥锁。

Sounds like you're looking for System V IPC You would probably use a semaphore to synchronize between processes. 听起来像是在寻找System V IPC您可能会使用信号量来在进程之间进行同步。

Here is a nice introduction to Sys V IPC in Linux 以下是Linux中Sys V IPC的一个很好的介绍

You'll want named mutex/semaphore's. 你会想要命名的互斥/信号量。 Take a look at this StackOverflow answer that describes some points. 看一下描述一些要点的StackOverflow答案 Here's an IBM describing the use of pthread_mutexattr_setname_np . 这是IBM描述pthread_mutexattr_setname_np的使用。 One thing to note that named mutex's in Linux are not 100% portable (ie it MIGHT work on Ubuntu but NOT on CentOS, etc. etc.), so you'll need to be sure your platform supports it. 有一点需要注意,Linux中的命名互斥体不是100%可移植的(即它可以在Ubuntu上工作,但不能在CentOS上工作等等),所以你需要确保你的平台支持它。 If a named mutex is not available on your system, you could use named pipes with some wait conditions or even local sockets. 如果系统上没有命名的互斥锁,则可以使用带有一些等待条件的命名管道甚至本地套接字。 As another answer points out is SysV IPC. 另一个答案指出是SysV IPC。

The biggest question you'll need to answer first is how 'cross-linux-platform' compatible do you want your app to be. 您首先需要回答的最大问题是,您希望应用程序与“跨Linux平台”兼容。

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

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