在C ++/Linux中,系统范围内的全局变量/信号量/互斥量? [英] System-wide global variable / semaphore / mutex in C++/Linux?

查看:253
本文介绍了在C ++/Linux中,系统范围内的全局变量/信号量/互斥量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在Linux上的C ++中创建系统范围的全局变量/信号灯/互斥锁?

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

这是原因:我有一个系统,该系统通常在不相关的数据上运行同一软件的多个副本.通常有4个作业,每个作业运行相同的软件.该软件有一小部分,在其中创建了一个占用大量内存的巨大图形.在该部分之外,内存使用量适中.

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.

有时会发生2个作业同时到达相同的内存占用区域,并且整个系统开始交换的情况.因此,我们想通过在不同作业之间创建一个临界区互斥锁来防止这种情况,这样最多只能有一个作业同时分配大量内存.

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.

如果这些是同一作业的线程,则pthread锁将完成该作业.

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

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

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

推荐答案

您可以使用

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

已命名信号量由以下形式的名称标识 /somename;也就是说,一个以Null结尾的字符串,最大为 NAME_MAX-4(即251个)字符,由首字母组成 斜杠,后跟一个或多个字符,都不是 斜线. 两个进程可以在相同的名称下运行 通过向sem_open(3) 传递相同的名称来表示信号量.

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).

这篇关于在C ++/Linux中,系统范围内的全局变量/信号量/互斥量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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