?? pv.cpp
字號:
#include "basic.h"
semphore sem[5];
pnode * pr[20];
//wait operate
void wait(char * sname,int pid)
{
int fflag,pflag;
pnode *p,*p1;
semphore *s;
fflag=0;
pflag=0;
for(int i=0;i<5;i++)
if(!strcmp(sem[i].name,sname))
{
s=&sem[i];
fflag=1;
break;
}
for(i=0;i<20;i++)
if(pr[i]->node->pid == pid)
{
p1 = pr[i];
pflag=1;
break;
}
if(!fflag)
{
printf("the semphore '%s' is not exist!\n",sname);
return;
}
if(!pflag)
{
printf("the process '%d' is not exist!\n",pid);
return;
}
s->count--;
if(s->count>=0)
s->curpid = p1->node->pid;
else
{
if(s->wlist)
{
for(p=s->wlist;p->next;p=p->next);
p->next=p1;
}
else
s->wlist=p1;
}
}
//signal operate
void signal(char *sname)
{
int fflag=0;
for(int i=0;i<5;i++)
if(!strcmp(sem[i].name,sname))
{
fflag=1;
break;
}
if(fflag)
{
sem[i].count++;
if(sem[i].wlist)
{
sem[i].curpid = sem[i].wlist->node->pid;
sem[i].wlist = sem[i].wlist->next;
}
}
else
printf("the semphore '%s' is not exist!\n",sname);
}
//show semphore infomation
void showdetail()
{
int i;
pnode *p;
printf("\n");
for(i=0;i<5;i++)
{
if(sem[i].count<=0)
{
printf("%s (curp %d): ",sem[i].name,sem[i].curpid);
p=sem[i].wlist;
while(p)
{
printf("%5d",p->node->pid);
p=p->next;
}
}
else
printf("%s : ",sem[i].name);
printf("\n");
}
}
/*********************************************************************************/
/* don't change */
void init()
{
strcat(sem[0].name,"s0");
strcat(sem[1].name,"s1");
strcat(sem[2].name,"s2");
strcat(sem[3].name,"s3");
strcat(sem[4].name,"s4");
for(int i=0;i<5;i++)
{
sem[i].wlist=NULL;
sem[i].count=1;
}
for(i=0;i<20;i++)
{
pr[i] = new pnode;
pr[i]->node=new pcb;
pr[i]->node->pid=i;
pr[i]->brother=NULL;
pr[i]->next=NULL;
pr[i]->sub=NULL;
}
}
void main()
{
short cflag,pflag;
char cmdstr[32];
char *s,*s1,*s2;
initerror();
init();
for(;;)
{
cflag=0;
pflag=0;
printf("cmd:");
scanf("%s",cmdstr);
if(!strcmp(cmdstr,"exit")) //exit the programe
break;
if(!strcmp(cmdstr,"showdetail"))
{
cflag = 1;
pflag = 1;
showdetail();
}
else
{
s = strstr(cmdstr,"wait"); //create process
if(s)
{
cflag=1;
//getparameter
s1 = substr(s,instr(s,'(')+1,instr(s,',')-1);
s2 = substr(s,instr(s,',')+1,instr(s,')')-1);
if(s1 && s2)
{
wait(s1,atoi(s2));
pflag=1;
}
}
else
{
s=strstr(cmdstr,"signal");//delete process
if(s)
{
cflag=1;
s1 = substr(s,instr(s,'(')+1,instr(s,')')-1);
if(s1)
{
signal(s1);
pflag=1;
}
}
}
}
if(!cflag)
geterror(0);
else if(!pflag)
geterror(1);
}
}
/*****************************************************************************/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -