- 论坛徽章:
- 0
|
如何只让我的程序只运行一个实例?
终于弄好了。
晚上回家,翻书看到apue(unix 环境高级编程)12-5,就是我所需要的。
经过测试,就是我所需要的。所用的方法同上面的朋友说的差不多
----我本来打算用信号量的,查了一下书,还是有点复杂。
谢谢所有回复的朋友。
抄来的代码如下:
- #include <sys/stat.h>;
- #include <errno.h>;
- #include <fcntl.h>;
- #include <sys/types.h>;
- #include <fcntl.h>;
- #define PIDFILE "pid-s2mail.pid"
- #define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
- int
- lock_reg(int fd, int cmd, int type, off_t offset, int whence, off_t len)
- {
- struct flock lock;
-
- lock.l_type = type; /* F_RDLCK, F_WRLCK, F_UNLCK */
- lock.l_start = offset; /* byte offset, relative to l_whence */
- lock.l_whence = whence; /* SEEK_SET, SEEK_CUR, SEEK_END */
- lock.l_len = len; /* #bytes (0 means to EOF) */
-
- return( fcntl(fd, cmd, &lock) );
- }
- #define read_lock(fd, offset, whence, len) \
- lock_reg(fd, F_SETLK, F_RDLCK, offset, whence, len)
- #define readw_lock(fd, offset, whence, len) \
- lock_reg(fd, F_SETLKW, F_RDLCK, offset, whence, len)
- #define write_lock(fd, offset, whence, len) \
- lock_reg(fd, F_SETLK, F_WRLCK, offset, whence, len)
- #define writew_lock(fd, offset, whence, len) \
- lock_reg(fd, F_SETLKW, F_WRLCK, offset, whence, len)
- #define un_lock(fd, offset, whence, len) \
- lock_reg(fd, F_SETLK, F_UNLCK, offset, whence, len)
- void err_sys(char * str)
- {
- printf("%s\n",str);
- exit(-2);
- }
- int check_running()
- {
- int fd, val;
- char buf[10];
-
- if ( (fd = open(PIDFILE, O_WRONLY | O_CREAT, FILE_MODE)) < 0)
- err_sys("open file error\n");
-
- /* try and set a write lock on the entire file */
- if (write_lock(fd, 0, SEEK_SET, 0) < 0) {
- if (errno == EACCES || errno == EAGAIN)
- exit(0); /* gracefully exit, daemon is already running */
- else
- err_sys("write_lock error");
- }
-
- /* truncate to zero length, now that we have the lock */
- if (ftruncate(fd, 0) < 0)
- err_sys("ftruncate error");
-
- /* and write our process ID */
- sprintf(buf, "%d\n", getpid());
- if (write(fd, buf, strlen(buf)) != strlen(buf))
- err_sys("write error");
-
- /* set close-on-exec flag for descriptor */
- if ( (val = fcntl(fd, F_GETFD, 0)) < 0)
- err_sys("fcntl F_GETFD error");
- val |= FD_CLOEXEC;
- if (fcntl(fd, F_SETFD, val) < 0)
- err_sys("fcntl F_SETFD error");
- return 0;
- }
复制代码 |
|