C / C ++如何判断一个程序已经运行? [英] C/C++ How to tell if a program is already running?

查看:332
本文介绍了C / C ++如何判断一个程序已经运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows环境中,我不希望在同一时间运行我的程序的两个实例。


  

相关


  
  

<一个href=\"http://stackoverflow.com/questions/646480/is-using-a-mutex-to-$p$pvent-multipule-instances-of-the-same-program-from-running\">Is使用互斥至prevent同一个程序的多个实例运行安全吗?



解决方案

我想你需要前进前一点来考虑您的方案。还有的运行同一程序不止一次许多不同的跨pretations。比如你


  1. 一旦每台机器

  2. 每一次登录会话

  3. 当每个用户

所有这些具有不同的,尽管类似,溶液。

来形容最简单的一种是每台机器。在这种情况下,你要创建一个名为互斥。一个启动每个程序都必须获得这个互斥,如果他们成功了,他们运行并持有到互斥的进程一生的时间。否则,其他一些程序正在运行,他们立即退出。

Unforunately这种方式也有它的缺点。如果我想搞砸你的程序,我可以创建具有相同名称的互斥。这将prevent你的程序运行任何实例,因为他们无法分辨谁拥有互斥,只是一些持有互斥锁。

In a Windows environment, I don't want two instances of my program running at the same time.

Related

Is using a Mutex to prevent multiple instances of the same program from running safe?

解决方案

I think you need to consider your scenario a bit before going forward. There are many different interpretations of "running the same program" more than once. For instance do you

  1. Once per machine
  2. Once per logon session
  3. Once per user

All of these have different, albeit similar, solutions.

The easiest one to describe is the per machine. In this case you want to create a named Mutex. One startup every program must obtain this mutex, if they are successful they run and hold onto the Mutex for the duration of the process lifetime. Otherwise some other program is running and they exit immediately.

Unforunately this approach also has its drawbacks. If I want to mess up your program, I can create a mutex with the same name. This will prevent your program from running any instance because they are unable to tell who holds the Mutex, just that something is holding the mutex.

这篇关于C / C ++如何判断一个程序已经运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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