问2个问题,帮忙
1
int main()
{
int i;
int status = 0;
pid_t pid;
pid = fork();
if(pid == 0)
{
sleep(2);
cout<<"It's child process turn!"<<endl;
for(int i = 0;i < 10;i++)
{
cout<<"child process "<<i<<endl;
}
//_exit(15);
}
else
{
cout<<"It's parent process turn!"<<endl;
for(int i = 0;i < 10;i++)
{
cout<<"parent process "<<i<<endl;
}
}
//wait(&status);
cout<<"status = "<<hex<<status<<endl;
return 0;
}
结果是:
[li@02 ~]$ ./a.out
It's parent process turn!
parent process 0
parent process 1
parent process 2
parent process 3
parent process 4
parent process 5
parent process 6
parent process 7
parent process 8
parent process 9
status = 0 //是不是因为这里是父进程先执行完,所以下面会出现提示符
[li@02 ~]$ It's child process turn!
child process 0
child process 1
child process 2
child process 3
child process 4
child process 5
child process 6
child process 7
child process 8
child process 9
status = 0 //这里就停在这里,不会出现提示符,怎么回事?return 0;子进程不会执行么?
2
int main()
{
int result = 0;
int data = 1;
int b = 0;
pid_t pid = fork();
int pip[2];
if(pipe(pip) == -1)
{
cerr<<"pipe() failed!"<<endl;
}
if(pid == 0)
{
cout<<"Child!!"<<endl;
int num = write(pip[1],&data,4);
cout<<"num = "<<num<<endl;
exit(0);
}
else
{
cout<<"Parent!!"<<endl;
sleep(2);
int a = read(pip[0],&b,4);
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
cout<<strerror(errno)<<endl;
}
return 0;
}
结果是
Child!!
num = 4
Parent!!//停在这里,为什么不会去read?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论