|
楼主 |
发表于 2002-11-12 10:37:13
|
显示全部楼层
没什么 ! 我也很菜的 !
你看看下面的程序:
#include<sys/types.h>
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
void Randomnum(void);
pid_t p1,p2,p3,p4;
main(void)
{
int sysgiven=5;
struct stares1{ int s; char r1[11]; } statue1={0,"ABCDEFGHIJ"};
struct stares2{ int s; char r2[11]; } statue2={0,"KLMNOPQRST"};
struct stares3{ int s; char r3[11]; } statue3={0,"UVWXYZabcd"};
struct stares4{ int s; char r4[11]; } statue4={0,"efghijklmn"};
struct stares5{ int s; char r5[11]; } statue5={0,"opqrstuvwx"};
struct pidstatues{
int name;
int renum;
int given;
int nogiven;
}capro[4]; /* define struct over */
/* initial data struct */
capro[0].name=1; capro[0].renum =3; capro[0].given=0; capro[0].nogiven=0;
capro[1].name=2; capro[1].renum =2; capro[1].given=0; capro[1].nogiven=0;
capro[2].name=3; capro[2].renum =2; capro[2].given=0; capro[2].nogiven=0;
capro[3].name=4; capro[3].renum =4; capro[3].given=0; capro[3].nogiven=0;
Randomnum();
if(p1<0) {
printf(" fork error\n "); exit(0); }
else if( (p1==0) || (p1==1) )
{
if((capro[0].renum <= sysgiven) && (capro[0].nogiven<= sysgiven))
{
if((statue1.s==0)&&(capro[0].given<3)) { capro[0].given++;
sysgiven--; printf("%s\n",statue1.r1); statue1.s = 1; }
if((statue2.s==0)&&(capro[0].given<3)) { capro[0].given++;
sysgiven--; printf("%s\n",statue2.r2); statue2.s = 1; }
if((statue3.s==0)&&(capro[0].given<3)) { capro[0].given++;
sysgiven--; printf("%s\n",statue3.r3); statue3.s = 1; }
if((statue4.s==0)&&(capro[0].given<3)) { capro[0].given++;
sysgiven--; printf("%s\n",statue4.r4); statue4.s = 1; }
if((statue5.s==0)&&(capro[0].given<3)) { capro[0].given++;
sysgiven--; printf("%s\n",statue5.r5); statue5.s = 1; }
sysgiven = 5; exit(1); }
else printf(" Dead \n");
}
if(p2<0)
{ printf(" fork fault\n " ); exit(1); }
else if((p2==0) || (p2==1))
{
if((capro[1].renum <= sysgiven) && (capro[0].nogiven<= sysgiven))
{
if((statue1.s==0)&&(capro[1].given<3)) { capro[1].given++;
sysgiven--; printf("%s\n",statue1.r1); statue1.s = 1; }
if((statue2.s==0)&&(capro[1].given<3)) { capro[1].given++;
sysgiven--; printf("%s\n",statue2.r2); statue2.s = 1; }
if((statue3.s==0)&&(capro[1].given<3)) { capro[1].given++;
sysgiven--; printf("%s\n",statue3.r3); statue3.s = 1; }
if((statue4.s==0)&&(capro[1].given<3)) { capro[1].given++;
sysgiven--; printf("%s\n",statue4.r4); statue4.s = 1; }
if((statue5.s==0)&&(capro[1].given<3)) { capro[1].given++;
sysgiven--; printf("%s\n",statue5.r5); statue5.s = 1; }
sysgiven = 5; exit(1);
}
else printf(" Dead \n");
}
if( p3<0 ) {
printf(" fork error\n " ); exit(1);}
else if((p3==0) || (p3==1))
{
if((capro[2].renum <= sysgiven) && (capro[0].nogiven <= sysgiven))
{
if((statue1.s==0)&&(capro[2].given<3)) { capro[2].given++;
sysgiven--; printf("%s\n",statue1.r1); statue1.s = 1; }
if((statue2.s==0)&&(capro[2].given<3)) { capro[2].given++;
sysgiven--; printf("%s\n",statue2.r2); statue2.s = 1; }
if((statue3.s==0)&&(capro[2].given<3)) { capro[2].given++;
sysgiven--; printf("%s\n",statue3.r3); statue3.s = 1; }
if((statue4.s==0)&&(capro[2].given<3)) { capro[2].given++;
sysgiven--; printf("%s\n",statue4.r4); statue4.s = 1; }
if((statue5.s==0)&&(capro[2].given<3)) { capro[2].given++;
sysgiven--; printf("%s\n",statue5.r5); statue5.s = 1; }
sysgiven = 5; exit(1); }
else printf(" Dead \n"); }
if( p4<0 ) {
printf(" fork error\n " ); exit(1); }
else if((p4==0) || (p4==1))
{
if((capro[3].renum <= sysgiven) && (capro[0].nogiven <= sysgiven))
{
if((statue1.s==0)&&(capro[3].given<3)) { capro[3].given++;
sysgiven--; printf("%s\n",statue1.r1); statue1.s = 1; }
if((statue2.s==0)&&(capro[3].given<3)) { capro[3].given++;
sysgiven--; printf("%s\n",statue2.r2); statue2.s = 1; }
if((statue3.s==0)&&(capro[3].given<3)) { capro[3].given++;
sysgiven--; printf("%s\n",statue3.r3); statue3.s = 1; }
if((statue4.s==0)&&(capro[3].given<3)) { capro[3].given++;
sysgiven--; printf("%s\n",statue4.r4); statue4.s = 1; }
if((statue5.s==0)&&(capro[3].given<3)) { capro[3].given++;
sysgiven--; printf("%s\n",statue5.r5); statue5.s = 1; }
sysgiven = 5; exit(1); }
else printf(" Dead \n"); }
}
void Randomnum(void)
{
int i;
i = random();
i = i%4;
switch(i){
case 0 : p1 = fork();
case 1 : p2 = fork();
case 2 : p3 = fork();
case 3 : p4 = fork();
default: printf("fork fault\n"); break;
} |
|