日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux线程出错,在线程应用程序(linux,pthreads)中读取文件大小时出错

發布時間:2023/12/9 linux 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux线程出错,在线程应用程序(linux,pthreads)中读取文件大小时出错 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我試圖從Linux中的文件夾中讀取所有文件和目錄,其線程為 獲取最大文件大小&當前目錄和當前目錄樹下的名稱。在線程應用程序(linux,pthreads)中讀取文件大小時出錯

主線程掃描基本目錄查找文件,當找到它的目錄時,會生成一個新線程以生成繼續掃描的線程。

在這一點上,線程加入,直到最后創建的線程結束。 (我知道這不是最好的方法,但它只是一個練習。)

問題是,它的程序返回錯誤的結果,我不知道為什么。

我有以下文件樹來測試應用程序:下樹

. (Debug folder under codelite project/workspace)

├── [ 4096] dir1

│ └── [ 9] arch-dir1.txt

├── [ 4096] dir2

│ ├── [ 27] arch-dir2.txt

│ └── [ 29083] huge

├── [ 29053] direxp

├── [ 27048] direxp.o

└── [ 68] direxp.o.d

正如你可以看到當前目錄下的最大文件大小是direxp(這個節目),最大文件大小是巨大的

運行二進制文件,我得到了以下結果:

dir: .

dir: ..

arch: direxp.o.d

max dir & tree set to: direxp.o.d size: 68

arch: direxp.o

max file dir set to: direxp.o size: 27048

arch: .d

arch: direxp

max file dir set to: direxp size: 29053

dir: dir1

th dir: .

th dir: ..

th arch: arch-dir1.txt thsize: 4096

max tree file set to: arch-dir1.txt thsize: 4096

dir: dir2

th dir: .

th dir: ..

th arch: arch-dir2.txt thsize: 4096

th arch: huge thsize: 4096

Highest current directory file:

direxp tam:29053 bytes.

Highest tree file:

arch-dir1.txt tam:4096 bytes.

個前綴字符串顯示在另一個線程處理的數據。

我使用函數readdir(主線程)和readdir_r(衍生線程)來讀取目錄條目。

我認為這可能是麻煩,但后來編譯程序調用readdir_r在所有線程下,并且錯誤的結果仍然存在。

