📄 os4.c
字号:
#include "os4head1.h"
int dcti=4;/*DCT现有数目*/
void help()
{
printf("\nhelpfile:");
printf("\n\th. helpfile.");
printf("\n\t1. add an equipment.");
printf("\n\t2. delete one equipment.");
printf("\n\t3. apply an equipment.");
printf("\n\t4. restitute an equipment.");
printf("\n\tQ. exit.\n");
}
int add()
{
char ch,s[20];
int i;
if (dcti >= 10)
return 0;
else
{
printf("enter type(I\\O):");
scanf("%1s",&ch);
if(ch=='i'||ch=='I')
{
SDT[dcti].type=1;
DCT[dcti].type=1;
}
else
{
SDT[dcti].type=0;
DCT[dcti].type=0;
}
printf("\nenter signing:");
scanf("%s",s);getchar();
strcpy(SDT[dcti].signing,s);
strcpy(DCT[dcti].deviceid,s);
SDT[dcti].next=&DCT[dcti];
SDT[dcti].driveradd=rand()%2000;
DCT[dcti].state=0;
printf("\nchoose coct 1.IN1 2.IN2 3.OUT1 4.OUT2 :");
scanf("%d",&i);getchar();
DCT[dcti].next=&COCT[i-1];
DCT[dcti].time=10;/**needn't**/
DCT[dcti].head=NULL;
}
dcti++;
}
int del()
{
char str[22];
int i=0;
printf("enter signing to delete:");
scanf("%s",str);
while (i<dcti)
{
if(strcmp(SDT[i].signing,str)==0)
break;
i++;
}
if(strcmp(SDT[i].signing,str)==0)
{
if (DCT[i].state==1)
return NULL;
while (i<dcti-1)
{
SDT[i]=SDT[i+1];
DCT[i]=DCT[i+1];
i++;
};dcti--;
}
else
;
}
int apply()
{
char s[22];
int i=0;
processnode * p,*q;
printf("\nenter the processname:");
scanf("%s",s);
p=malloc(sizeof(processnode));
p->next=NULL;
strcpy(p->name,s);
printf("choose device <signing>: ");
while (i<dcti)
{
printf("\n\t%s",SDT[i].signing);
i++;
}
printf("\n");
scanf("%s",s);
i=0;
while (i< dcti && strcmp(DCT[i].deviceid,s)!=0)
{
i++;
}
if (strcmp(DCT[i].deviceid,s)==0)
{
if(DCT[i].state==0)
{
DCT[i].state=1;
if (DCT[i].next->state==1)
{
if(DCT[i].next->head==NULL)
{
DCT[i].next->head=p;
DCT[i].next->end=p;
return 1;
}
else
{
q=DCT[i].next->head;
while ( q->next!=NULL)
q=q->next;
q->next=p;
DCT[i].next->end=p;
return 1;
}
}
else
{
DCT[i].next->state=1;
return 1;
}
}
if (DCT[i].head==NULL)
DCT[i].head=p;
else
{
q=DCT[i].head;
while ( q->next!=NULL)
q=q->next;
q->next=p;
}
printf("");
}
else printf("apply device name wrong.\n");
}
int restitute()
{
char s[22];
int i=0,j=0,flag=0;
processnode * p,*q;
printf("enter the device signing:");
scanf("%s",s);
while (i<dcti && strcmp(DCT[i].deviceid,s)!=0)
i++;
if(strcmp(DCT[i].deviceid,s)==0)
{
if(DCT[i].head==NULL)
{
DCT[i].state=0;
j=0;
while(j<dcti)
{
if(strcmp(DCT[j].next->controllerid,DCT[i].next->controllerid)==0)
flag++;
j++;
}
if(flag>=2)
{
if(DCT[i].next->head==NULL)
DCT[i].next->state=0;
return 1;
}
else/*通道只有一个设备*/
{
if(DCT[i].next->head!=NULL)
/*通道队列不空*/
{
if(DCT[i].next->head==DCT[i].next->end)
{ DCT[i].next->head=DCT[i].next->end=NULL;return 1;}
else
{
p=DCT[i].next->head;
DCT[i].next->head=p->next;
free(p);
return 1;
}
}
else
{
DCT[i].next->state=0;
return 1;
}
}
}
else
{
p=DCT[i].head;
DCT[i].head=p->next;
free(p);
/*有等待进程*/
}
}
else printf("restitute. device name wrong.");
}
show()
{
int i=0;
processnode *p;
printf("\nSDT:");
while (i<dcti)
{
printf("\n\t%s",SDT[i].signing);
i++;
}
printf("\nDCT type state COCT waitingDEVICElist:");
i=0;
while (i<dcti)
{
printf("\n%-10s",SDT[i].signing);
if (DCT[i].type==1)
{
printf("%4s","I");
}
else
{
printf("%4s","O");
}
printf("%4d",DCT[i].state);
printf("%10s",DCT[i].next->controllerid);
p=DCT[i].head;
while (p!=NULL)
{
printf("%8s",p->name);
p=p->next;
}
i++;
}
printf("\nCOCT state waintinglist:");
i=0;
while (i<4)
{
printf("\n%-10s %4d",COCT[i].controllerid,COCT[i].state);
p=COCT[i].head;
while (p!=NULL)
{
printf("%8s",p->name);
p=p->next;
}
i++;
}
}
int main()
{
char command;
SDT[0].type=1;
strcpy(SDT[0].signing,"mouse");
SDT[0].next=&DCT[0];
SDT[0].driveradd=rand()%2000;
DCT[0].type=1;
strcpy(DCT[0].deviceid,"mouse");
DCT[0].state=0;
DCT[0].next=&COCT[0];
DCT[0].time=10;/**needn't**/
DCT[0].head=NULL;
/******************sdt0dct0***********************/
SDT[1].type=1;
strcpy(SDT[1].signing,"keyboard");
SDT[1].next=&DCT[1];
SDT[1].driveradd=rand()%2000;
DCT[1].type=1;
strcpy(DCT[1].deviceid,"keyboard");
DCT[1].state=0;
DCT[1].next=&COCT[0];
DCT[1].time=10;/**needn't**/
DCT[1].head=NULL;
/***************************dct1******************************/
SDT[2].type=0;
strcpy(SDT[2].signing,"display");
SDT[2].next=&DCT[2];
SDT[2].driveradd=rand()%2000;
DCT[2].type=0;
strcpy(DCT[2].deviceid,"display");
DCT[2].state=0;
DCT[2].next=&COCT[2];
DCT[2].time=10;/**needn't**/
DCT[2].head=NULL;
/***************************dct2******************************/
SDT[3].type=0;
strcpy(SDT[3].signing,"soundbox");
SDT[3].next=&DCT[3];
SDT[3].driveradd=rand()%2000;
DCT[3].type=0;
strcpy(DCT[3].deviceid,"soundbox");
DCT[3].state=0;
DCT[3].next=&COCT[2];
DCT[3].time=10;/**needn't**/
DCT[3].head=NULL;
/***************************dct3******************************/
help();
while(1)
{
clrscr();
show();
help();
printf("\n-");
scanf("%c",&command);
switch (command)
{
case '1':
add();
break;
case '2':
del();
break;
case '3':
apply();
break;
case '4':
restitute();
break;
case 'q':
case 'Q':
exit(0);
break;
case 'h':
help();
break;
default :
break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -