Tag: fork

管道流和子进程

我需要编写我的管道流。 我的程序应该获取另一个程序的名称并调用它们,第一个程序应该从第一个输出的stdin读取第二个等等。 最后一个程序在stdout中打印结果。 问题是它不起作用。 当我把这个程序称为另外两个简单程序时(它们都是读取x并打印2 * x,而它可以读取),并给它一些数据,我想我应该立即得到结果,但我没有。 更重要的是,当我给它文件结束它没有反应。 不要注意safe_ *函数,它们与标准相同,但检查错误。 帮帮我,请=) #include #include #include #include #include #include #include #include “error.h” #include “safe_functions.h” void call(const char *filename, int in_descr, int out_descr, pid_t *sons, int n) { sons[n] = safe_fork(); if(sons[n] == 0) { safe_dup2(in_descr, STDIN_FILENO); safe_dup2(out_descr, STDOUT_FILENO); safe_execv(filename, (char **)NULL); } } int find_num(pid_t * sons, […]

在C中用叉子搜索

我应该对fork很熟悉,我看到一个练习,说使用fork调用来搜索从0到15索引的数组。我们假设每个进程只能做两件事…( 1)检查数组的长度是否为1,(2)将数组的单个元素与搜索的数字进行比较。 基本上我传给它一个数字,它应该做有限数量的分叉并返回该数字的索引。 这是我的代码.. #define MAXINDEX 16 int forkSearch(int a[], int search, int start, int end){ if(start == end){ if(*(a + end) == search){ return end; } } else{ pid_t child = fork(); if(child == 0) return forkSearch(a, search, start, end/2); else return forkSearch(a, search, (start + end)/2, end); } } int main(int argc, char* […]

C:execv之前的dup2()

