C ++ / Linux中的系统范围的全局变量/信号量/互斥量?

时间:2021-05-06 17:42:28

Is it possible to create a system-wide global variable / semaphore / mutex in C++ on Linux?

是否有可能在Linux上用C ++创建系统范围的全局变量/信号量/互斥量?

Here's the reason: I've got a system that often runs multiple copies of the same software on unrelated data. It's common to have 4 jobs, each running the same software. The software has a small section where it creates a huge graph that takes a lot of memory; outside that section memory usage is moderate.

原因如下:我有一个系统经常在不相关的数据上运行同一软件的多个副本。通常有4个作业,每个作业运行相同的软件。该软件有一个小部分,它创建一个占用大量内存的巨大图形;在该部分以外的内存使用量适中。

It so happens sometimes that 2 jobs simultaneously hit the same memory-hungry section and the whole system starts swapping. Thus we want to prevent that by creating something like a critical section mutex between different jobs so that no more than one of them would allocate a lot of memory at a time.

有时会发生两个作业同时遇到同一个内存需求的部分,整个系统开始交换。因此,我们希望通过在不同作业之间创建类似于临界区互斥体的东西来防止这种情况,这样它们中只有一个会一次分配大量内存。

If these were thread of the same job pthread locks would do the job.

如果这些是相同工作的线程pthread锁将完成工作。

What would be a good way to implement such mutex between different jobs?

在不同的工作之间实现这种互斥量的好方法是什么?

3 个解决方案

#1


5  

You can use a named semaphore if you can get all the processes to agree on a common name.

如果您可以使所有进程同意公用名,则可以使用命名信号量。

A named semaphore is identified by a name of the form /somename; that is, a null-terminated string of up to NAME_MAX-4 (i.e., 251) characters consisting of an initial slash, followed by one or more characters, none of which are slashes. Two processes can operate on the same named semaphore by passing the same name to sem_open(3).

命名信号量由表单名称/ somename标识;也就是说,一个以空字符结尾的字符串,最多包含NAME_MAX-4(即251个)字符,由一个初始斜杠组成,后跟一个或多个字符,其中没有一个是斜杠。通过将相同的名称传递给sem_open(3),两个进程可以对同一个命名信号量进行操作。

#2


3  

For interprocess mutual exclusion, you can use file locking. With linux, the code is as simple as protecting the critical section with a call to flock.

对于进程间互斥,您可以使用文件锁定。使用linux,代码就像通过调用flock来保护关键部分一样简单。

int fd_lock = open(LOCK_FILE, O_CREAT);

flock(fd_lock, LOCK_EX);

// do stuff

flock(fd_lock, LOCK_UN);

If you need POSIX compatibility, you can use fcntl.

如果需要POSIX兼容性,可以使用fcntl。

#3


0  

Mutual exclusion locks (mutexes) prevent multiple threads from simultaneously executing critical sections of code that access shared data (that is, mutexes are used to serialize the execution of threads). All mutexes must be global. A successful call for a mutex lock by way of mutex_lock() will cause another thread that is also trying to lock the same mutex to block until the owner thread unlocks it by way of mutex_unlock(). Threads within the same process or within other processes can share mutexes.

互斥锁(互斥锁)可防止多个线程同时执行访问共享数据的关键代码段(即,互斥锁用于序列化线程的执行)。所有互斥锁都必须是全局的。通过mutex_lock()成功调用互斥锁将导致另一个线程也试图锁定相同的互斥锁,直到所有者线程通过mutex_unlock()解锁它。同一进程内或其他进程内的线程可以共享互斥锁。

Mutexes can synchronize threads within the same process or in other processes. Mutexes can be used to synchronize threads between processes if the mutexes are allocated in writable memory and shared among the cooperating processes (see mmap(2)), and have been initialized for this task.

互斥锁可以在同一进程或其他进程中同步线程。如果互斥锁在可写内存中分配并在协作进程之间共享(请参阅mmap(2)),并且已针对此任务进行了初始化,则互斥锁可用于在进程之间同步线程。

For inter-process synchronization, a mutex needs to be allocated in memory shared between these processes. Since the memory for such a mutex must be allocated dynamically, the mutex needs to be explicitly initialized using mutex_init(). also, for inter-process synchronization, besides the requirement to be allocated in shared memory, the mutexes must also use the attribute PTHREAD_PROCESS_SHARED, otherwise accessing the mutex from another process than its creator results in undefined behaviour (see this: linux.die.net/man/3/pthread_mutexattr_setpshared): "The process-shared attribute is set to PTHREAD_PROCESS_SHARED to permit a mutex to be operated upon by any thread that has access to the memory where the mutex is allocated, even if the mutex is allocated in memory that is shared by multiple processes."

