进程撤销模拟实现

进程撤销模拟实现

main.cpp

#include <iostream>
#include"basic.h"
pnode *proot;
pnode *plink;

int createpc(int *para){
    pnode *p,*p1,*pp;
    int pflag;
    pflag=0;
    for(p=plink;p;p=p->next){
        if(p->node->pid==para[0]){
            printf("pid %d is already exist!\n",para[0]);
            return -1;
        }
        if(p->node->pid==para[1]){
            pflag=1;
            pp=p;
        }
    }
    if(!pflag){
        printf("parent id %d is not exist!\n",para[1]);
        return -2;
    }
    p1=new pnode;
    p1->node=new pcb;
    p1->node->pid=para[0];
    p1->node->ppid=para[1];
    p1->node->prio=para[2];
    p1->sub=NULL;
    p1->next=NULL;
    p1->brother=NULL;
    
    if(!pp->sub){
        pp->sub=p1;
    }else{
        for(p=pp->sub;p->brother;p=p->brother);
        p->brother=p1;
    }
    for(p=plink;p->next;p=p->next);
    p->next=p1;
    return 0;
}

void showdetail(){
    pnode *p,*p1;
    p=plink;
    for(;p;){
        printf("%d(prio %d)    :",p->node->pid,p->node->prio);
        p1=p->sub;
        for(;p1;){
            printf("%d(prio %d)     ",p1->node->pid,p1->node->prio);
            p1=p1->brother;
        }
        printf("\n");
        p=p->next;
    }
    printf("\n");
}

void droppc(int a){
    pnode *p,*p1,*p2;
    p=plink;
    for(;p;){
        p1=p->sub;
        if(p1&&p1->node->pid==a){
            p2=p1->sub;
            for(;p2;){
                droppc(p2->node->pid);
                p2=p2->brother;
            }
            if(p1->brother){
                p->sub=p1->brother;
            }else{
                p->sub=NULL;
            }
        }
        else{
        for(;p1;){
            if(p1->brother&&p1->brother->node->pid==a){
                p2=p1->brother->sub;
                for(;p2;){
                droppc(p2->node->pid);
                p2=p2->brother;
                }
                if(p1->brother->brother){
                    pnode *bro;
                    bro=p1->brother->brother;
                    p1->brother=bro;
                }else{
                    p1->brother=NULL;
                }
            }
            p1=p1->brother;
        }
        }
        if(p->next&&p->next->node->pid==a){
            if(p->next->next){
                p->next=p->next->next;
            }else{
                p->next=NULL;
            }
        }
        p=p->next;
    }
}

int main(){
    initerror();
    short cflag,pflag;
    char cmdstr[32];
    proot=new pnode;
    proot->node=new pcb;
    proot->node->pid=0;
    proot->node->ppid=-1;
    proot->node->prio=0;
    proot->next=NULL;
    proot->sub=NULL;
    proot->brother=NULL;
    plink=proot;
    for(;;){
        cflag=0,pflag=0;
        printf("cmd:");
        scanf("%s",cmdstr);
        if(!strcmp(cmdstr,"exit")) break;
        if(!strcmp(cmdstr,"showdetail")){
            cflag=1;
            pflag=1;
            showdetail();
        }
        else{
            int *para;
            char *s,*s1,*s2;
            s=strstr(cmdstr,"createpc");
            s2=strstr(cmdstr,"droppc");
            if(s){
            cflag=1;
            para=(int*)malloc(3);
            s1=substr(s,instr(s,'(')+1,strlen(s)-2);
            para=strtoarray(s1);
            createpc(para);
            pflag=1;
            }
            if(s2){
                cflag=1;
                int para1;
                para1=atoi(substr(s2,instr(s2,'(')+1,strlen(s2)-2));
                droppc(para1);
                pflag=1;
            }
        }
    if(!cflag) geterror(0);
    else if(!pflag) geterror(1);
    }
    return 0;
}

basic.h

#ifndef basic_h
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define basic_h
char *errormsg[256];
//process control block
struct pcb
{
int pid; //process id
int ppid; //parent process id
int prio; //priority
int state; //state
int lasttime; //last execute time
int tottime; //totle execute time
};
//process node
struct pnode
{
pcb *node;
pnode *sub;
pnode *brother;
pnode *next;
};
//信号量
struct semphore{
char name[5]; //名称
int count; //计数值
int curpid; //当前进程id
pnode *wlist; //等待链表
};
#define geterror(eno) printf("%s\n",errormsg[eno])
void initerror()
{
errormsg[0] = (char *)malloc(20);
errormsg[0]="error command!";
errormsg[1] = (char *)malloc(20);
errormsg[1]="error parameter!";
}
//get a substring in string s
char * substr(char *s,int start,int end)
{
char *s1;
int len = strlen(s);
if(start<0 || end>=len || start>end)
return NULL;
s1=(char *)malloc(end-start+2);
int i;
for(i=0;i<=end-start;i++)
s1[i] = s[i+start];
s1[i]='\0';
return s1;
}
//find the location of c in string s
int instr(char *s,char c)
{
int i;
for(i=0;i<strlen(s);i++)
if(s[i]==c)
return i;
return -1;
}
//change the string to array data
int * strtoarray(char *s)
{
int *a,count,i,x1;
char c, *s1,*s2;
if(!s)
{
printf("string can't be null!\n");
return NULL;
}
count=0;
s1=s;
for(i=0;i<strlen(s1);i++)
if(s1[i]==',')
count++;
count++;
a = (int *)malloc(count);
c=',';
for(i=0;i<count;i++)
{
x1 = instr(s1,c);
if(x1>=0)
s2=substr(s1,0,x1-1);
else
s2=s1;
a[i]=atoi(s2);
if(c==',')
s1=substr(s1,x1+1,strlen(s1)-1);
}
return a;
}
#endif

  • 7
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值