Linux 多執行緒拷貝檔案案例及其傳參說明

2021-07-28 02:40:24 字數 3049 閱讀 4838

下面的簡單案例模擬了多執行緒拷貝乙個檔案

#include #include #include #include #include #include #include #include #include #define max_path 255

#define block_size 4096

typedef struct

thread_info;

int copy(const char *src, const char *dst, int threads);

void *run(void *arg);

pthread_t *tid = null;

thread_info *tinfo = null;

int main(int argc, char *argv)

char src[max_path] = ;

char dst[max_path] = ;

int threads;

memcpy(src, argv[1], strlen(argv[1]));

memcpy(dst, argv[2], strlen(argv[1]));

threads = atoi(argv[3]);

if (threads <= 0)

struct stat srcinfo;

struct stat dstinfo;

if (stat(src, &srcinfo) < 0)

if (srcinfo.st_mode & s_ifdir)

if (access(dst, f_ok) == 0)

if (!(dstinfo.st_mode & s_ifdir))

}int fd = open(dst, o_creat | o_excl, 0644);

if (fd < 0)

tid = (pthread_t *)malloc(threads * sizeof(pthread_t));

if (tid == null)

tinfo = (thread_info *)malloc(threads * sizeof(thread_info));

if (tinfo == null)

copy(src, dst, threads);

int i = 0;

for (; i < threads; ++i)

free(tid);

free(tinfo);

return 0;

}int copy(const char *src, const char *dst, int threads)

int dstfd = open(dst, o_rdwr);

if (dstfd < 0)

struct stat srcinfo;

if (stat(src, &srcinfo) < 0)

off_t size = srcinfo.st_size;

lseek(dstfd, size, seek_set);

write(dstfd, '\0', 1);

int i = 0;

for(; i < threads; ++i)

else

tinfo[i].srcfd = srcfd;

tinfo[i].dstfd = dstfd;

tinfo[i].start = start;

tinfo[i].end = end;

//printf("begin-info: src_fd(%d), dst_fd(%d), start(%d), end(%d)\n", tinfo[i].srcfd, tinfo[i].dstfd, tinfo[i].start, tinfo[i].end);

pthread_create(&tid[i], null, run, (void *)&tinfo[i]); }}

void *run(void *arg)

if (lseek(tinfo.dstfd, tinfo.start, seek_set) < 0)

int length = tinfo.end - tinfo.start;

int size = 0;

char line[block_size] = ;

int readsize = 0;

doelse if (length > 0 && length > block_size)

else

size = read(tinfo.srcfd, line, readsize);

write(tinfo.dstfd, line, size);

length -= size;

memset(line, 0x0, sizeof(line));

}while(1);

return (void *)0;

}

*其中需要注意的是,對於多執行緒的傳參問題。之前很多案例上都是在main函式中呼叫pthread_create,然後傳遞乙個引數給執行緒主函式。這樣幾乎不會出現問題,能夠出現問題的情況是,對於以下案例,將pthread_create在另乙個函式中呼叫,傳遞引數的問題

#include #include #include #include #include int start = 1000;

void *run(void *arg)

void startthread(pthread_t *tid)

int main()

在run中列印start得到的是乙個0值。在前後列印出位址,發現位址沒有改變,值卻變了。經過摸索,得到的結論是,在startthread函式中,當呼叫pthread_create之後整個函式返回,相當於函式的堆疊解旋了。那時候的區域性變數state被覆蓋,顯而易見位址是不變的。當呼叫run函式時候,run函式的堆疊段是乙個新的位址空間,或許就是覆蓋了startthread函式的。

1. 全域性變數,靜態變數

2. 簡單的傳值(這裡的傳值指在run函式中能夠不需要間接引用就能獲取的)

3. 堆資料(malloc,new)

多執行緒拷貝檔案

多執行緒拷貝檔案的步驟 宣告乙個帶有 原始檔,目標檔案,當前執行緒拷貝開始位置,當前執行緒拷貝結束位置 這4個引數的構造器 拷貝執行緒中需要實現 1.統計當前執行緒的拷貝進度 2.使用randomaccessfile獲取輸出流,然後使用seek 方法跳過需要讀寫位元組數 3.while迴圈的條件要加...

Python 多執行緒技術拷貝檔案

使用多執行緒拷貝乙個目錄,要求同時拷貝該目錄下的這些檔案 import os from threading import thread 先定義拷貝函式 defcopy dir from,dir to,file 拷貝檔案 if os.path.isfile s s dir from,file f op...

Linux之多程序拷貝與多執行緒拷貝

讓我們開始編寫 吧 include include include include include include include include intcutting char src,int n int len lseek fd,0,seek end 獲取檔案位元組數 if len n 0 el...