对于进程间同步,需要在这些进程之间共享的内存中分配互斥锁。由于必须动态分配这种互斥锁的内存,因此需要使用mutex_init()显式初始化互斥锁。此外,对于进程间同步,除了要在共享内存中分配的要求外,互斥锁还必须使用属性PTHREAD_PROCESS_SHARED,否则从其创建者之外的其他进程访问互斥锁会导致未定义的行为(请参阅:linux.die.net / man / 3 / pthread_mutexattr_setpshared):“进程共享属性设置为PTHREAD_PROCESS_SHARED,以允许任何可以访问分配互斥锁的内存的线程对互斥锁进行操作,即使互斥锁在内存中分配了由多个进程共享。“

#1


5  

You can use a named semaphore if you can get all the processes to agree on a common name.

如果您可以使所有进程同意公用名,则可以使用命名信号量。

A named semaphore is identified by a name of the form /somename; that is, a null-terminated string of up to NAME_MAX-4 (i.e., 251) characters consisting of an initial slash, followed by one or more characters, none of which are slashes. Two processes can operate on the same named semaphore by passing the same name to sem_open(3).

命名信号量由表单名称/ somename标识;也就是说,一个以空字符结尾的字符串,最多包含NAME_MAX-4(即251个)字符,由一个初始斜杠组成,后跟一个或多个字符,其中没有一个是斜杠。通过将相同的名称传递给sem_open(3),两个进程可以对同一个命名信号量进行操作。

#2


3  

For interprocess mutual exclusion, you can use file locking. With linux, the code is as simple as protecting the critical section with a call to flock.

对于进程间互斥,您可以使用文件锁定。使用linux,代码就像通过调用flock来保护关键部分一样简单。

int fd_lock = open(LOCK_FILE, O_CREAT);

flock(fd_lock, LOCK_EX);

// do stuff

flock(fd_lock, LOCK_UN);

If you need POSIX compatibility, you can use fcntl.

如果需要POSIX兼容性,可以使用fcntl。

#3


0  

Mutual exclusion locks (mutexes) prevent multiple threads from simultaneously executing critical sections of code that access shared data (that is, mutexes are used to serialize the execution of threads). All mutexes must be global. A successful call for a mutex lock by way of mutex_lock() will cause another thread that is also trying to lock the same mutex to block until the owner thread unlocks it by way of mutex_unlock(). Threads within the same process or within other processes can share mutexes.

互斥锁(互斥锁)可防止多个线程同时执行访问共享数据的关键代码段(即,互斥锁用于序列化线程的执行)。所有互斥锁都必须是全局的。通过mutex_lock()成功调用互斥锁将导致另一个线程也试图锁定相同的互斥锁,直到所有者线程通过mutex_unlock()解锁它。同一进程内或其他进程内的线程可以共享互斥锁。

Mutexes can synchronize threads within the same process or in other processes. Mutexes can be used to synchronize threads between processes if the mutexes are allocated in writable memory and shared among the cooperating processes (see mmap(2)), and have been initialized for this task.

互斥锁可以在同一进程或其他进程中同步线程。如果互斥锁在可写内存中分配并在协作进程之间共享(请参阅mmap(2)),并且已针对此任务进行了初始化,则互斥锁可用于在进程之间同步线程。

For inter-process synchronization, a mutex needs to be allocated in memory shared between these processes. Since the memory for such a mutex must be allocated dynamically, the mutex needs to be explicitly initialized using mutex_init(). also, for inter-process synchronization, besides the requirement to be allocated in shared memory, the mutexes must also use the attribute PTHREAD_PROCESS_SHARED, otherwise accessing the mutex from another process than its creator results in undefined behaviour (see this: linux.die.net/man/3/pthread_mutexattr_setpshared): "The process-shared attribute is set to PTHREAD_PROCESS_SHARED to permit a mutex to be operated upon by any thread that has access to the memory where the mutex is allocated, even if the mutex is allocated in memory that is shared by multiple processes."

对于进程间同步,需要在这些进程之间共享的内存中分配互斥锁。由于必须动态分配这种互斥锁的内存,因此需要使用mutex_init()显式初始化互斥锁。此外,对于进程间同步,除了要在共享内存中分配的要求外,互斥锁还必须使用属性PTHREAD_PROCESS_SHARED,否则从其创建者之外的其他进程访问互斥锁会导致未定义的行为(请参阅:linux.die.net / man / 3 / pthread_mutexattr_setpshared):“进程共享属性设置为PTHREAD_PROCESS_SHARED,以允许任何可以访问分配互斥锁的内存的线程对互斥锁进行操作,即使互斥锁在内存中分配了由多个进程共享。“