Unix環境產生單一實例進程方案

來源:互聯網
上載者:User

在一些情況下,一個進程只能產生一個執行個體來執行。Unix環境,提供了檔案-記錄鎖(file- and record-locking)機制,提供了事項單一實例進程的基本解決方案。
 
假如,一個進程在開始運行時,產生了一個檔案,並且,對整個檔案上鎖,並且,只有一個這樣的寫鎖允許產生。
 
如果,後續的進程要試圖產生寫鎖,會導致失敗。這暗示了,前面已經有執行個體運行了。
 

 

 

下面一個判斷是否有執行個體啟動並執行方法。每個執行個體,都會試圖產生一個檔案(/var/run/daemon.pid).如果檔案已經鎖上了,lockfile方法,返回失敗,判斷函數返回1,表示進程已經運行了。如果沒有執行個體運行,程式,清空檔案,寫入進程id,返回0.
 
下面為一個實現的程式:
 

#include <unistd.h>   
#include <stdio.h>   
#include <stdlib.h>   
#include <fcntl.h>   
#include <syslog.h>   
#include <string.h>   
#include <errno.h>   
#include <sys/stat.h>   
#define LOCKFILE "/var/run/daemon.pid"   
#define LOCKMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)   
 int already_running(void);   
 int lockfile(int );   
 int main(int argc,char * argv[])   
 {   
     int val = already_running();   
     if(val == 0)   
     {   
         printf("sart to running...\n");   
     }   
     else  
     {   
         printf("alredy running...\n");   
         exit(0);   
     }   
     while(1)   
     {   
         sleep(3);   
         printf("...\n");   
     }   
     return 0;   
 }   
 int already_running(void)   
 {   
     int fd;   
     char buf[16];   
     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;   
 }   
 int lockfile(int fd)   
 {   
     struct flock fl;   
     fl.l_type = F_WRLCK;   
    fl.l_start = 0;   
    fl.l_whence = SEEK_SET;   
     fl.l_len = 0;   
     return(fcntl(fd, F_SETLK, &fl));   
 } 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.