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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/394795.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/394795.shtml
英文地址,請注明出處:http://en.pswp.cn/news/394795.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【用jQuery來判斷瀏覽器的類型】及【javascript獲取用戶ip地址】

用jQuery來判斷瀏覽器的類型,主要是使用$.browser這個工具類,使用方法: $.browser.[瀏覽器關鍵字] //谷歌瀏覽器、360瀏覽器等其他一些瀏覽器&#xff0c;沒有專門的判斷 function appInfo() {var bro $.browser;var binfo "";if (bro.msie) {binfo "Micr…

python函數學習1

函數1 &#xff08;1&#xff09;定義&#xff1a; def 函數名&#xff08;參數列表&#xff09;函數體 &#xff08;2&#xff09;參數傳遞&#xff1a; 在python中&#xff0c;一切都是對象&#xff0c;類型也屬于對象&#xff0c;變量是沒有類型的。 a [1,2,3] a "he…

kafka應用于區塊鏈_Apache Kafka的區塊鏈實驗

kafka應用于區塊鏈by Luc Russell盧克羅素(Luc Russell) Apache Kafka的區塊鏈實驗 (A blockchain experiment with Apache Kafka) Blockchain technology and Apache Kafka share characteristics which suggest a natural affinity. For instance, both share the concept o…

pythonfor循環100次_以寫代學: python for循環 range函數 xrange函數

腳本一&#xff1a; #!/usr/bin/env python # coding: utf8 sum100 0 for i in range(101): sum100 i #&#xff08;1&#xff09;range是一個可以取值的函數&#xff0c;上邊這個取的是0-100&#xff0c;并不包含101 #&#xff08;2&#xff09;也可以指定&#xff0c;比如r…

iis下php 500錯誤

很不想用iis&#xff0c;然而客戶不想增加機器&#xff0c;只好按客戶的意思了。可是沒想到發送短信以在本地 機器上是好的&#xff0c;在iis下直接500。 ??一開始以為是防火墻問題&#xff0c;后來檢查了一下沒有&#xff0c;再后來換了一個短信接口&#xff0c;就莫名其妙好…

linux mv 遞歸拷貝,奇技淫巧 - 給Linux中的cp和mv命令中添加進度條的高級拷貝

GNU cp和GNU mv命令用于在GNU/Linux操作系統中復制和移動文件和目錄。這兩個命令缺少的一個特性是它們不顯示任何進度條。如果復制一個大文件或目錄&#xff0c;您就不知道完成復制過程需要多長時間&#xff0c;也不知道復制的數據所占的百分比。還有您將看不到當前正在復制哪個…

webgl 著色器_如何在WebAssembly中使用WebGL著色器

webgl 著色器by Dan Ruta通過Dan Ruta 在WebAssembly中使用WebGL著色器 (Using WebGL shaders in WebAssembly) WebAssembly is blazing fast for number crunching, game engines, and many other things, but nothing can quite compare to the extreme parallelization of …

【洛谷P1966】火柴排隊

兩列排序后將編號一一對應 歸并排序求逆序對 &#xff08;每一次交換就去掉一個逆序對&#xff09; 1 #include<cstdio>2 #include<cstring>3 #include<algorithm>4 #define ll long long5 using namespace std;6 const int N100100;7 const ll P99999997;8 …

python字符串補空格輸出_Python去除空格,Python中常見字符串去除空格的方法總結...

今天小編就為大家分享一篇關于Python去除字符串前后空格的幾種方法&#xff0c;小編覺得內容挺不錯的&#xff0c;現在分享給大家&#xff0c;具有很好的參考價值&#xff0c;需要的朋友一起跟隨小編來看看吧&#xff1a; Python去除空格方法一&#xff1a; strip()方法&#x…

Alan Walker MV 合輯01 by defender

Alan Walker MV合輯 出來啦&#xff01; 百度網盤下載地址&#xff1a; 鏈接&#xff1a;https://pan.baidu.com/s/10WSool70XBe_8tJOae8V-w 提取碼&#xff1a;uckq 地址查看Microsoft Onedrive Download Address:  BE DELETED Google Drive Download Address&#xff1a; …

scanf函數具體解釋與緩沖區

1.基本信息 函數原型&#xff1a; int scanf( char *format, args, ...); 函數返回值&#xff1a; 讀入并賦給args的數據個數。遇到文件結束返回EOF&#xff0c;出錯返回0。 函數功能&#xff1a; scanf函數是格式化輸入函數&#xff0c;它從標準輸入設備(鍵盤)讀取輸入的信息。…

linux中win文件轉為unix,如何將文本文件從Windows轉換為Unix

從Unix轉換到Windows時&#xff0c;我得到正確的輸出;但是&#xff0c;從Windows到Unix時&#xff0c;我得到了一些奇怪的輸出。我認為我必須允許的是刪除回車\ r。雖然這不起作用。當我運行代碼后打開文本文件時&#xff0c;我得到了一些奇怪的結果&#xff0c;第一行是正確的…

程序員偽造一年工作經驗_試火—如何偽造程序員

程序員偽造一年工作經驗2017年9月6日 (6 September 2017) Sweat is running down my face. I’m staring down a blank sublime text document. What on earth am I doing? My hands are resting above the keyboard of my MacBook pro.汗水順著我的臉。 我盯著一個空白的崇高…

在unity中設置多種怪物數據_Unity可編程渲染管線(SRP)系列(三)——光照(單通道 正向渲染)...

本文重點:1、漫反射著色2、支持方向光、點光源和聚光燈3、每幀允許16個可見光源4、每個對象最多計算四個像素光和四個頂點光這是涵蓋Unity可編寫腳本的渲染管線的教程系列的第三部分。這次&#xff0c;我們將通過一個Drawcall為每個對象最多著色8個燈光來增加對漫反射光照的支持…

Java內部類的定義和使用

為什么要用到內部類&#xff1a; 在java開發學習中我們經常會碰到內部類。內部類又有很多的優勢&#xff1a;首先舉一個簡單的例子&#xff0c;如果你想實現一個接口&#xff0c;但是這個接口中的一個方法和你構想的這個類中的一個方法名稱參數相同&#xff0c;你應該怎么辦&am…

蛋清打發奶油狀

在做蛋糕、冰淇凌、面包之類的時候往往都需要奶油狀蛋清&#xff0c;讓蛋糕、面包更蓬松&#xff0c;冰激凌也可以使用其當做奶油來用。用料 雞蛋4個 根據用量選擇鹽(只做冰激凌用奶油放)5g(根據蛋量)白醋(可以不放&#xff0c;根據喜好)5g(根據蛋量)白砂糖40g(分三次放)根據…

react構建_您應該了解的有關React的一切:開始構建所需的基礎知識

react構建by Scott Domes由斯科特多姆斯(Scott Domes) 您應該了解的有關React的一切&#xff1a;開始構建所需的基礎知識 (Everything You Should Know About React: The Basics You Need to Start Building) Are you curious about React and haven’t had the chance to lea…

榮新linux培訓,51CTO博客-專業IT技術博客創作平臺-技術成就夢想

切換用戶 su - root文件夾管理 mkdir(新建文件夾) rmdir(刪除空目錄)文件管理 touch(新建文件) rm(刪除文件)rm -rf(刪除文件夾) cat(查詢文件)文件文件夾 mv(剪切文件) cp(復制文件)默認拷貝文件&#xff0c;cp -r 就可以拷貝文件夾啦批量建文件 touch /root/tes…

Educational Codeforces Round 33 (Rated for Div. 2) E. Counting Arrays

題目鏈接 題意&#xff1a;給你兩個數x,yx,yx,y,讓你構造一些長為yyy的數列&#xff0c;讓這個數列的累乘為xxx&#xff0c;輸出方案數。 思路:考慮對xxx進行質因數分解&#xff0c;設某個質因子PiP_iPi?的的冪為kkk,則這個質因子的貢獻就相當于把kkk個PiP_iPi?放到yyy個盒子…

《面向對象分析與設計》一第2章 什么是面向對象分析

第2章 什么是面向對象分析 面向對象分析&#xff08;ObjectOriented Analysis&#xff0c;OOA&#xff09;&#xff0c;就是運用面向對象方法進行系統分析。它是軟件生命周期的一個階段&#xff0c;具有一般分析方法所共同具有的內容、目標及策略。但是OOA強調運用面向對象方…