- 论坛徽章:
- 0
|
求救!!!子进程和父进程要交互的访问一个变量,怎么实现。
#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的数据就没有了
不知道怎么回事 |
|