zoukankan      html  css  js  c++  java
  • 守护进程-线程

     

     

     

     

    #include <stdio.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <string.h>
    #include <stdlib.h>
    #include <signal.h>
    #include <sys/time.h>
    #include <time.h>
    #define _FILE_NAME_FORMAT_ "%s/log/mydaemon.%ld"
    
    void touchfile(int num){
        char* HomeDir=getenv("HOME");
        char filename[256]={0};
        sprintf(filename,_FILE_NAME_FORMAT_,HomeDir,time(NULL));
        int fd=open(filename,O_RDWR|O_CREAT,0666);
    
        if(fd<0){
            perror("open error!");
            exit(1);
        }
        close(fd);
    }
    
    int main()
    {
    
        //创建子进程,父进程退出
        pid_t pid=fork();
        if(pid>0){
            exit(1);
        }
        //当会长
        setsid();
        //设置掩码
        umask(0);
        //切换目录
        chdir(getenv("HOME")); //
        //关闭文件描述符
        //执行核心逻辑
        struct itimerval myit={{60,0},{1,0}};
        setitimer(ITIMER_REAL,&myit,NULL);
        struct sigaction act;
        act.sa_flags=0;
        sigemptyset(&act.sa_mask);
        act.sa_handler=touchfile;
        sigaction(SIGALRM,&act,NULL);
        while(1){
            //每隔一分钟在/home/.../log下创建文件
            sleep(1);
        }
        //推出
        return 0;
    }

     

     

    如果不sleep,则进程退出,创建的线程无法执行。

    可以用pthread_exit()退出一个线程

     

    “I will be out”不会执行。exit整个进程都退出了。

     

     (5s后打印第二句,阻塞等待)

     

     (同样效果)

     

     若while(1)中无代码,则无法杀死,即需要一个取消点。

     

    创建多个线程:

     

    (这种情况下num会不稳定,&i指向的值一直在变,num赋值时不一定是什么值)

     

     

    多线程拷贝文件:

    #include <stdio.h>
    #include <pthread.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <string.h>
    #include <fcntl.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <sys/mman.h>
    
    #define _THR_CNT_ 5
    
    typedef struct _TaskInfo{
        int num;
        void* src;
        void* des;
        int size;
    }TaskInfo;
    
    void *thr(void* arg){
        TaskInfo* info=arg;
        int num=info->num;
        int cpsize=info->size/_THR_CNT_;
        int mod=info->size%_THR_CNT_;
        
        if(num==_THR_CNT_-1){
            memcpy(info->des+num*cpsize, info->src+num*cpsize, cpsize+mod);
        }else{
            memcpy(info->des+num*cpsize, info->src+num*cpsize, cpsize);
        }
        return NULL;
    }
    
    int main(int argc, char* argv[])
    {
        if(argc!=3){
            printf("need srcfile and desfile
    ");
            return -1;
        }
        int n=_THR_CNT_;
        struct stat sb;
        if(stat(argv[1],&sb)<0){
            perror(argv[1]);
            exit(1);
        }
        long filesize=sb.st_size;
    
        int fdsrc=open(argv[1],O_RDONLY);
        int fddes=open(argv[2],O_RDWR|O_CREAT|O_TRUNC,0666);
        ftruncate(fddes,filesize);
    
        if(fdsrc<0 || fddes<0){
            printf("open file %s %s err
    ", argv[1], argv[2]);
            exit(1);
        }
    
        void* srcmem=mmap(NULL,filesize,PROT_READ,MAP_PRIVATE,fdsrc,0);
        if(srcmem==MAP_FAILED){
            perror("mmap srcfile err");
            exit(1);
        }
    
        void* desmem=mmap(NULL,filesize,PROT_READ|PROT_WRITE,MAP_SHARED,fddes,0);
        if(desmem==MAP_FAILED){
            perror("mmap desfile err");
            exit(1);
        }
    
        TaskInfo tasks[_THR_CNT_];
        pthread_t tid[_THR_CNT_];
        int i;
        for(i=0;i<n;i++){
            tasks[i].src=srcmem;
            tasks[i].des=desmem;
            tasks[i].num=i;
            tasks[i].size=filesize;
            pthread_create(&tid[i],NULL,thr,&tasks[i]);
        }
    
        for(i=0;i<n;i++){
            pthread_join(tid[i],NULL);
        }
    
        munmap(srcmem,filesize);
        munmap(desmem,filesize);
    
        return 0;
    }

    用守护进程写log:

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <sys/time.h>
    #include <time.h>
    #include <signal.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    void catch_alarm(int num){
        time_t nowtime=time(NULL);
        struct tm* nowtm=localtime(&nowtime);
    
        char strLogFile[100]={0};
        sprintf(strLogFile,"%s/log/touchevery.%04d%02d", getenv("HOME"),nowtm->tm_year+1900, nowtm->tm_mon+1);
    
        int fd=open(strLogFile,O_WRONLY|O_CREAT|O_APPEND,0666);
        if(fd<0){
            perror("open file err");
            printf("file is %s
    ", strLogFile);
            exit(1);
        }
    
        char buf[2014]={0};
        sprintf(buf,"%02d-%02d %02d:%02d:%02d %s [%06d] %s
    ", nowtm->tm_mon+1, nowtm->tm_mday, nowtm->tm_hour, nowtm->tm_min, nowtm->tm_sec, "touchevery",
                getpid(), "I am alive!");
    
        write(fd,buf,strlen(buf));
        close(fd);
    }
    
    int main()
    {
        char *strHomeDir=getenv("HOME");
        pid_t pid=fork();
        if(pid>0){
            exit(1);
        }
        setsid();
        umask(0);
        chdir(strHomeDir);
        close(0);
    
        struct sigaction act;
        sigemptyset(&act.sa_mask);
        act.sa_flags=0;
        act.sa_handler=catch_alarm;
        sigaction(SIGALRM,&act,NULL);
    
        struct itimerval myit={{60,0},{1,0}};
        setitimer(ITIMER_REAL,&myit,NULL);
    
        while(1){
            sleep(120);
        }
        return 0;
    }
  • 相关阅读:
    网络结构解读之inception系列五:Inception V4
    网络结构解读之inception系列四:Inception V3
    网络结构解读之inception系列三:BN-Inception(Inception V2)
    网络结构解读之inception系列二:GoogLeNet(Inception V1)
    网络结构解读之inception系列一:Network in Network
    深度学习相关转载收集
    Person Re-identification 系列论文笔记(八):SPReID
    Person Re-identification 系列论文笔记(七):PCB+RPP
    Person Re-identification 系列论文笔记(六):AlignedReID
    Spring@Autowired注解与自动装配
  • 原文地址:https://www.cnblogs.com/FEIIEF/p/12919846.html
Copyright © 2011-2022 走看看