我真的不明白,為什么文件大小它返回錯誤的(4096它在我的文件系統默認的簇大小。那么,為什么文件處理為目錄?

能給我一個忙嗎? 感謝

主要功能碼

#include

#include

#include

#include

#include

#include

#include

#include

#include

using std::cout;

using std::cin;

using std::endl;

#define MAX_PATH 255

struct archivo

{

char nombre[MAX_PATH+1];

off_t tam;

};

// thread args

struct thargs

{

char nextdir[MAX_PATH+1]; // next dir

void* (*pth)(void*); // pointer to thread function

archivo* arch; // pointer to archivo

};

pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;

int main(int argc, char **argv)

{

char target[MAX_PATH+1] = {0}; // directorio inicial

archivo grande_dir ={{0}},grande_arbol = {{0}};

// No params

if (argc < 2)

{

if (! getcwd(target,MAX_PATH))

{

perror("Error en path:");

exit(-1);

}

}

if (argc == 2)

strncpy(target,argv[1],MAX_PATH);

if (argc > 2)

{

perror("Num params incorrecto");

exit(-2);

}

DIR* midir = NULL;

// try to open target dir

if (! (midir = opendir(target)))

{

perror("Error abriendo dir:");

exit(-3);

}

dirent* direntry;

//dirent* rentry1 = NULL;

struct stat estado = {0}; // struct needed for desambiguation

bool primera = true; // control var to initialize the search

// read current dir contents

//while((readdir_r(midir,&direntry,&rentry1) == 0) && rentry1 )

while((direntry = readdir(midir)))

{

stat(direntry->d_name,&estado);

// current entry it's a file

if (direntry->d_type == DT_REG)

{

cout << "arch: " << direntry->d_name << endl;

// init search to find the highest file

if (primera)

{

strncpy(grande_dir.nombre,direntry->d_name,MAX_PATH);

grande_dir.tam = estado.st_size;

strncpy(grande_arbol.nombre,direntry->d_name,MAX_PATH);

grande_arbol.tam = estado.st_size;

primera = false;

cout << "max dir & tree set to: " << direntry->d_name << " size: " << estado.st_size << endl;

}

// High file size

if (estado.st_size > grande_dir.tam)

{

pthread_mutex_lock(&lock);

strncpy(grande_dir.nombre,direntry->d_name,MAX_PATH);

grande_dir.tam = estado.st_size;

pthread_mutex_unlock(&lock);

cout << "max file dir set to: " << direntry->d_name << " size: " << estado.st_size << endl;

}

}

// current entry it's a directory

if (direntry->d_type == DT_DIR)

{

cout << "dir: " << direntry->d_name << endl;

// check not . or .. dir

if ((strcmp(direntry->d_name,".") != 0) && (strcmp(direntry->d_name,"..") != 0))

{

thargs args = {{0}};

pthread_t th1;

pthread_mutex_lock(&lock);

sprintf(args.nextdir,"%s/%s",target,direntry->d_name);

args.arch = &grande_arbol;

args.pth = &procesadir;

pthread_mutex_unlock(&lock);

// new thread creation

pthread_create(&th1,NULL,procesadir,&args);

// main thread waits th1 completion

pthread_join(th1, NULL);

}

}

}

closedir(midir);

pthread_mutex_destroy(&lock);

cout << endl << "Highest file in current directory file :" << endl

<< grande_dir.nombre << " tam:" << grande_dir.tam

<< " bytes." << endl;

cout << endl << "Highest file in tree:" << endl

<< grande_arbol.nombre << " tam:" << grande_arbol.tam

<< " bytes." << endl;

return 0;

}

線程函數代碼

void* procesadir(void* args)

{

thargs* myargs = reinterpret_cast(args);

DIR* thdir = NULL;

if ((thdir = opendir(myargs->nextdir)))

{

dirent thentry;

dirent* rentry = NULL;

struct stat thstat = {0};

//while((thentry = readdir(thdir)))

while((readdir_r(thdir,&thentry,&rentry) == 0) && rentry )

{

stat(thentry.d_name,&thstat);

if (thentry.d_type == DT_REG)

{

cout << " th arch: " << thentry.d_name << " thsize: " << thstat.st_size << endl;

if (thstat.st_size > myargs->arch->tam)

{

pthread_mutex_lock(&lock);

memset(myargs->arch->nombre,0,MAX_PATH);

strncpy(myargs->arch->nombre,thentry.d_name,MAX_PATH);

myargs->arch->tam = thstat.st_size;

pthread_mutex_unlock(&lock);

cout << "max tree file set to: " << thentry.d_name << " thsize: " << thstat.st_size << endl;

}

}

if (thentry.d_type == DT_DIR)

{

if ((strcmp(thentry.d_name,".") != 0) && (strcmp(thentry.d_name,"..") != 0))

{

thargs largs = {{0}};

pthread_t th2;

sprintf(largs.nextdir,"%s/%s",myargs->nextdir,thentry.d_name);

largs.arch = myargs->arch;

largs.pth = myargs->pth;

// thread creation

pthread_create(&th2,NULL,procesadir,&args);

// current thread waits th2 completion

pthread_join(th2, NULL);

}

cout << " th dir: " << thentry.d_name << endl;

}

}

closedir(thdir);

else

perror("Error abriendo dir en thread:");

return 0;

}

2012-01-26

ppk

總結

以上是生活随笔為你收集整理的linux线程出错,在线程应用程序(linux,pthreads)中读取文件大小时出错的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。