- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试使用递归 fork 系统创建一个包含 800 个整数的数组的合并排序,以便每个最底部的子级(总共 8 个)每个 qsort 100,然后将数组传回它们各自的父进程进行合并排序并再次向上传递。
由于某种原因,该函数在第一组最底层的子进程完成向其父进程的写入后挂起。
我的递归 fork 函数接收初始数组 800...
static void forkSort(int* parentNums, int size)
{
printf("PPid:%ld Pid:%ld Size:%d\n", (long)getppid(), (long)getpid(), size);
int childNums[size/2], fd[2], left, right;
if(size <= 100) //Send sorted array to parent thru pipe
{
qsort(parentNums, size, sizeof(int), compare);
write(fd[1], &parentNums, sizeof(parentNums));
exit(0);
}
if (pipe(fd)==-1){perror("Failed");}
size /= 2;
if(!(left = tryFork()) || !(right = tryFork())) //Children
{
if(left) copy(childNums, parentNums, size);
else copy(childNums, parentNums + size, size);
forkSort(childNums, size);
}
//Parents
int first[size], second[size], combined[size*2];
read(fd[0], first, sizeof(first));
read(fd[0], second, sizeof(second));
mergeSort(first, second, combined, size);
if(size*2 == SIZE) //Finished, write to out.dat
printArray(combined, SIZE);
else
write(fd[0], combined, sizeof(combined));
}
最佳答案
您的代码有几个问题(我必须补充一点,这看起来很有趣)。
A) 你应该 exit() 而不仅仅是返回客户端代码。否则你将继续执行,尤其是当你进行递归时。
B) 您需要将管道的写入端传递给您的客户,以便他们也知道在哪里写入。我将其作为参数添加到 forkSort()。
C) 当大小 <= 100 时,你执行 sizeof(parentNums)
这会变成 sizeof(int*)
,正确的方法是:大小(整数)*大小
。
D) 当你写回一组合并的整数时,你只写了第一部分,然后把它写到管道的读端。正确的调用是:write(write_fd, combined, sizeof(combined));
。
E) 我删除了 wait(NULL) 调用,因为我不明白这一点。同步将通过 read()
和 write()
调用完成。
这是我的建议:
static void forkSort(int* parentNums, int size, int write_fd)
{
int fd[2];
printf("PPid:%ld Pid:%ld Size:%d\n", (long)getppid(), (long)getpid(), size);
int childNums[size/2], left, right;
if(size <= 100) //Send sorted array to parent thru pipe
{
qsort(parentNums, size, sizeof(int), compare);
write(write_fd, parentNums, size*sizeof(int));
exit(0);
}
if (pipe(fd)==-1){perror("Failed");}
printf("Creating child processes...\n");
size /= 2;
if(!(left = tryFork()) || !(right = tryFork())) //Children
{
if(left) copy(childNums, parentNums, size);
else copy(childNums, parentNums + size, size);
forkSort(childNums, size, fd[1]);
}
/* parent */
int first[size], second[size], combined[size*2];
read(fd[0], first, sizeof(first));
read(fd[0], second, sizeof(second));
printf("\n\nMerge sorting... Size:%d\n", size*2);
mergeSort(first, second, combined, size);
if(size*2 == SIZE) { //Finished, write to out.dat
printf("\nFinished!!! (%d)\n\n", size * 2);
printArray(combined, SIZE);
}
else {
write(write_fd, combined, sizeof(combined));
exit(0);
}
}
关于c - 递归 C mergesort 在使用管道/叉读取时挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13666261/
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 Improve th
我在使用 fork 和 pipes 制作一个用于学习目的的简单程序时遇到了问题。我想要一个 child 向 parent 发送一些数据,然后这个( parent )再次将它发送给 child 。 结果
我正在制作一个需要同时做 3 件事的 python 脚本。什么是实现此目的的好方法,就像我听说的关于 GIL 的方法一样,我不再那么倾向于使用线程了。 脚本需要做的两件事将非常活跃,他们将有很多工作要
有没有办法运行sshd以便它可以(至少对于有限数量的登录)成功返回提示(可能是 busybox),即使 fork 不可用(例如,PID 不足)? 在我看来,这应该是可能的,例如,sshd 预 fork
我意识到 Bootstrap 将使用 v4 切换到 rem。但是,我使用的是当前版本 (v3),我想使用 rem。 原因?我希望网站上有可以为最终用户缩放字体大小的按钮。我相信最好的实现方式是使用 r
我试图在这个程序中将信息从子进程传递到父进程。这是到目前为止的代码,仍在清理它: #include #include #include #include main() { char *
我试图理解 fork 在 C 中是如何工作的,但我在某个地方误解了一些东西。 我去年遇到了一位教授给我的测试,但我无法回复它:我们有 3 个任务(进程或线程),伪代码如下: Th1 { display
我在使用 fork() 之类的东西时遇到了一些麻烦。 我正在开发一个 shell,用户可以在其中编写将像在普通普通 shell 中一样执行的命令。 我有一个像这样的主要功能: void Shell::
我有一个 Python 主进程,以及由主进程使用 os.fork() 创建的一组或多个 worker . 我需要将大型且相当复杂的数据结构从工作程序传递回主进程。您会为此推荐哪些现有库? 数据结构是列
我对这个 fork 语句很陌生,我不知道 C 程序上的 fork 方法。你能告诉我这段代码的三个可能的输出是什么吗? #include #include int main(void) {
for(i=0;i #include int main() { for(int i=0;i<2;i++) { if(fork()==0) { printf("Hi %d %d
背景 我正在用 C 语言编写一个共享库,与 LD_PRELOAD 动态链接,这意味着拦截和覆盖预加载它的应用程序的网络调用,例如 socket()、connect()、recv()、send()等 在
我是一名优秀的程序员,十分优秀!