1.
代码:
#include<sys/types.h>
#include<stdio.h>int main()
{int remaining = 4;int child_pid;while(remaining > 0){child_pid = fork();if(child_pid == 0) break;remaining--;}printf("P");wait();return 0;
}
结果:
(出来while()循环后还会打印一下)
2.
代码:
#include<sys/types.h>
#include<stdio.h>int main()
{int remaining = 4;int child_pid;while(remaining > 0){child_pid = fork();//if(child_pid == 0) break;remaining--;}printf("P");wait();return 0;
}
结果:
3.面试题
打印了什么
(4题为去掉"\n"后的打印)
代码:
#include<stdio.h>
#include<unistd.h>int main()
{for(int i=0;i<3;i++){fork();printf("*\n");}return 0;
}
过程:
运行结果:
4.
代码:
#include<stdio.h>
#include<unistd.h>int main()
{for(int i=0;i<3;i++){fork();//printf("*\n");printf("*");}return 0;
}
原理:
(缓冲区会被拷贝 (缓冲区在写满或者换行时会刷新))
3*8 == 24