0

I have application that has a several process, including the authentication process. I need to prevent launching of authentication, if connection was established and authentication successfull. How can I implement this behavior? Platform - linux.

4

2 回答 2

0

您可以利用共享信号量和小型共享内存。我们称之为信号量mutex和共享内存bool is_authenticated。用值 1 和falsemutex值初始化。is_authenticated

然后您的身份验证过程变为:

wait(mutex);

if (!is_authenticated)
    authenticate();
is_authenticated = true;

signal(mutex);

然后,您将不得不处理身份验证过期问题。所以当会话结束时:

wait(mutex);

assert(is_authenticated == true);  /* if not, you have been compromised */
deauthenticate();
is_authenticated = false;

signal(mutex);
于 2013-04-23T08:51:06.640 回答
0

您可以简单地签入您的进程,因为已经有相同的应用程序正在运行。有强大的库可以让您进行高级检查、锁定资源等等……取决于您使用的语言。

最简单的解决方案是创建一个表明程序正在运行的锁定文件(例如在 /tmp 中),并在程序的开头检查是否存在这样的文件。这种方法的缺点是您必须确保即使应用程序崩溃也会删除锁定文件。

于 2013-04-23T08:45:52.643 回答