当前位置: 技术问答>linux和unix
UNIX 新手一问 关于fork 和 2进程同时写一文件出现的问题
来源: 互联网 发布时间:2016-02-04
本文导语: 下面一段程序运行会死! 请问是什么原因呢? #include #include #include #include #include typedef int XFILE; typedef enum _BOOL{FALSE,TRUE} BOOL; BOOL IsFileCanbeSeek(XFILE file); const char buf1[]="abcdefghijklmnopqrstuvwxyzn"; const char buf2...
下面一段程序运行会死! 请问是什么原因呢?
#include
#include
#include
#include
#include
typedef int XFILE;
typedef enum _BOOL{FALSE,TRUE} BOOL;
BOOL IsFileCanbeSeek(XFILE file);
const char buf1[]="abcdefghijklmnopqrstuvwxyzn";
const char buf2[]="ABCDEFGHIJKLMNOPQRSTUVWXYZn";
const char buf3[]="--------------------------n";
int count=0;
char buf4[8];
int main(void)
{
XFILE file=open("testfile.t",O_RDWR|O_CREAT|O_TRUNC);
write(file,buf3,sizeof(buf3));
int pid,cpid,cpid2;
pid=fork();
if(pid==0) cpid=getpid();
if(cpid==getpid()) printf("%dn",cpid);
if(cpid!=getpid()) pid=fork();
if(pid==0&&cpid!=getpid())cpid2=getpid();//.............
if(pid!=0)exit(0);
int i=0;
for(i=0;i
#include
#include
#include
#include
#include
typedef int XFILE;
typedef enum _BOOL{FALSE,TRUE} BOOL;
BOOL IsFileCanbeSeek(XFILE file);
const char buf1[]="abcdefghijklmnopqrstuvwxyzn";
const char buf2[]="ABCDEFGHIJKLMNOPQRSTUVWXYZn";
const char buf3[]="--------------------------n";
int count=0;
char buf4[8];
int main(void)
{
XFILE file=open("testfile.t",O_RDWR|O_CREAT|O_TRUNC);
write(file,buf3,sizeof(buf3));
int pid,cpid,cpid2;
pid=fork();
if(pid==0) cpid=getpid();
if(cpid==getpid()) printf("%dn",cpid);
if(cpid!=getpid()) pid=fork();
if(pid==0&&cpid!=getpid())cpid2=getpid();//.............
if(pid!=0)exit(0);
int i=0;
for(i=0;i