免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: wangfei007
打印 上一主题 下一主题

求救!!!子进程和父进程要交互的访问一个变量,怎么实现。 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2005-05-11 17:42 |只看该作者

求救!!!子进程和父进程要交互的访问一个变量,怎么实现。

#include <stdio.h>;
#include <stdlib.h>;
#include <unistd.h>;
#include<sys/types.h>;
#include<sys/time.h>;
#include<signal.h>;
#include<fcntl.h>;

#define THINKER_NUM 5

#define LEFT 0
#define RIGHT 1

#define USING 1
#define NO_USE 0

#define OTHER_STATUS 0
#define        THINKING_STATUS 1
#define        HUNGRY_STATUS 2
#define        EATING_STATUS 3


typedef struct think {
                      int u_id;
                      int u_status;
                     }Thinker;

void initial(Thinker thinkers[THINKER_NUM],int chop_status[THINKER_NUM])
{
int i;
for(i=0;i<THINKER_NUM;i++)
{
  thinkers.u_id=i;
  thinkers.u_status=THINKING_STATUS;
  chop_status=NO_USE;
}
}

void read_share(Thinker thinkers[THINKER_NUM],int chop_status[THINKER_NUM])
{
  int fd;
  int n;
  while (open("share.lock",O_RDONLY)!=-1);
  link("share.dat","share.lock";
  while((fd=open("share.dat",O_RDONLY))<=0)
  {
    perror("file open error in read_share \n";
    sleep(1);
  }
  n=sizeof(thinkers);
  if(read(fd,thinkers,n)<0)
    perror("file read error in read_share thinkers\n";
  n=sizeof(chop_status);
  if(read(fd,chop_status,n)<0)
    perror("file read error in read_share chop_static\n";
  close(fd);
  unlink("share.lock";
}

void write_share(Thinker thinkers[THINKER_NUM],int chop_status[THINKER_NUM])
{
  int fd;
  int n;
  while (open("share.lock",O_WRONLY)!=-1) ;
  link("share.dat","share.lock";
  while((fd=open("share.dat",O_WRONLY|O_TRUNC))<=0)
  {
    perror("file open error in write_share\n";
    sleep(1);
  }
  n=sizeof(thinkers);
  if(write(fd,thinkers,n)<n)
    perror("file write error in write_share thinkers\n";
  n=sizeof(chop_status);
  if(write(fd,chop_status,n)<n)
    perror("file write error in write_share chop_static\n";
  close(fd);
  unlink("share.lock";
}
  
void drop(int u,int first)
{
  void try(int );
  Thinker thinkers[THINKER_NUM];
  int chop_status[THINKER_NUM];
  read_share(thinkers,chop_status);
  if(first==RIGHT)
   chop_status[(u+1)%THINKER_NUM]==NO_USE;
  else
   chop_status[u%THINKER_NUM]==NO_USE;
  write_share(thinkers,chop_status);
  printf("now drop :%d:\n",u);
  sleep(3);
/*  try(u); */
  exit(0);
}
void reset_left(int u)
{
  void try(int );
  Thinker thinkers[THINKER_NUM];
  int chop_status[THINKER_NUM];
  printf("now in rest_left : %d :\n",u);
  read_share(thinkers,chop_status);
  chop_status[u%THINKER_NUM]==NO_USE;
  thinkers.u_status=THINKING_STATUS;
  write_share(thinkers,chop_status);
  printf("is thinking :%d:\n",u);
  sleep(5+rand()%5);   /*thinking */
/* try(u);  */
  exit(0);
  
}
  
  

void reset_right(int u)
{
  void reset_left(int );
  Thinker thinkers[THINKER_NUM];
  int chop_status[THINKER_NUM];
  read_share(thinkers,chop_status);
  printf("now reset right : %d :\n",u);
  chop_status[(u+1)%THINKER_NUM]==NO_USE;
  write_share(thinkers,chop_status);
  sleep(1);
  reset_left(u);
}
  
void second(int u,int first)
{
  void drop(int, int );
  void reset_right(int );
  Thinker thinkers[THINKER_NUM];
  int chop_status[THINKER_NUM];
  printf("now in second :%d:\n",u);
  read_share(thinkers,chop_status);
  if(first==RIGHT)
  {
    if(chop_status[u%THINKER_NUM]==NO_USE)
    {
     chop_status[u%THINKER_NUM]=USING;
     thinkers.u_status=EATING_STATUS;
     write_share(thinkers,chop_status);
     printf(":now process : %d: is eating\n",u);
     sleep(5);   /*the eating time    as to crit */
     thinkers.u_status=OTHER_STATUS;
     write_share(thinkers,chop_status);
     reset_right(u);
    }
    else if(chop_status[u%THINKER_NUM]==USING)
     drop(u,first);
  }
  else
  {
    if(chop_status[(u+1)%THINKER_NUM]==NO_USE)
    {
      chop_status[(u+1)%THINKER_NUM]==USING;
      thinkers.u_status=EATING_STATUS;
      write_share(thinkers,chop_status);
      printf("now process : %d : is eating \n",u);
      sleep(5);
      thinkers.u_status=OTHER_STATUS;
      write_share(thinkers,chop_status);
      reset_right(u);
    }
    else if(chop_status[(u+1)%THINKER_NUM]==USING)
     drop(u,first);
  }
}


void wait(int u,int first)
{
  void second(int, int);
  Thinker thinkers[THINKER_NUM];
  int chop_status[THINKER_NUM];
  if(first==RIGHT)
    printf("now in wait one :%d:  wait for right \n",u);
  else
   printf("now in wait one :%d: wait for left \n",u);
  BEG:
  read_share(thinkers,chop_status);
  if(first==RIGHT)
  {
    if(chop_status[(u+1)%THINKER_NUM]==NO_USE)
     {
       chop_status[(u+1)%THINKER_NUM]==USING;
       write_share(thinkers,chop_status);
       second(u,first);
     }
    else if(chop_status[(u+1)%THINKER_NUM]==USING)
   {  
     printf("chop now busy wait pid %d:  chopstick %d\n",u,(u+1)%THINKER_NUM);
     sleep(1);
     goto BEG;   
   }
  }
  else
  {
   if(chop_status[u%THINKER_NUM]==NO_USE)
     {
       chop_status[u%THINKER_NUM]==USING;
       write_share(thinkers,chop_status);
       second(u,first);
     }
   else if(chop_status[u%THINKER_NUM]==USING)
   {
    printf("chop now busy wait %d:   chopstatic  %d\n",u,u%THINKER_NUM);
    sleep(1);
    goto BEG;
  }
}
}
void try(int u)
{
  void wait(int,int );
  Thinker thinkers[THINKER_NUM];
  int chop_status[THINKER_NUM];
  int first;
  read_share(thinkers,chop_status);
  thinkers.u_status=HUNGRY_STATUS;
  write_share(thinkers,chop_status);
  if(rand()%2==1)
    first=RIGHT;
  else
    first=LEFT;
  printf("now try porcess: %d : in the first is %d\n",u,first);
  wait(u,first);
}
  
void LehmannRabin(int u)
{   
    void try(int u);
    Thinker thinkers[THINKER_NUM];
    int chop_status[THINKER_NUM];
    read_share(thinkers,chop_status);
    printf("now lehmannrabin :%d:\n",u);
    sleep(rand()%5);  
    thinkers.u_status=HUNGRY_STATUS;
    write_share(thinkers,chop_status);
    try(u);
}

int main(void)
{
  int i;
  pid_t pid;
  int fd;
  int n;  
  Thinker thinkers[THINKER_NUM];
  int chop_status[THINKER_NUM];
  initial(thinkers,chop_status);
  if((fd=open("share.dat",O_CREAT|O_TRUNC|O_WRONLY,0777))==0)
  {
    perror("file open error in main\n");
    exit(0);
  }

  n=sizeof(thinkers);
  if(write(fd,thinkers,n)<n)
    perror("write fifo error in main\n");

  n=sizeof(chop_status);
  if(write(fd,chop_status,n)<n)
    perror("write fifo error in main\n");
  close(fd);
  for(i=0;i<THINKER_NUM;i++)
  {
    pid=fork();
    if(pid==0)
    {
      srand((unsigned int)time(NULL));
      LehmannRabin(i);
   }
    else if(pid>;0)
    {
     sleep(1);
     continue;
    }
    else
     perror("fork error\n");
  }
exit(0);
}
                 
               
               
请各位指点

运行一段时间后 share.dat的数据就没有了
不知道怎么回事

论坛徽章:
0
12 [报告]
发表于 2005-05-11 17:46 |只看该作者

求救!!!子进程和父进程要交互的访问一个变量,怎么实现。

这个是根据《分布式算法》Nancy A.Lynch著  机械工业出版社
上的算法描叙写的
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP