新闻详情

新闻详情

首页 / 资讯中心 / 详情

c实现fastdu替代du -sh ./* -c命令

发布时间:2026/11/28 19:28:39来源:尧图网络
c实现fastdu替代du -sh ./* -c命令
编辑命令gcc -O2 -pthread -stdc11 -o fastdu fastdu.c运行命令./fastdu # 用当前系统 CPU 核数个线程./fastdu -j 16 # 手动指定 16 线程#define _GNU_SOURCE #include stdio.h #include stdlib.h #include string.h #include dirent.h #include sys/stat.h #include unistd.h #include pthread.h typedef struct { char *path; int root; } task_t; static struct { task_t *buf; int cap, head, count; int active; /* 已弹出但未完成的任务数 */ pthread_mutex_t mtx; pthread_cond_t cond; } Q; static long long *root_sizes; static void queue_init(int cap) { Q.cap cap 0 ? cap : 64; Q.buf malloc(sizeof(task_t) * Q.cap); Q.head Q.count Q.active 0; pthread_mutex_init(Q.mtx, NULL); pthread_cond_init(Q.cond, NULL); } /* 取任务返回 0 表示全部完成 */ static int queue_pop(task_t *out) { pthread_mutex_lock(Q.mtx); while (Q.head Q.count Q.active 0) pthread_cond_wait(Q.cond, Q.mtx); if (Q.head Q.count) { pthread_mutex_unlock(Q.mtx); return 0; } *out Q.buf[Q.head]; if (Q.head Q.count) Q.head Q.count 0; /* 顺带回收数组头部 */ Q.active; pthread_mutex_unlock(Q.mtx); return 1; } static void queue_push(char *path, int root) { /* 接管 path 所有权 */ pthread_mutex_lock(Q.mtx); if (Q.count Q.cap) { if (Q.head 0) { /* 先压缩再扩容 */ int n Q.count - Q.head; memmove(Q.buf, Q.buf Q.head, sizeof(task_t) * n); Q.head 0; Q.count n; } if (Q.count Q.cap) { Q.cap * 2; Q.buf realloc(Q.buf, sizeof(task_t) * Q.cap); } } Q.buf[Q.count].path path; Q.buf[Q.count].root root; Q.count; pthread_cond_signal(Q.cond); pthread_mutex_unlock(Q.mtx); } static void queue_done(void) { pthread_mutex_lock(Q.mtx); Q.active--; if (Q.active 0 Q.head Q.count) pthread_cond_broadcast(Q.cond); pthread_mutex_unlock(Q.mtx); } static inline void add_size(int root, long long sz) { __atomic_fetch_add(root_sizes[root], sz, __ATOMIC_RELAXED); } static void *worker(void *arg) { (void)arg; task_t t; while (queue_pop(t)) { DIR *d opendir(t.path); if (d) { size_t plen strlen(t.path); struct dirent *e; while ((e readdir(d)) ! NULL) { const char *n e-d_name; if (n[0] . (n[1] \0 || (n[1] . n[2] \0))) continue; /* 跳过 . 和 .. */ size_t nlen strlen(n); char *full malloc(plen nlen 2); memcpy(full, t.path, plen); full[plen] /; memcpy(full plen 1, n, nlen 1); if (e-d_type DT_DIR) { queue_push(full, t.root); } else if (e-d_type DT_REG) { struct stat st; if (stat(full, st) 0) add_size(t.root, st.st_size); free(full); } else if (e-d_type DT_UNKNOWN) { struct stat st; /* 某些文件系统 d_type 无效 */ if (lstat(full, st) 0) { if (S_ISDIR(st.st_mode)) queue_push(full, t.root); else { add_size(t.root, st.st_size); free(full); } } else free(full); } else { free(full); /* 符号链接/fifo 等直接忽略 */ } } closedir(d); } free(t.path); queue_done(); } return NULL; } static void fmt_size(long long sz, char *buf, size_t buflen) { static const char *u[] {, K, M, G, T, P}; double v (double)sz; int i 0; while (v 1024.0 i 5) { v / 1024.0; i; } if (i 0) snprintf(buf, buflen, %lld, sz); else if (v 10.0) snprintf(buf, buflen, %.1f%s, v, u[i]); else snprintf(buf, buflen, %.0f%s, v, u[i]); } static int cmp_str(const void *a, const void *b) { return strcmp(*(char * const *)a, *(char * const *)b); } int main(int argc, char **argv) { int nthreads 0, opt; while ((opt getopt(argc, argv, j:)) ! -1) if (opt j) nthreads atoi(optarg); if (nthreads 0) { long n sysconf(_SC_NPROCESSORS_ONLN); nthreads (n 0) ? (int)n : 4; } if (nthreads 64) nthreads 64; DIR *d opendir(.); if (!d) { perror(opendir); return 1; } char **names NULL; int nn 0, cap 0; struct dirent *e; while ((e readdir(d)) ! NULL) { if (e-d_name[0] .) continue; /* 与 ./* 通配一致 */ if (cap nn) { cap cap ? cap * 2 : 64; names realloc(names, sizeof(char *) * cap); } names[nn] strdup(e-d_name); } closedir(d); qsort(names, nn, sizeof(char *), cmp_str); root_sizes calloc(nn 0 ? nn : 1, sizeof(long long)); queue_init(nn 0 ? nn : 64); for (int i 0; i nn; i) { size_t l strlen(names[i]) 3; char *p malloc(l); snprintf(p, l, ./%s, names[i]); struct stat st; if (lstat(p, st) 0) { if (S_ISDIR(st.st_mode)) { queue_push(p, i); continue; } add_size(i, st.st_size); /* 顶层普通文件 */ } free(p); } pthread_t *th malloc(sizeof(pthread_t) * nthreads); for (int i 0; i nthreads; i) pthread_create(th[i], NULL, worker, NULL); for (int i 0; i nthreads; i) pthread_join(th[i], NULL); long long total 0; char buf[32]; for (int i 0; i nn; i) { fmt_size(root_sizes[i], buf, sizeof buf); printf(%8s ./%s\n, buf, names[i]); total root_sizes[i]; } fmt_size(total, buf, sizeof buf); printf(%8s total\n, buf); return 0; }
网站建设高端定制企业官网
RELATED

相关资讯

更多精彩内容,欢迎继续阅读

较早相关资讯

最新相关资讯

【外设】之大彩串口显示屏 2026/11/28 5:05:09

【外设】之大彩串口显示屏

大彩串口屏初步使用 1 .官网下载 STM32 屏幕 GUI 设计资料 http://www.gz-dc.com/category/typeid/4112 找到 STM32 Keil 工程,移植相关代码因项目而异进行移植,由于项目简单,本人只对用到的指令接口进行修改。 比如:注意事项&…

阅读更多 →
无法下载Windows系统iso文件 2026/11/25 23:02:13

无法下载Windows系统iso文件

当我遇到这个问题的时候,我打开了一个网站: 登录 然后我打算下载的时候: 突然那个官方的连接就可以下载了:

阅读更多 →
【清华代码熊】DeepSeek V4.1 Flash 后训练详解 2026/11/27 23:07:11

【清华代码熊】DeepSeek V4.1 Flash 后训练详解

📌 上期解析了 DeepSeek V4.1 Flash 模型架构改进,本期解析 DeepSeek V4.1 Flash 预训练/后训练技术: 🌟 预训练:45T 文本 多模态混合语料、直接训练 sparse attention(取消 DeepSeek V4 的 dense 冷启动&…

阅读更多 →
Shuffle-R1: Efficient RL framework for Multimodal Large Language Models via Data-centric Dynamic ... 2026/11/28 17:21:16

Shuffle-R1: Efficient RL framework for Multimodal Large Language Models via Data-centric Dynamic ...

文章主要内容和创新点 主要内容 本文聚焦于多模态大语言模型(MLLM)强化学习(RL)训练中的效率问题,提出了一个名为Shuffle-R1的框架。研究发现,当前RL训练存在两个关键缺陷: 优势值坍缩(Advantage Collapsing):批次中大多数优势值集中在零附近,导致有效梯度信号被淹…

阅读更多 →
PRvL: Quantifying the Capabilities and Risks of Large Language Models for PII Redaction 2026/11/28 2:48:02

PRvL: Quantifying the Capabilities and Risks of Large Language Models for PII Redaction

一、文章主要内容总结 本文聚焦于利用大型语言模型(LLMs)实现个人身份信息(PII)脱敏的研究,旨在解决传统脱敏方法(如基于规则的系统、领域特定命名实体识别(NER)模型)泛化能力差、跨格式/跨语境适应性弱的问题。 研究通过全面评估多种LLM架构(包括密集型LLM(D-LLM…

阅读更多 →
LLaVA-RE: Binary Image-Text Relevancy Evaluation with Multimodal Large Language Model 2026/11/27 19:57:42

LLaVA-RE: Binary Image-Text Relevancy Evaluation with Multimodal Large Language Model

文章主要内容和创新点 主要内容 本文聚焦于二进制图像-文本相关性评估任务(判断图像与文本“相关”或“不相关”),针对该任务中文本格式多样、相关性定义随场景变化等挑战,提出了基于多模态大语言模型(MLLM)的解决方案LLaVA-RE。 模型设计:LLaVA-RE基于LLaVA 1.5架构,…

阅读更多 →

今日资讯

本周资讯

本月资讯

看完文章仍有疑问?

联系尧图顾问,获取一对一建站咨询

立即免费咨询 📞 400-888-8888
📞 ✉