简体   繁体   English

信号量可用于访问同一库的两个不同应用程序

[英]can semaphores be used for two different applications accessing the same library

i have two different applications accessing the same code in the shared library in Linux. 我有两个不同的应用程序在Linux的共享库中访问相同的代码。

Case1:- when first application access the code in the library, it acquires lock. 情况1:-当第一个应用程序访问库中的代码时,它将获得锁。 But when the second application access the same code, how does it know that lock is already applied because another separate copy of code will be created for the second application. 但是,当第二个应用程序访问相同的代码时,它将如何知道已经应用了锁定,因为将为第二个应用程序创建另一个单独的代码副本。

Semaphore is a kernel variable. Semaphore是一个内核变量。

A useful way to think of a semaphore is as a record of how many units of a particular resource are available, coupled with operations to safely (ie, without race conditions) adjust that record as units are required or become free, and, if necessary, wait until a unit of the resource becomes available link 考虑信号量的一种有用方法是记录特定资源有多少个单位,并结合操作以安全地(即,在没有竞争条件的情况下)根据需要或释放单位来调整该记录,并在必要时进行调整,等到资源的一部分变为可用 链接

so when you lock your semaphore its value will be 0(zero) which shows now its unavailable. 因此,当您锁定semaphore其值将为0(零),这表明其不可用。 So when other processes try to access that shared region due to lock the shared region will not be available. 因此,当其他进程由于锁定而尝试访问该共享区域时,该共享区域将不可用。 And when semaphore is unlocked the value will be 1(one) which means now its available. 并且当信号量解锁时,该值将为1(one),这意味着它现在可用。

since its a kernel variable we use it for synchronization 由于它是一个内核变量,我们将其用于同步

There are no methods for accessing the value of semaphore we rely on Semaphore's invariant to define its behavior. 没有任何方法可以访问信号量的值,我们依靠信号量的不变量来定义其行为。

On Unix-based systems, the code segment (.text) may be shared among multiple processes because it's immutable. 在基于Unix的系统上,代码段(.text)不可更改,因此可以在多个进程之间共享。 The code for the shared library is mapped into memory by the operating system. 共享库的代码由操作系统映射到内存中。

Basically, each shared library that contains static data (such as global variables) has a Global Offset Table GOT . 基本上,每个包含静态数据(例如全局变量)的共享库都有一个Global Offset Table GOT On shared libraries, all references to static data (global) occur via. 在共享库上,对静态数据(全局)的所有引用都通过发生。 So even if the code segment is shared among multiple processes, each process has its exclusive mapping of other segments of the shared library, including the respective Global Offset Table, whose entries are relocated accordingly. 因此,即使代码段在多个进程之间共享,每个进程也会对共享库的其他段(包括其各自的全局偏移表)进行独占映射,其条目会相应地重定位。 The complexity is resolved by CPU's memory management unit. 复杂度由CPU的内存管理单元解决。

You can go through this link Dynamic Linking and Loading memory management 您可以通过此链接进行动态链接和加载 内存管理

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

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