代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main()
{
char buf[100]={0};
pid_t cld_pid;
int fd;
int status;
if((fd=open("temp",O_CREAT|O_RDWR|O_TRUNC,0664))==-1)
{
perror("create file");
exit(1);
}
strcpy(buf,"parent process data");
if((cld_pid=fork())==0)
{
strcpy(buf,"chid process data");
puts(" child process is working!");
printf("child process PID is %d\n",getpid());
printf("parent process PID is %d\n",getppid());
write(fd,buf,strlen(buf));
close(fd);
exit(0);
}
else
{
puts(" parent process is working!");
printf("parent process PID is %d\n",getpid());
printf("child process PID is %d\n",cld_pid);
write(fd,buf,strlen(buf));
close(fd);
}
wait(&status);
return 0;
}
运行
文章评论(0条评论)
登录后参与讨论