当前位置: 技术问答>linux和unix
小弟在学习LINUX程序设计时遇到有关wait和waitpid的问题,请各位达人帮忙,不胜感激。
来源: 互联网 发布时间:2015-10-25
本文导语: 我写了一个简单的小程序,用fork复制当前进程,然后让子进程创建一个文件(文件名:a,权限:755),然后向这个文件中写入一些数据。当子进程执行完毕以后,父进程打开并读取该文件,并且将读出的数据输出到...
我写了一个简单的小程序,用fork复制当前进程,然后让子进程创建一个文件(文件名:a,权限:755),然后向这个文件中写入一些数据。当子进程执行完毕以后,父进程打开并读取该文件,并且将读出的数据输出到屏幕上。可是,为什么父进程永远执行而不停止呢?(即不停地输出数据,而实际上数据只有十行)小弟百思不得其解,望高手指点。(环境:REDHAT LINUX 9,GCC3.2.2)
#include
#include
#include
#include
#include
#include
#include
#include
int main()
{
#ifdef DEBUG
printf("Program startedn");
#endif
pid_t child;
pid_t childpid;
int fd,fd2;
int ret;
int i;
int length;
int status;
char data[] = "This is for testn";
char *buf;
length = strlen(data);
printf("Process ID: %ldn", (long)getpid());
printf("Parent Process ID: %ldn", (long)getppid());
child = fork();
childpid = wait(NULL);
fprintf(stderr,"Waiting for child pid = %ldn",(long)childpid);
if(child == -1)
{
perror("Process duplication errorn");
return -1;
}
if(child == 0)
{
fprintf(stderr,"child pid = %ldn",(long)getpid());
fd = open("a",O_CREAT | O_RDWR,0755);
if(fd
#include
#include
#include
#include
#include
#include
#include
#include
int main()
{
#ifdef DEBUG
printf("Program startedn");
#endif
pid_t child;
pid_t childpid;
int fd,fd2;
int ret;
int i;
int length;
int status;
char data[] = "This is for testn";
char *buf;
length = strlen(data);
printf("Process ID: %ldn", (long)getpid());
printf("Parent Process ID: %ldn", (long)getppid());
child = fork();
childpid = wait(NULL);
fprintf(stderr,"Waiting for child pid = %ldn",(long)childpid);
if(child == -1)
{
perror("Process duplication errorn");
return -1;
}
if(child == 0)
{
fprintf(stderr,"child pid = %ldn",(long)getpid());
fd = open("a",O_CREAT | O_RDWR,0755);
if(fd