Tag: fork

fork()子进程和父进程

我正在尝试创建一个使用fork()来创建新进程的程序。 示例输出应如下所示: 这是子进程。 我的pid是733,我父母的id是772。 这是父进程。 我的pid是772,我孩子的id是773。 这就是我编写程序的方式: #include #include int main() { printf(“This is the child process. My pid is %d and my parent’s id is %d.\n”, getpid(), fork()); return 0; } 这导致输出: 这是子进程。 我的pid是22163,我父母的id是0。 这是子进程。 我的pid是22162,我父母的id是22163。 为什么它会两次打印语句?如何在第一句中显示子ID后如何正确显示父语句? 编辑: #include #include int main() { int pid = fork(); if (pid == 0) { printf(“This is […]

使用fork()和exec()在C中执行命令行管道

我询问了我的代码,答案是不正确的。 在这种情况下perror使用现在我想知道我如何调整和改进,以便我不再有这些错误? execvp不会返回,除非发生错误,所以如果一切正常,封闭函数将永远不会返回。 由于先前的循环,值’i’已经超过’cmd’中数组的末尾,因此’cmd [i] .argv [0]不正确。 cmd不是struct命令的数组,因此不应编入索引 cmd.argv中的第一个条目是指向最后一个条目为NULL的数组的指针。 execvp将在该(并且只有那个)数组上工作,因此所有其他指向数组的指针都将被忽略 代码中存在大量错误。 例如,第一次通过fork_pipe()’in’中的循环包含垃圾。 传递给execvp()的第二个参数需要是一个指向字符串的指针,带有一个最终的NULL指针。 缺少最后的NULL指针,还有很多问题 #include #include #include #include #include #include struct command { const char **argv; }; /* Helper function that spawns processes */ int spawn_proc (int in, int out, struct command *cmd) { pid_t pid; if ((pid = fork ()) == 0) { if […]

waitpid()的示例正在使用中?

我知道waitpid()用于等待进程完成,但是如何才能完全使用它? 在这里我想要做的是,创造两个孩子并等待第一个孩子完成,然后在退出前杀死第二个孩子。 //Create two children pid_t child1; pid_t child2; child1 = fork(); //wait for child1 to finish, then kill child2 waitpid() … child1 { kill(child2) }

cd(C)的Minishell问题

我在C中制作了一个简单的minishell,它的工作原理,除了cd命令。 当我尝试运行它时,没有任何事情发生,除了它创建一个永远不会真正结束的子进程。 例如,在我的minishell中运行cd ,我需要输入两次退出以退出minishell,而不是标准的一次。 代码:int debug = 0; void safeQuit(){ if (debug==1) printf(“INFO: Parent process, pid=%d exiting.\n”, getpid()); exit(0); } int main(int argc,char** argv) { int pid, stat; char * Array_arg[50]; char command_line[200];//user input if (argc>1) if (strcmp(argv[1],”-debug”)==0) debug=1; while (1){ printf(“[minishell]> “+debug); gets(command_line); if (strcmp(command_line,”exit”)==0) safeQuit(); char * subcommand=strtok(command_line,” “); //divides the string according […]

C – fork和printf行为

与printf一起测试fork函数我发现了一些奇怪的行为 例如,代码: int main(){ if(fork()==0){ printf(“TestString”); } } 不打印任何东西,而 int main(){ if(fork()==0) { printf(“TestString\n”); } } 正确打印出TestString。 为什么打印新行会改变行为? 我怀疑它可能与fflush()有关,但我不确定。 我可以获得解释或链接,我可以阅读它吗? 提前谢谢你的回答。 编辑:我正在寻找的解释是什么实际上是冲洗,为什么和冲洗相同。

vfork()atexit断言失败

我想了解下面这段代码 #include #include #include int main() { pid_t pid ; unsigned int i=0; pid=vfork(); switch(pid) { case -1: // some sort of error puts(“fork error”); break; case 0: // the child process while(i<100) { printf("%d\n", i); i++; } break; default: //parent while(i<1000) { printf("%d\n", i); i++; } break; } // _exit(0); } 请不要告诉我vfork()是坏事和这些事情。我知道它是,但正是在这段代码中正在发生的事情导致了这种错误。 提前致谢

从execv()中获取返回值

//code for foo (run executable as ./a.out) #include #include #include #include #include int main (int argc, char **argv) { pid_t pid; pid = fork(); int i = 1; char *parms[] = {“test2”, “5”, NULL}; //test executable named test2 if(pid < 0) { fprintf(stderr, "Fork failed"); return 1; } else if(pid == 0) { printf("Child pid […]

fork(),多个孩子的问题

我编辑了一下: for ( ii = 0; ii < nbEnfants; ++ii) { switch (fork()){ case -1 : { printf("\n\nSoucis avec fork() !!! \n\n"); exit(0); }; case 0 : { EEcrireMp(ii); }break; default : { tabPidEnfants[ii] = p; usleep(50000); ELireMp(nbSect, nbEnfants,tabPidEnfants); }; } } 我的问题:我遇到了很多孩子,就像孩子产卵的炸弹一样。 我怎么能阻止那些孩子? rest应该阻止它吗? 谢谢

c /中断系统调用/ fork与线程

我发现了线程实现的问题,这对我来说很奇怪。 也许有些人可以向我解释一下,会很棒。 我正在开发类似代理的程序,一个程序(在不同的机器上运行),它通过eth0接收数据包并通过ath0(无线)发送到另一台完全相同的机器。 实际上我完全不确定是什么导致了我的问题,那是因为我对一切,linux和c编程都是新手。 我开始两个线程, 一个是在eth0上侦听(套接字)传入的数据包并通过ath0(也是套接字)发送出去 另一个线程正在监听ath0并通过eth0发送。 如果我使用线程,我会得到一个错误: sh-2.05b# ./socketex Failed to send network header packet. : Interrupted system call 如果我使用fork(),程序按预期工作。 有人可以向我解释这种行为吗? 只是为了显示发件人实现,这里是它的代码片段: while(keep_going) { memset(&buffer[0], ‘\0’, sizeof(buffer)); recvlen = recvfrom(sockfd_in, buffer, BUFLEN, 0, (struct sockaddr *) &incoming, &ilen); if(recvlen < 0) { perror("something went wrong / incoming\n"); exit(-1); } strcpy(msg, buffer); buflen = strlen(msg); […]

如何使用execvp()

用户将读取一行,我将保留第一个单词作为execvp的命令。 让我们说他会输入“cat file.txt” …命令将是cat。 但我不知道如何使用这个execvp() ,我读了一些教程,但仍然没有得到它。 #include #include int main() { char *buf; char command[32]; char name[32]; char *pointer; char line[80]; printf(“>”); while((buf = readline(“”))!=NULL){ if (strcmp(buf,”exit”)==0) break; if(buf[0]!=NULL) add_history(buf); pointer = strtok(buf, ” “); if(pointer != NULL){ strcpy(command, pointer); } pid_t pid; int status; if ((pid = fork()) < 0) { printf("*** ERROR: forking […]