|
楼主 |
发表于 2002-11-10 22:39:23
|
显示全部楼层
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
pid_t p1,p2,p3,p4;
int main(void){
int Randomnum(void);
struct stares1{ int s; char r1[10]; }statue1={"0","ABCDEFGHIJ"};
struct stares2{ int s; char r2[10]; }statue2={"0","KLMNOPQRST"};
struct stares3{ int s; char r3[10]; }statue3={'0","UVWXYZabcd"};
struct stares4{ int s; char r4[10]; }statue4={"0","efghijklmn"};
struct stares5{ int s; char r5[10]; }statue5={"0","opqrstuvwx"};
struct pidstatue{
char name[2];
int renum;
int given;
int nogiven; } * capro[4];
capro[0]->name[2] = 'p1'; capro[0]->renum = 3;
capro[1]->name[2] = 'p2'; capro[1]->renum = 2;
capro[2]->name[2] = 'p3'; capro[2]->renum = 2;
capro[3]->name[2] = 'p4'; capro[3]->renum = 4;
int sysgiven = 5;
Randomnum();
if(p1<0) {
printf(" fork error\n "); exit(); }
else if( (p1==0) || (p1==1) )
{if((capro[0]->renum <= sysgiven) && (capro->nongive <= sysgiven))
{ if((statue1->s=0)&&(capro[0]->given<3)) { capro[0]->given++;
sysgiven--; printf("%s\n",statue1->r1[10]); statue1->s = 1; }
if((statue2->s=0)&&(capro[0]->given<3)) { capro[0]->given++;
sysgiven--; printf("%s\n",statue2->r2[10]); statue2->s = 1; }
if((statue3->s=0)&&(capro[0]->given<3)) { capro[0]->given++;
sysgiven--; printf("%s\n",statue3->r3[10]); statue3->s = 1; }
if((statue4->s=0)&&(capro[0]->given<3)) { capro[0]->given++;
sysgiven-- printf("%s\n",statue4->r4[10]); statue4->s = 1; }
if((statue5->s=0)&&(capro[0]->given<3)) { capro[0]->given++;
sysgiven-- printf("%s\n",statue5->r5[10]); statue5->s = 1 ; }
sysgiven = 5; exit();
} else printf(" Dead \n"); }
Randomnum();
if(p2<0) { printf(" fork error\n " ); exit(1); }
else if((p2==0) || (p2==1))
{ if((capro[1]->renum <= sysgiven) && (capro->nongive <= sysgiven))
{ if((statue1->s=0)&&(capro[1]->given<3)) { capro[1]->given++;
sysgiven--; printf("%s\n",statue1->r1[10]); statue1->s = 1; }
if((statue2->s=0)&&(capro[1]->given<3)) { capro[1]->given++;
sysgiven--; printf("%s\n",statue2->r2[10]); statue2->s = 1; }
if((statue3->s=0)&&(capro[1]->given<3)) { capro[1]->given++;
sysgiven--; printf("%s\n",statue3->r3[10]); statue3->s = 1; }
if((statue4->s=0)&&(capro[1]->given<3)) { capro[1]->given++;
sysgiven-- printf("%s\n",statue4->r4[10]); statue4->s = 1; }
if((statue5->s=0)&&(capro[1]->given<3)) { capro[1]->given++;
sysgiven-- printf("%s\n",statue5->r5[10]); statue5->s = 1 ; }
sysgiven = 5; exit(); } else printf(" Dead \n"); }
Randomnum();
if( p3<0 ) { printf(" fork error\n " ); exit(1); }
else if((p3==0) || (p3==1))
{ if((capro[2]->renum <= sysgiven) && (capro->nongive <= sysgiven))
{ if((statue1->s=0)&&(capro[2]->given<3)){ capro[2]->given++;
sysgiven--; printf("%s\n",statue1->r1[10]); statue1->s = 1; }
if((statue2->s=0)&&(capro[2]->given<3)) { capro[2]->given++;
sysgiven--; printf("%s\n",statue2->r2[10]); statue2->s = 1; }
if((statue3->s=0)&&(capro[2]->given<3)) { capro[2]->given++;
sysgiven--; printf("%s\n",statue3->r3[10]); statue3->s = 1; }
if((statue4->s=0)&&(capro[2]->given<3)) { capro[2]->given++;
sysgiven-- printf("%s\n",statue4->r4[10]); statue4->s = 1; }
if((statue5->s=0)&&(capro[2]->given<3)) { capro[2]->given++;
sysgiven-- printf("%s\n",statue5->r5[10]); statue5->s = 1 ; }
sysgiven = 5; exit(); }
else printf(" Dead \n"); }
Randomnum();
if( p4<0 ) { printf(" fork error\n " ); exit(1); }
else if((p4==0) || (p4==1))
{ if((capro[3]->renum <= sysgiven) && (capro->nongive <= sysgiven))
{ if((statue1->s=0)&&(capro[3]->given<3)) { capro[3]->given++;
sysgiven--; printf("%s\n",statue1->r1[10]); statue1->s = 1; }
if((statue2->s=0)&&(capro[3]->given<3)) { capro[3]->given++;
sysgiven--; printf("%s\n",statue2->r2[10]); statue2->s = 1; }
if((statue3->s=0)&&(capro[3]->given<3)) { capro[3]->given++;
sysgiven--; printf("%s\n",statue3->r3[10]); statue3->s = 1; }
if((statue4->s=0)&&(capro[3]->given<3)) { capro[3]->given++;
sysgiven-- printf("%s\n",statue4->r4[10]); statue4->s = 1; }
if((statue5->s=0)&&(capro[3]->given<3)) { capro[3]->given++;
sysgiven-- printf("%s\n",statue5->r5[10]); statue5->s = 1 ; }
sysgiven = 5; exit(); } else printf(" Dead \n"); } }
int Radomnum(void)
{ int i;
i = random();
i = i%4;
switch(i){
case 0 : p1 = fork(); break;
case 1 : p2 = fork(); break;
case 2 : p3 = fork(); break;
case 3 : p4 = fork(); break;
} return; } |
|