对于家庭作业,我必须编写一个基本的shell,包括重定向。 该程序使用readline来提示输入,解析输入字符串,并将其分解为可执行文件名,参数和输入/输出文件(如果适用)。 在解析字符串之后,它将forx和子execv()传递给传入的可执行文件。我使用dup2()在fork之后和execv之前更改文件描述符,但是一旦遇到问题我就会遇到问题程序已执行新的可执行文件。 如果在我的shell中我运行ls > foo.out ,我得到: ls: cannot access H y A $ L H) I $ : No such file or directory 构建c-> argv: char *args[6]; int i; for(i=0;i<=4;i++){ char *_arg=strsep(&_str_cmd," "); printf("Found _arg: %s\n",_arg); // If there is an argument and it is not blank if(_arg && strcmp(_arg,"")!=0){ if(strcmp(_arg,"”)==0){ _cmd.outfile=strsep(&_str_cmd,” “); i–; […]

在C中使用共享内存的fibonacci序列

我有一个问题要解决,但它给了我错误: 2009-EE-182-Part2.c: In function ‘main’: 2009-EE-182-Part2.c:35:13: error: expected identifier or ‘(‘ before ‘->’ token 2009-EE-182-Part2.c:40:22: error: expected identifier or ‘(‘ before ‘->’ token 2009-EE-182-Part2.c:41:14: error: expected identifier or ‘(‘ before ‘->’ token 2009-EE-182-Part2.c:42:22: error: expected expression before ‘shared_data’ 2009-EE-182-Part2.c:44:15: error: expected identifier or ‘(‘ before ‘->’ token 2009-EE-182-Part2.c:54:15: error: expected expression before ‘shared_data’ 2009-EE-182-Part2.c:55:19: error: […]

子进程的返回值c

我需要帮助从我的子程序返回一个“状态代码”回到父程序,它将检查状态代码,打印代码,并退出父代。 这是一个类项目,所以我将在这里放一些相关的代码,但我不会出于显而易见的原因发布整个项目。 我已经通过exec分叉并创建了子进程。 父进行一些奇特的数学运算并使用命名管道将数据推送到子进程。 孩子做了一些更奇特的数学。 当我使用关键字时,孩子需要返回它在结束时将花式数学返回到父级的次数,其中父级将看到这一点,打印出返回的数字,并退出父级。 int status; pid_t child_id; child_id = fork(); if (child_id == 0) { // put child code here exec(opens second process); } if (child_id < 0) { perror("fork failed\n"); exit(EXIT_FAILURE); } while(child_id != 0) { //parent process //do fancy math stuff here // pipe //open pipe //converted math to "string" […]

在fork和exec之后在父级和子级之间共享文件描述符

我在Linux,A和B上有两个进程。我想从进程A与进程B共享文件描述符,现在我只是将它序列化为char*并将其传递给execl参数,但这不起作用。 Ac看起来像这样: union descriptor{ char c[sizeof(int)]; int i; } fd; pid_t pid; fd.i = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); // Perform other socket functions pid = fork(); if(pid == 0){ // Read data from socket if(execl(“./B”, fd.c, NULL) < 0){ exit(EXIT_FAILURE); }else( exit(EXIT_SUCCESS); } }else if(pid < 0){ exit(EXIT_FAILURE); }else{ waitpid(pid, NULL, 0); } Bc看起来像这样: union descriptor{ […]

带叉子的二进制进程树()

我的OS类的第一个项目是使用fork()创建一个进程树,它具有用户在命令行指定的深度。 每个叶级节点都需要对数据进行排序,并使用命名管道(FIFO)将其传递回父级。 我可以用fork()创建一个N深度树,每个进程有2个子节点。 我无法弄清楚的是如何将FIFO一直传递给树中的每个子进程 ,然后让这个进程对FIFO中的某些数据执行排序,然后将其从树中传递回顶部。 这是我到目前为止构建树的伪代码: void CreateTree(int level) { if level = 0 return int left_child = fork(); if(left_child != 0) //we are the parent { int right_child = fork(); if(right_child == 0) CreateTree(level – 1); } else { CreateTree(level-1); } } 那么我如何单独抓取每个进程来与它们一起工作呢?

如何使用gcov / lcov生成fork()’d孩子的覆盖率报告?

我无法为我的项目生成覆盖率报告 – 看起来叉子之后的子进程中的线路从未被击中,尽管它们显然已经实际存在。 这是分叉部分的工作服报告 (结果与lcov + genhtml相同)和构建日志 。 该项目使用带有libtool的autotools进行构建,并将所有内容打包为静态库。 ( configure.ac , library makefile.am , tests makefile.am ) 我试图将覆盖标志添加到测试中,并且–coverage在CFLAGS中添加 – 但无济于事。 最让我烦恼的是我试图在一个简单的C文件上重现行为,如下所示: #include #include #include #include int main(void) { pid_t pid; if (!(pid = fork())) { puts(“In child”); } else { puts(“In parent”); waitpid(pid, NULL, 0); } return 0; } 使用以下shell会话: /bin/sh ./libtool –tag=CC –mode=compile […]

vfork()系统调用

我读到使用vfork()系统调用创建的新进程作为父地址空间中的线程执行,直到子线程不调用exit()或exec()系统调用,父进程被阻塞。 所以我用vfork()系统调用编写了一个程序 #include #include int main() { pid_t pid; printf(“Parent\n”); pid = vfork(); if(pid==0) { printf(“Child\n”); } return 0; } 我得到的输出如下: Parent Child Parent Child Parent Child …. …. …. 我假设return语句必须在内部调用exit()系统调用,所以我只期望输出 Parent Child 有人可以解释一下为什么它实际上并没有停止并持续打印无限循环。

cat / Xargs /命令VS for / bash / command

Linux 101 Hacks一书的第38页建议: cat url-list.txt | xargs wget –c 我经常这样做: for i in `cat url-list.txt` do wget -c $i done 有什么东西,除了长度,xargs技术在bash中优于旧的for-loop-technique? 添加 C源代码似乎只有一个fork。 相比之下,有多少叉子有bash-combo? 请详细说明这个问题。