Tag:

叉后的变量

这是一个代码: int i=0; pid_t pid; puts(“Hello, World!”); puts(“”); pid = fork(); if(pid) i=42; printf(“%p\n”, &i); printf(“%d\n”, i); puts(“”); 并输出 Hello, World! 0x7fffc2490278 42 0x7fffc2490278 0 程序打印Hello,World! 有一次,所以子进程从一开始就没有开始,也没有重新定义变量。 变量的地址是相同的。 所以他们是一样的。 但是我在父进程中更改了i的值,它首先被执行,它对于子进程没有改变。 为什么?

我为什么要在这里分叉5次以上?

所以我在这里有代码,我期望它严格运行ls -l 5次,但似乎运行的次数要多得多。 我在这做错了什么? 我想跑ls 5次,所以我叉5次。 也许我不理解正确等待的概念? 我浏览了大量的教程,似乎没有人能彻底解决使用fork的多个进程。 #include #include #include #include #include int main() { pid_t pidChilds[5]; int i =0; for(i = 0; i<5; i++) { pid_t cpid = fork(); if(cpid<0) printf("\n FORKED FAILED"); if(cpid==0) printf("FORK SUCCESSFUL"); pidChilds[i]=cpid; } }

for()循环中的fork()

我正在尝试做一个家庭作业,我必须使用fork()但我不知道为什么在通过我的for循环运行它们之后我无法阻止我的for: #include #include #include #include #include int main(int argc, char *argv[]){ int limit = argc/2; if(argc%2 == 0){ perror(“the number of arguments given must pe even!”); exit(1); } int i; for(i=0; i<=limit; i++){ if(fork()==-1){ perror("childes couldn't be created\n"); exit(1); } if(fork()==0){ printf("fork: %d \n",i); exit(1); } wait(0); } printf("exiting…\n"); return 0; } 输出: warzaru@ubuntu:~/OS/UprocH$ ./exe […]

使用fork()以广度优先顺序打印序列

给定一个进程树 A / \ / \ BC / \ / \ DEFG 我被要求以BFS顺序打印序列,即使用fork()系统调用ABCDEFG,其中每个节点表示一个具有相同父子构造的进程,如树中所示(即A是B和C的父,B是父像D和E那样)。 我想出了这个解决方案,但我真的不明白如何使它以递归方式打印。 static int count; char *msg[] = {“A”, “B”, “C”, “D”, “E”, “F”, “G”}; main(){ if(!fork()){ //Child 1 printf(“%s\t\t%d\t\t%d\n”, msg[count++], (int)getpid(), (int)getppid()); } else{ if(!fork()){ //Child 2 printf(“%s\t\t%d\t\t%d\n”, msg[count++], (int)getpid(), (int)getppid()); } } } 这个逻辑只打印ABC,如何使其递归,以便打印到G? 请帮忙。 谢谢。

如何等待子进程发送信号?

#include #include #include #include void handler(int signumber) { return; } int main() { int i, pid; int children_count = 5; int arr_childprocesses[5]; int parent_pid = getpid(); for(i=0;i<children_count;i++) { pid = fork(); if(pid == -1) { perror("Err"); exit(EXIT_FAILURE); } if(pid == 0) break; arr_childprocesses[i] = pid; } if (pid == 0) // children { kill(parent_pid,SIGUSR1); printf("Child(%d) […]

如何在if语句中使用fork()

有人可以向我解释一下fork() != 0是什么意思? 根据我的理解,我认为这意味着fork是不是假的? 或者如果fork是真的那么….我不明白Fork()如何是真或假,看到它只是创建一个进程的副本到父和子。 另外如果一个程序在哪里说if (Fork() == 0)那意味着什么? #include “csapp.h” int main(void) { int x = 3; if (Fork() != 0) printf(“x=%d\n”, ++x); printf(“x=%d\n”, –x); exit(0); }

C编程中的Fork()函数

我只需要理解这句话: if (fork() && !fork()) 不应该总是假的吗? 我的意思是,如果我写: if (a && !a) 它总是假的,所以第一个也应该总是假的,我错了吗? 我当然是,但我希望有人可以向我解释这个奇怪的事情。 我正在攻读C考试,我必须解决这个问题: int main(){ if(fork && !fork()){ printf(“a\n”); } else printf(“b\n”); }

C:dup2,管道和fork没有按预期工作

我正在尝试做一个简单的fork – >执行另一个程序 – >对那个子进程说“你好” – >回读一些东西 – >打印收到的东西。 作为孩子使用的程序只是等待任何输入行,并向stdout打印一些东西,比如“你好!” 这是我的“主机”程序(不起作用): #include #include #include #define IN 0 #define OUT 1 #define CHILD 0 main () { pid_t pid; int pipefd[2]; FILE* output; char buf[256]; pipe(pipefd); pid = fork(); if (pid == CHILD) { printf(“child\n”); dup2(pipefd[IN], IN); dup2(pipefd[OUT], OUT); execl(“./test”, “test”, (char*) NULL); } else { […]

使用管道在两个子进程之间进行通信

我正在尝试编写产生两个子进程的代码,这些进程通过管道相互发送消息然后终止。 但是,当我运行以下代码时,只有child2打印出问候语,但是子1仍然会打印从子2获取的消息,而子节点1则不会。 有人知道我的方法有什么问题吗? #include #include #include #include #include int main(int argc, char** argv) { char chld_1_send[20] = “hello from child 1”; char chld_1_recv[20]; char chld_2_send[20] = “hi from child 2”; char chld_2_recv[20]; int chld_1_outgoing[2]; int chld_2_outgoing[2]; pipe(chld_1_outgoing); pipe(chld_2_outgoing); int chld_1_status, chld_2_status; pid_t chld_1, chld_2; chld_1 = fork(); if(chld_1 == 0) { chld_2 = fork(); } […]

C管道,fork,dup和exec()

我正在尝试通过管道传递字符串列表到子进程,它应该通过/bin/cat使用execl() 。 我之前有工作,除了管道没有关闭所以程序一直在等待。 不知道我做了什么,现在根本不工作。 有人可以看到我的代码并告诉我,在子进程中cat没有显示str数据我做错了吗? int main(int argc, char** argv) { char *str[] = {“The”, “quick”, “brown”, “fox”, “jumped”, “over”, “the”, “lazy”, “dog”}; int fds[TOTAL_CHILDREN]; int writeFds; int catPID; int status; FILE * write_to_child; //create pipe if (pipe(fds) == -1) { perror(“creating pipe: failed”); exit(EXIT_FAILURE); } pipe(fds); //create subprocess for cat child switch (catPID) { […]