在共享Linux的多个独立的程序之间相同的变量 [英] Sharing same variable between more than one independent programs in Linux

查看:193
本文介绍了在共享Linux的多个独立的程序之间相同的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想分享在Linux中不止一个无关的C可执行程序之间的变量。也就是说,程序会写一个数组,并设置一个标志,以便没有任何其他程序可以使用它,而这个操作后,它会取消设置标志,然后另一个程序将读取该数组。我尝试使用相同的自定义头文件(包含变量)的每一个程序,但它似乎被调用程序时创建的变量的不同实例。

I want to share a variable between more than one independent C executables in Linux. That is, a program will write on an array and set a flag so that no other program can use it, and after this operation it'll unset the flag and then another program will read the array. I tried using the same custom header file (containing the variable) in every program, but it seems different instances of the variables are created when the programs are invoked.

推荐答案

你在头文件中声明的变量将产生一个副本在任何你有他们(的除非你声明它们的extern )。当然,独立的进程打交道时,每个进程都有其自己的内存空间。你需要使用更复杂的技术来规避这一点,即进程间通信(IPC)。例如:

Variables you declare in your headers will generate a copy where ever you include them (unless you declare them extern). Of course, when dealing with separate processes every process will have its own memory space. You need to use more sophisticated techniques to circumvent this, i.e. Inter Process Communication (IPC). For example:


  • (命名)管


  • 共享内存

您的问题读起来就像共享内存是你想要的,因为在此多个进程可以访问同样的内存区域共享某些变量。请参见这里的一个例子。也看看这个问题及其答案。

Your question reads like shared memory is what you want, since hereby multiple processes can access the same memory regions to share some variables. See here for an example. Also take a look at this question and its answers.

您的程序将需要创建一些共享内存,如使用 shmget的和附加使用的的shmat
当多个进程访问相同的内存区域,它总是一个健康的方式来读取过程中添加进程同步/上写的变量,例如,使用共享信号灯(了semget ,的执行semop )。

Your program would be required to create some shared memory, e.g. using shmget and to attach the shared memory object using shmat. When multiple processes access same memory regions, it's always a healthy approach to add process synchronization during read/write on the variable, e.g. using a shared semaphore (semget, semop).

当你与你共享内存做了你需要分离( shmdt )从它。因此你告诉你的程序不再需要访问它的内核。
创建共享内存/信号灯对象的过程也需要在你的程序(S)的最后消灭他们。否则,将驻留在内存中,大概直到你重新启动机器(请参见了shmctl ,<一个HREF =htt​​p://linux.die.net/man/2/semctl相对=nofollow>了semctl ,尤其是 IPC_RMID )。

When you are done with your shared memory you need to detach (shmdt) from it. Thereby you tell the kernel that your process no longer needs access to it. The process that created the shared memory/semaphore object also needs to destroy them at the end of your program(s). Otherwise it will reside in memory, probably until you reboot your machine (see shmctl, semctl, especially IPC_RMID).

需要注意的是共享内存对象的段只能实际销毁的最后一个过程分离后的。所以,你要确保,这实际发生的所有进程( shmdt

Note that for shared memory objects "The segment will only actually be destroyed after the last process detaches it". So you want to make sure, that this actually happens for all of your processes (shmdt).

在回应的意见,这里是POSIX的做法:

In response to the comments, here is the POSIX approach:

System V共享存储器(shmget的(2),shmop(2)等),是一个较旧的共享存储器的API。 POSIX共享内存提供更简单,更好的设计的界面;在另一方面POSIX共享内存是多少比System V共享内存更少的广泛使用(尤其是在旧系统)。

System V shared memory (shmget(2), shmop(2), etc.) is an older shared memory API. POSIX shared memory provides a simpler, and better designed interface; on the other hand POSIX shared memory is somewhat less widely available (especially on older systems) than System V shared memory.

  • shm_open - to get shared memory (via file descriptor)
  • ftruncate - to set the size of the shared memory
  • mmap - to get a pointer to the memory
  • sem_open - to get a semaphore
  • sem_wait, sem_post - for your read/write synchronisation
  • shm_unlink, sem_close - to clean up after all
  • 另请参阅本概述并的这里的例子。

    最后,要注意

    POSIX共享内存对象有内核的持久性:共享内存对象会一直存在,直到系统被关闭,或者直到所有进程都映射的对象,它已经与shm_unlink删除(3)

    POSIX shared memory objects have kernel persistence: a shared memory object will exist until the system is shut down, or until all processes have unmapped the object and it has been deleted with shm_unlink(3)

    为了考虑到共享内存对象的持久性,不要忘了信号处理程序添加到您的应用程序将在一个特殊的终止的情况下(SIGINT,SIGTERM等)。执行清理操作


    In order to take into account the persistence of the shared memory objects, don't forget to add signal handlers to your application that will perform the clean up operations in case of an exceptional termination (SIGINT, SIGTERM etc.).

    这篇关于在共享Linux的多个独立的程序之间相同的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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