linux多线程【6】单实例的进程-记录锁的使用

    守护进程往往只维护一个实例,再次起一个进程时,检测到已经运行,就不会再运行了。这一点可以由记录锁来完成。


#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <errno.h>
#include <syslog.h>
#include <errno.h>
#include <sys/types.h>
#define LOCKFILE "/var/run/daemon.pid"
#define LOCKMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)


int
isAlreadRun (void);
int
lockfile (int fd);



int main()
{
   if(isAlreadRun())
    {
        printf("already run!\n");
        return 0;
    }
    printf("first time!\n");
    sleep(10);

    printf("pid: %d; The only thing I do is sleep!\n", getpid());
   return 0;
}

    //在整个文件上加锁  
int
lockfile (int fd)
{
    struct flock fk;
    fk.l_type = F_WRLCK;
    fk.l_start = 0;
    fk.l_whence = SEEK_SET;
    fk.l_len = 0;
    return (fcntl (fd, F_SETLK, &fk));
}

int
isAlreadRun (void)
{
    int fd;
    char buf[16];
    int errno;
    fd = open (LOCKFILE, O_RDWR | O_CREAT, LOCKMODE);
    if (fd < 0)
      {
	  syslog (LOG_ERR, "can't open %s : %s", LOCKFILE, strerror (errno));
	  exit (1);
      }
    if (lockfile (fd) < 0)
      {
	  if (errno == EACCES || errno == EAGAIN)
	    {
		close (fd);
		return (1);
	    }
	  syslog (LOG_ERR, "can't lock %s : %s", LOCKFILE, strerror (errno));
	  exit (1);
      }
    ftruncate (fd, 0);
    sprintf (buf, "%ld", (long) getpid ());
    write (fd, buf, strlen (buf) + 1);
    return 0;
}


    锁的是整个文件,所以其实也是文件锁。文件是

/var/run/daemon.pid
    如果不是root,执行的 时候需要sudo。在主函数中,测试
   if(isAlreadRun())
  

执行:

administrator@ubuntu:~/test$ sudo ./dae &                              后台运行
[1] 3272                                                     
administrator@ubuntu:~/test$ first time!
                                                                                                    回车
administrator@ubuntu:~/test$ sudo ./dae                                  第二次运行
already run!                                                                                 检测到了第一次的运行,退出
administrator@ubuntu:~/test$ sudo ./dae
already run!
administrator@ubuntu:~/test$ sudo ./dae
already run!
administrator@ubuntu:~/test$ sudo ./dae
already run!
administrator@ubuntu:~/test$ sudo ./dae
already run!
administrator@ubuntu:~/test$ pid: 3273; The only thing I do is sleep!                            第一次结束
                                                                                                                                回车
[1]+  完成                  sudo ./dae

如果已经运行,那么就退出。那么,第一次运行和第二次运行有什么不一样呢?
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值