首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 操作系统 > UNIXLINUX >

UNIX 新手一问 关于fork 和 2进程同时写一文件出现的有关问题

2012-02-28 
UNIX 新手一问 关于fork 和 2进程同时写一文件出现的问题下面一段程序运行会死!请问是什么原因呢?#include

UNIX 新手一问 关于fork 和 2进程同时写一文件出现的问题
下面一段程序运行会死!   请问是什么原因呢?
#include   <sys/types.h>
#include   <sys/stat.h>
#include   <fcntl.h>
#include   <stdio.h>
#include   <unistd.h>

typedef   int   XFILE;
typedef   enum   _BOOL{FALSE,TRUE}   BOOL;
BOOL   IsFileCanbeSeek(XFILE   file);

const   char   buf1[]= "abcdefghijklmnopqrstuvwxyz\n ";
const   char   buf2[]= "ABCDEFGHIJKLMNOPQRSTUVWXYZ\n ";
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( "%d\n ",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 <20;i++)
                  {
                  printf( "%d-------%d---------%d\n ",getpid(),cpid,cpid2);
                  if(cpid==getpid())  
                  {          
                  sprintf(buf4, "%d\t ",count);
                  count++;
                  write(file,buf4,sizeof(buf4));
                  write(file,buf1,sizeof(buf1));
                  sleep(1);
                  }
                    if(cpid2==getpid())
                  {
                  sprintf(buf4, "%d\t ",count);
                  count++;
                  write(file,buf4,sizeof(buf4));
                  write(file,buf2,sizeof(buf1));
                  sleep(1);
                  }
                  }
close(file);
}


[解决办法]
执行没问题.

不过你fork那逻辑太混乱了,还有点问题.

如果你那死掉的话,可能是cpid,cpid2的初值的问题.赋初值再试一下.

[root@shwhg test]# 4538-------4538---------134518836
4539-------1321517044---------4539


4538-------4538---------134518836
4539-------1321517044---------4539
4538-------4538---------134518836
4539-------1321517044---------4539
4538-------4538---------134518836
4539-------1321517044---------4539
4538-------4538---------134518836
4539-------1321517044---------4539
4538-------4538---------134518836
4539-------1321517044---------4539
4538-------4538---------134518836
4539-------1321517044---------4539
4538-------4538---------134518836
4539-------1321517044---------4539
4538-------4538---------134518836
4539-------1321517044---------4539
4538-------4538---------134518836
4539-------1321517044---------4539
4538-------4538---------134518836
4539-------1321517044---------4539
4538-------4538---------134518836
4539-------1321517044---------4539
4538-------4538---------134518836
4539-------1321517044---------4539
4538-------4538---------134518836
4539-------1321517044---------4539
4538-------4538---------134518836
4539-------1321517044---------4539
4538-------4538---------134518836
4539-------1321517044---------4539
4538-------4538---------134518836
4539-------1321517044---------4539
4538-------4538---------134518836
4539-------1321517044---------4539
4538-------4538---------134518836
4539-------1321517044---------4539

[root@shwhg test]#

热点排行