|
发表于 2002-11-11 21:30:13
|
显示全部楼层
我把程序整理了一下,总算能通过编译,但是还有很多错误。
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
pid_t p1,p2,p3,p4;
int Randomnum(void);
int main(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]={{"p1",3,0,0},{"p2",2,0,0},{"p3",2,0,0},{"p4",4,0,0}};
int sysgiven = 5;
Randomnum();
if(p1<0) {
printf(" fork error\n ");
exit(EXIT_FAILURE);
}
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[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(EXIT_SUCCESS);
}
else
printf(" Dead \n");
}
Randomnum();
if(p2<0) {
printf(" fork error\n " );
exit(EXIT_FAILURE);
}
else if((p2==0) || (p2==1)) {
if((capro[1].renum <= sysgiven) && (capro[1].nogiven <= 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(EXIT_SUCCESS);
}
else
printf(" Dead \n");
}
Randomnum();
if( p3<0 ) {
printf(" fork error\n " );
exit(EXIT_FAILURE);
}
else if((p3==0) || (p3==1)) {
if((capro[2].renum <= sysgiven) && (capro[2].nogiven <= 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(EXIT_SUCCESS);
}
else
printf(" Dead \n");
}
Randomnum();
if( p4<0 ) {
printf(" fork error\n " );
exit(EXIT_FAILURE);
}
else if((p4==0) || (p4==1)) {
if((capro[3].renum <= sysgiven) && (capro[3].nogiven <= 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(EXIT_SUCCESS);
}
else
printf(" Dead \n");
}
}
int Randomnum(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;
} |
|