- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Write a C program that uses two processes (not threads) to sort the regular files in a directory and the sub-directories based on file sizes. The program spawns one process (i.e., child process) to scan the directory and the sub-directories for regular files. When the (child) process finds a regular file, it obtains the size, and sends the size and the pathname of the file to the parent process through a pipe or FIFO. The parent process sorts (in ascending order) and organizes the size and pathname information. When the child process finishes scanning, it notifies the parent process, and the parent process prints out the sizes and pathnames of the files.
#define _XOPEN_SOURCE 500
#include <ftw.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/wait.h>
static int dirSize = 0;
char *dirPath = NULL;
static int dirInfo(const char *fpath, const struct stat *sb, int tflag, struct FTW *ftwbuf){
dirSize = sb -> st_size;
dirPath = fpath;
return 0;
}
int main(int argc, char *argv[]){
pid_t processCheck[1];
int i = 0;
int pipes[1][2];
char *directoryPath = argv[1];
pipe(pipes[i]);
processCheck[0] = fork();
if(processCheck[0]==0){
close(pipes[i][0]);
nftw(directoryPath, dirInfo, 10, FTW_PHYS);
write(pipes[i][1], &dirSize, sizeof dirSize);
write(pipes[i][1], &dirPath, sizeof dirPath);
close(pipes[i][1]);
exit(0);
}
close(pipes[i][1]);
int childProcessStatus;
if(WIFEXITED(childProcessStatus)&&WEXITSTATUS(childProcessStatus)==0){
int v;
char * d=NULL;
if(read(pipes[i][0], &v, sizeof v) == sizeof(v)){
printf("%d\t" , v);
}
if(read(pipes[i][0], &d, sizeof d) == sizeof(d)){
printf("%s\n", d);
}
}
close(pipes[i][0]);
return 0;
}
程序仅打印 4096 4096
最佳答案
sizeof dirPath
返回指针的大小,而不是路径的长度字符串。因为 read
和 write
没有消息边界,如 Mark Plotnick指出在注释中,您需要声明字符串的固定大小(意味着父级和子级使用相同的大小)或者子级写入的长度字符串,然后是字符串。我会选择第二个:
对于 child :
size_t len = strlen(dirPath);
write(pipes[i][1], &dirSize, sizeof dirSize);
write(pipes[i][1], &len, sizeof len);
write(pipes[i][1], dirPath, len); // not &dirPath, otherwise you are sending
// an address, not the string
对于父级:
int v;
size_t len;
char *d=NULL;
if(read(pipes[i][0], &v, sizeof v) != sizeof(v))
{
fprintf(stderr, "Invalid answer from child\n");
exit(1);
}
if(read(pipes[i][0], &len, sizeof len) != sizeof len)
{
fprintf(stderr, "Invalid answer from child\n");
exit(1);
}
// don't have to worry about setting the
// \0-terminating byte
d = calloc(len + 1, 1);
if(d == NULL)
{
fprintf(stderr, "out of memory\n");
exit(1);
}
if(read(pipes[i][0], d, len) != len)
{
fprintf(stderr, "Invalid answer from child\n");
free(d);
exit(1);
}
printf("child sent: dirSize = %d\n", v);
printf("child sent: dirPath = %s\n", d);
free(d);
close(pipes[i][0]);
写这个
write(pipes[i][1], &dirPath, sizeof dirPath);
是一个坏主意,您正在发送指针的地址。这个地址最可能只在 child 的虚拟空间的内存中有效,你没有保证父进程在同一地址有相同的信息在父级的虚拟空间中,父级可能会尝试访问它所在的内存不被允许。您必须发送数组的内容,或者如果您想要避免这种情况,那么你必须使用共享内存。
最后一件事,您不会等待
子进程结束。你应该这样做:
close(pipes[i][1]);
int childProcessStatus;
waitpid(processCheck[0], &childProcessStatus, 0);
if(WIFEXITED(childProcessStatus)&&WEXITSTATUS(childProcessStatus)==0)
{
...
}
关于c - ftw 和流程问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49184104/
Write a C program that uses two processes (not threads) to sort the regular files in a directory and
我正在进行递归遍历,发现 ftw 可以帮助我。 我的程序必须遍历目录并查找文件。对于每个文件,它必须填充一个 FILEMATCH 结构。为此,buildFileMatch 函数接受 char* 文件名
假设一个线程程序正在执行文件系统操作(遍历、读取、创建、删除、移动目录和文件)。 根据 an answer , ftw() 在这种情况下是不安全的。 (此外,ftw() 需要全局变量,这看起来不太优雅
有没有办法将参数发送到 ftw() 以用于处理路径上的每个文件/目录?由于多线程问题,将参数作为全局变量关注有点困难,即将值设置为全局变量将对所有线程可见,这是错误的。 最佳答案 正确设计的 C 回调
我的代码中有以下内容:(在 c 中编码) ftw(argv[2], parseFile, 100) argv[2] 是本地目录路径。例如。 argv[2] = "TestCases"并且在与
我从 opengroup.org 读了两个手册页( ftw 、 nftw ),所以我认为 ftw() 和 nftw() 不能保证线程-安全。 但我找到了另一个 page关于这些函数,请参阅 man7.
这是我正在尝试做的事情: Write a C program using two processes (one parent, one child). Child process goes throu
我想使用 ftw-function递归遍历文件系统结构。此外,该方法应在类内部使用。此外,由 nftw() 调用的入口函数属于同一类。情况必须如此,因为入口函数应该更改某些类成员,具体取决于它找到的文
来自 Wikipedia : Generic programming is a style of computer programming in which algorithms are writte
阅读 man 3 ftw 我观察到“POSIX.1-2008 将 ftw() 标记为已过时”。 现在我很担心并且想成为一个“好的程序员”并且可能尊重 ftw() 的过时性。我现在仍然在努力寻找我应该使
我是一名优秀的程序员,十分优秀!