我有这段代码:
#include <sys/types.h> /* pid_t */
#include <sys/wait.h> /* waitpid */
#include <stdio.h> /* printf, perror */
#include <stdlib.h> /* exit */
#include <unistd.h> /* _exit, fork */
#include <string.h>
void rec(n)
{
int l, r;
char *new_args[] = {"./bla1", NULL };
if (n)
{
l = fork();
if (l!=0) //parent
r = fork();
if (l == 0 || r == 0)
{
rec(--n); //return;
}
/* if (l == 0) {
rec(--n); return; }
r = fork();
if (r == 0) {
rec(--n); }*/
}
else //call Sorters
{
printf("Execv!!!!\n");
// if (execv(new_args[0], &new_args[0]) < 0) perror("execvp error!\n");
}
}
int main(int argc, char *argv[]) {
rec(3);
return 0;
}
我得到了这些结果:
vasilis @ ubuntu:〜/桌面$ Execv !!!!
Execv !!!!
Execv !!!!
Execv !!!!
Execv !!!!
Execv !!!!
Execv !!!!
&lt; ----这里是空的,就像是在等待一个字母
vasilis @ ubuntu:〜/ Desktop $
在我表现出来的时候就像在等待一个角色。我按下Enter然后程序终止。有什么想法吗?
答案 0 :(得分:0)
只有你的8叶子孩子都想在控制台上写一个字符串。他们正在同时做这件事。结果,它们的符号(包括\ n)是混合的。更多的是,主要任务返回,shell也将vasilis @ ubuntu:〜/ Desktop $写入同一个控制台。很奇怪你看到SOMETHING显然是明智的。
他们应该将他们的输出放在一个队列中,一个接一个地放在一起,并且只有在完成所有工作后才能输出。
答案 1 :(得分:0)
您的每个孩子都将所述字符串写入控制台。
正如在write()
次调用中所做的那样,输出保持不变。
但是:当您的子进程在“后台”执行时,父进程将以静默方式退出并返回到您在第一行中看到的提示。
由于孩子的输出是在 之后打印出来的,所以提示已经给出,当然不会重复。如果您输入ls
而不是按Enter键,则会看到。