linux c 创建线程 每隔2分钟,去check一个文件,若该文件大小大于1024k,删除文件

可以通过 pthread_create()函数创建新线程。

#include <pthread.h>
int pthread_create(pthread_t *restrict tidp,
                              const pthread_attr_t *restrict attr,
                              void *(*start_rtn)(void *),
                              void *restrict arg);

返回值:
若成功,返回0;否则,返回错误编码
参数说明:
  第一个参数为指向线程标识符的指针。
  第二个参数用来设置线程属性。默认为NULL。
  第三个参数是线程运行函数的起始地址。
  最后一个参数是运行函数的参数。
  另外,在编译时注意加上-lpthread参数,以调用静态链接库。因为pthread并非Linux系统的默认库


创建一个线程,每隔2分钟,去check一个文件,若该文件大小大于1024k,删除文件。再重新打开文件描述符。

    int rd_del_log;
    pthread_t thread_del_log_file;
    rd_del_log = pthread_create(&thread_del_log_file, NULL, delete_log_file,NULL);
    if (rd_del_log != 0)
    {           
        my_printf(LOG_MODE_LEVEL_1,"%s(%d) %s\n",__FUNCTION__,__LINE__,"thread_del_log_file pthread_create error");
    //  continue;
    }
void *delete_log_file()
{
    //int i=0;
    while(1)
    {
        //cprintf("%s(%d) sleep  i:%d\n",__FUNCTION__,__LINE__,i++);    
        sleep(120);
        int ret = rm_dir_file_by_size("/tmp/conf_debug", 1024);// if dir conf_debug size>1024k, rm 
        if(ret != 0)
        {
            cprintf("%s(%d) del conf_debug file error!\n",__FUNCTION__,__LINE__);
        }

        if(access("/tmp/conf_debug", F_OK) != 0)    // file not exist
        {               
            OpenLogFile();
        }
    }

    return NULL;
}

/*****************************************************************
*Description:rm the dir or file, if the dir or file > szie,rm the dir or file. The size unit is kb
*Input:dir_path,dir or file's path, 
*Return: 0 ok, -1 error
*****************************************************************/
int rm_dir_file_by_size(const char *dir_path, int size)
{
    char cmd[128] = {0};
    char out_buf[56] = {0};
    if(dir_path == NULL)
    {
        return -1;
    }
    memset( &cmd, 0x00, sizeof(cmd));
    snprintf(cmd, sizeof(cmd), "du -k %s | tail -n 1 | awk '{print $1}' ", dir_path);
    get_system_output(cmd, out_buf, sizeof(out_buf));
    //cprintf("atoi(out_buf):%d\n", atoi(out_buf));
    if( atoi(out_buf) > size )
    {
        memset( &cmd, 0x00, sizeof(cmd));
        snprintf(cmd, sizeof(cmd), "rm -rf %s", dir_path);
        //cprintf("%s(%d) cmd:%s\n",__FUNCTION__,__LINE__,cmd);
        system(cmd);
    }

    return 0;
}

/*****************************************************************
*Function: get_system_output
*Description:call the system command,gets the return value of the command.
*Input:cmd
*Input:size
*Output:output
*Author:blin
*****************************************************************/
static int get_system_output(char *cmd, char *output, int size)
{
    FILE *fp=NULL;  
    fp = popen(cmd, "r");   
    if (fp)
    {       
        if(fgets(output, size, fp) != NULL)
        {       
            if(output[strlen(output)-1] == '\n')            
                output[strlen(output)-1] = '\0';    
        }   
        pclose(fp); 
    }
    return 0;
}




int OpenLogFile(void)
{

//  CloseLogFile();

    log_fd = open("/tmp/conf_debug", O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR|S_IRGRP);     
    if(log_fd == INVALID_FD)
    {
        printf("%s(%d) open log_file failed:\n",__FUNCTION__,__LINE__);
        return INVALID_FD;
    }

    return 1;
}

猜你喜欢

转载自blog.csdn.net/linbounconstraint/article/details/80452336