📄 sun.cpp
字号:
pcb[exe].priority--;
printf(" **********pcb[%d].priority=%d*********\n",pcb[exe].identify,pcb[exe].priority);
return;
}
int v (int se, char ad)
{
if(++sem[se].value > 0)//判断(信号量)临界区是否可进入
return (FALSE); //不可,则继续阻塞
wakeup(se); //可,则唤醒
savearea[exe][0]=i;//记录程序执行到的地方
savearea[exe][1]=ad;//记录时间片用完时程序所处的状态
return (TRUE); /* scheduler */
//开始执行
}
void wakeup (int se)
{
int w;
w=sem[se].firstwr;
//给'等待链首指针(指示该信号量上等待的第一个进程的标识数)'赋值
if(w != NIL)
{
sem[se].firstwr=pcb[w].nextwr;
//赋'等待链指针(指示在同一信号量上等待的下一个进程的标识数)'值\
///给'等待链首指针(指示该信号量上等待的第一个进程的标识数)'
pcb[w].status='r';//状态变为低就绪
printf("\tProcess %d is waken up.\n",pcb[w].identify);
}
}
void process ()
{
if(addr == 'a') goto a1;
if(addr == 'b') goto b1;
if(addr == 'c') goto c1;
if(addr == 'd') goto d1;
if(addr == 'e') goto e1;
if(addr == 'f') goto f1;
for(i=1; i<6; i++)
{
printf("\tprocess%d calls P on the semaphore 1.\n",pcb[exe].identify);
time_silk();
if(p(0,'a')) break; /* Process1 is blocked */
a1:
printf("\tprocess%d is executing in the cretical section 1.\n",pcb[exe].identify);
time_silk();
if(timeint('b')) break; /* Time silce interrupt */
b1:
printf(" s1=%d\n",++s1);
printf("\tprocess%d calls V on semaphore 1 and quit cetical section 1.\n",pcb[exe].identify);
time_silk();
if(v(0,'c')) break; /* Wake up a blocked process */
c1:
printf("\tprocess%d calls P on semaphorel 2.\n",pcb[exe].identify);
time_silk();
if(p(1,'d')) break;
d1:
printf("\tprocess%d is executing cretical section 2.\n",pcb[exe].identify);
time_silk();
if(timeint('e')) break;
e1:
printf(" s2=%d\n",++s2);
printf("\tprocess%d calls V on semaphore 2 and quit cretical section 2.\n",pcb[exe].identify);
time_silk();
if(v(1,'f')) break; /* Wake up a block process */
f1:
printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("\tprocess%d cycle count =%d\n",pcb[exe].identify,i);
printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
}
if(i < 6)
return;
eexit(exe);
}
void eexit (int n)
{
pcb[n].status='c';//PCB状态变为终止态
printf("Process %d is completed!\n",pcb[n].identify);
deleted(n);
exe=NIL;//记录当前正在运行的进程为无
}
void main ()
{
int k;
printf("********* Process Management **********\n\n");
init();
printf("s1=%d, s2=%d\n",s1,s2);
printf("Process1,Process2,Process3 are all in ready !\n");
for( ; ; )
if((k=scheduler()) != NIL)
process();
else
break;
printf(" \ts1=%d, s2=%d\n",s1,s2);
printf("\n ********* END **********\n");
} /* end */
void deleted(int n)
{
pcb[n].flags=UNUSE;//撤消PCB
count[n]=0; //***修改'可用PCB索引'***
count[MAX-1]++; //*****
wentdown[cx]=pcb[n].identify;//***记录被撤消进程***
cx++; //*****
box[n].boxflag=UNUSE;
printf("process%d has been deleted!",pcb[n].identify);
}
void create()
{
int rep;
int pri;
for(int j=0;j<MAX;j++)
{
if(count[j]==0)
{
rep=j;
break;
}
}
pcb[rep].id =rep; //给进程身份(如:1,2,3...)赋值
pcb[rep].status ='r';//进程状态(可为E、R、T、W和C)
//R -- 高就绪态;
pcb[rep].nextwr =NIL;//等待链指针,指示在同一信号量上等待的下一个进程的标识数。
printf("Process %d priority?",remark);//***输入各进程优先级***
scanf("%d",&pri);
pcb[rep].priority=pri; //***
pcb[rep].identify=remark;//外部标识符
pcb[rep].flags=USE; //标识PCB已创建
box[rep].boxflag=USE;
count[rep]=1; //***修改'可用PCB索引'***
count[MAX]--; //*****
remark++; //修改'外部标识符的标识数'的记录
for(j=0; j<4; j++) //***恢复初始态***
savearea[rep][j]='0'; //******
}
void analyse()
{
char answer;//用于记录"外存是否还有作业需创建进程"的响应
printf("\nDo you want to create a new process?(Y/N)");
TRY:cin>>answer;//取得响应值
if(answer=='y'||answer=='Y')
create();
else if(answer=='n'||answer=='N')
return;
else
{
printf("\nThe answer is wrong!Try again...");
goto TRY;
}
int pcb_use_count=0; //用于寻找可用PCB数量
for(int pcbf=0;pcbf<MAX;pcbf++) //*****寻找可用PCB数量*****
{
if(pcb[pcbf].flags==UNUSE)
pcb_use_count++;
} //*****************
if(pcb_use_count!=0)
{
printf("\nDo you want create another onr?answer:");
goto TRY;
}
}
void call_time()//调用时间函数
{
/*unsigned int t,s,k;
for(t=0;t<6000;t++)
for(s=0;s<6000;s++)
for(k=0;k<20;k++);*/
}
void time_silk()//时间片函数
{
/*unsigned int t,s,k;
for(t=0;t<11;t++)
for(s=0;s<6000;s++)
for(k=0;k<6000;k++);*/
}
void inform()
{
char require;
printf("\tDo you want to send message?");
TRY: cin>>require;//取得响应值
if(require=='y'||require=='Y')//*********答案***********
{
int geter;
int number;
printf("\t\tWho will recive the message? prosess");
BACK:cin>>geter;
int policeman=-1; //***检验geter是否被撤消***
for(int pi=0;pi<MAX;pi++)
if(wentdown[pi]==geter)
policeman=1; //******
if((geter>=remark)||(policeman==1)) //***检验进程是否存在***
{
printf("\t\t!!!^^WARNING:^^!!!\n");
printf("\t\t this process not exist! please check and input again:process");
goto BACK;
} //******
printf("\t\tHow many messages will be gave?");
cin>>number;
for(int mi=0;mi<MAX;mi++) //***^^^发送消息^^^***
{
if(pcb[mi].identify==geter)
geter=pcb[mi].id;
}
if(box[geter].boxflag==USE)
//*****判断信箱是否为空*****
if(box[geter].mutex==USE) //是,发送
{
box[geter].mutex=UNUSE;
box[geter].info_num=number;
box[geter].comer=pcb[exe].id;
printf("\t\t$@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@$\n");
printf("\t\t SUCCEESS!!! WONDERFUL!!!\n");
printf("\t\t$@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@$\n");
}
else //否,失败
{
printf("\t\t$@@@@@@@@@@@@@@@@@@@@@@@@@@@@$\n");
printf("\t\t FAILED!!! GO ON!!!\n");
printf("\t\t$@@@@@@@@@@@@@@@@@@@@@@@@@@@@$\n");
return;
}
}
else if(require=='n'||require=='N')//**********答案************
return;
else //**********答案************
{
printf("\nThe require is wrong!Try again...");
goto TRY;
}
//***^^^ ^^^***
}
void recive()
{
if(box[exe].mutex==UNUSE)
{
printf("\t$^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^$\n");
printf("\t I have recive process%d's messages!!! messages'number is%d.\n",
pcb[box[exe].comer].identify,box[exe].info_num);
printf("\t$^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^$\n");
box[exe].mutex=USE; //清空信箱
}
else
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -