多线程拷机工具

#define __USE_LARGEFILE64
#define _LARGEFILE64_SOURCE
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif


#include <stdio.h>
#include <stdlib.h>
#include<unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>
#include "tools.h"


int main(int argc, char *argv[])
{
    struct stat64 buf1;
    int mode = atoi(read_cfg("conf","mode"));
    if(0 == mode && argc != 2){   
        printf("showld be ./program conf\n");
        return; 
    }


    if(0 == mode)
        multi_write(argv[1]);


    return 0; 

}


Host190:/home/oyw/perf_tool # cat tools.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>
#include <pthread.h>
#include <errno.h>
#define BUFSIZE (256 * 1024)
pthread_t tid[1024];
static pthread_mutex_t iRWContinueMutex = PTHREAD_MUTEX_INITIALIZER;


void writelocalfile(char *path,char *content)
{
    char *para = NULL;
    int argnum = 0, i = 0;
    int fd = open(path, O_WRONLY | O_CREAT, 00644);
    lseek(fd,0 ,SEEK_END);
    write(fd, content, strlen(content));
    close(fd);
}


/*return all string after "item1="*/
char * read_cfg(char *cfg, char *item1)
{
    char str[1024] = "", item[1024] = "";
    char * str_pos = NULL;
 
    sprintf(item, "%s=", item1);
    FILE *fp = fopen(cfg, "r"); 
    if(fp){
        while(NULL != fgets(str, 1024, fp)){
            if( NULL != (str_pos = strstr(str, item)) ){
                return str_pos + strlen(item);
            }
            memset(str, 0 , sizeof(str));
        }
    }


}
void *single_write(void *p)
{
    int fd = -1, write_cnt = 0, ret = -1, width = 0, i = 0, write_flag = -1, continue_write_flag = -1, mode = -1, trunc_flag = -1;
    unsigned long long filesize = 0;
    long pos = 0;
    char homepath[1024] = "", fullpath_dir[1024] = "", fullpath[1024] = "", filesize_num[100] = "";


    long unsigned int thread_num = pthread_self();
    int thread_idx = *((int *)p);
    //int status = pthread_detach( thread_num ); /*detach to be an independent thread*/


    char * filesize_full = read_cfg("conf","filesize");
    char filesize_flag = *(filesize_full + strlen(filesize_full) - 2 );
    strncpy(filesize_num, filesize_full, strlen(filesize_full) - 2 );


    if('k' == filesize_flag || 'K' == filesize_flag)
        filesize = atol(filesize_num) * 1024;
    else if('m' == filesize_flag || 'M' == filesize_flag)
        filesize = atol(filesize_num) * 1024 * 1024;
    else if('g' == filesize_flag || 'G' == filesize_flag)
        filesize = atol(filesize_num) * 1024 * 1024 * 1024;
    else if('t' == filesize_flag || 'T' == filesize_flag)
        filesize = atol(filesize_num) * 1024 * 1024 * 1024 * 1024;
    else
        filesize = atol(filesize_num);


    char *homepath_full = read_cfg("conf","path");
    strncpy(homepath, homepath_full, strlen(homepath_full) - 1);
    sprintf(fullpath_dir,"%s/%d",homepath, thread_idx);
    int mkdir_ret = mkdir(fullpath_dir, 0777);
    if(0 > mkdir_ret)
        printf("mkdir %s err,ret:%d\n", fullpath_dir, mkdir_ret);


    width = atoi(read_cfg("conf", "width"));
    write_flag = atoi(read_cfg("conf","write_flag"));
    trunc_flag = atoi(read_cfg("conf","trunc_flag"));
    char buf[BUFSIZE]="";
    for(i = 0; i <= BUFSIZE; i ++){
        buf[i] = (0 == (i % 2 )) ?  '-' :  '*';
    }
    while(1){
        for(i = 0; i < width ; i ++){
        sprintf(fullpath, "%s/file%d",fullpath_dir, i);
        write_cnt = (0 == filesize % BUFSIZE) ? filesize / BUFSIZE : filesize / BUFSIZE + 1;
        while(1){
            fd = open(fullpath, (0 == trunc_flag)? O_WRONLY | O_CREAT: O_WRONLY | O_CREAT | O_TRUNC , 00644);
            if(0 > fd)
                printf("open %s err,ret:%d\n", fullpath, fd);
            for(i = 0; i < write_cnt; i ++){
                ret = write(fd, buf, BUFSIZE);
            }
            if(0 > ret){
                close(fd);
                break;
            }
            if(0 < fd)
                ret = close(fd);
            if(1 == continue_write_flag) ;
            else
                break;
            }
        }
            if(1 == write_flag){
                pthread_mutex_lock(&iRWContinueMutex);
                continue_write_flag = 1;
                pthread_mutex_unlock(&iRWContinueMutex);             
            }
            else{
                pthread_mutex_lock(&iRWContinueMutex);
                continue_write_flag = 0;
                pthread_mutex_unlock(&iRWContinueMutex);
                break;
            }
    }
    pthread_exit(NULL);
}
int multi_write(char *conf)
{
    int thread_num = atoi(read_cfg(conf, "thread_num")), i = 0;
    int tmp[1024];
    for(i = 0; i < thread_num; i ++){
        tmp[i] = i;
        pthread_create( &tid[i], NULL, &single_write, &tmp[i]);
    }
    join();
    return ;
}
void join()
{
    int i = 0;
    int join_ret = -1;
    void *res;
    
    while(i < 1024) {
        if(tid[i] == -1 || tid[i] == 0) {
            i ++;
            continue;
        }
        else {
            join_ret = pthread_join(tid[i], &res);
            if (join_ret != 0) {
                perror( "pthread_join error"); 
                printf("pthread_join error, errno = %d, pthread_join ret :%d\n", errno, join_ret);
            }


            if (res == PTHREAD_CANCELED) {
                printf("Thread(%lx) was canceled\n", tid[i] );
            }
            else {
                printf("Thread(%lx) normal exit!\n", tid[i] );
            }
            tid[i] = 0;
            i ++;
        }
    }
    printf("join end, main thread exit\n");
}


Host190:/home/oyw/perf_tool # cat tools.h
void writelocalfile(char *path,char *content);
char * read_cfg(char *cfg, char *item);
int single_write(void);
int multi_write(char *conf);
void join();


Host190:/home/oyw/perf_tool # cat conf 
/*readme
mode:0 for kaoji,1 for multi thread copy file
write_flag:1(re_truncate_write after write over),0(quit after write over)
write_time:hours,take effect if write_flag is 0
*/
mode=0
filesize=100m
path=/multi_write
depth=1
width=10
thread_num=10
write_flag=0
trunc_flag=1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值