实验1 进程管理实验-计算机操作系统

在分析给定进程创建源代码基础上,结合可能使用的进程调度算法,完善PCB结构 编写程序实现进程撤销功能,并与进程创建程序构成一个进程管理程序,模拟实现进程的管理功能 进行动态的进程创建、撤销等测试。 按要求撰写实验报告并及时提交报告

进行10次以上的创建操作

进行10次以上的撤销操作(至少各有1次包含子、孙进程)

实验1 进程管理实验-计算机操作系统_第1张图片

 

代码一:main.cpp

代码一:basic.h  //与main.cpp放在同一目录

#include "basic.h"
pnode *proot; 	//system process tree root
pnode *plink; 	//system process link head

//delete process
void signal(int *PID){
	pnode *p,*p1,*pp;
} 


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;
    }
}





//create process
int createpc(int PID, int PPID, int PRIO) {
	//add your code here
	pnode *p,*p1,*pp;
	int pflag;
	pflag=0;
	for(p=plink; p; p=p->next) {
		if(p->node->pid == PID) { //check if this pid is already exist
			printf("pid %d is already exist!\n",PID);
			return -1;
		}
		if(p->node->pid == PPID) { //find parent pcb
			pflag=1;
			pp = p;
		}
	}
	if(!pflag) {
		printf("parent id %d is not exist!\n",PPID);
		return -2;
	}
	//init new pcb
	p1 = new pnode;
	p1->node=new pcb;
	p1->node->pid = PID;
	p1->node->ppid = PPID;

	p1->node->prio = PRIO;
	p1->sub=NULL;
	p1->next=NULL;
	p1->brother=NULL;
	//add to process tree
	if(!pp->sub)
		pp->sub=p1;

	else {
		for(p=pp->sub; p->brother; p=p->brother);
		p->brother=p1;
	}


	// add to process link
	for(p=plink; p->next; p=p->next);
	p->next=p1;
	return 0;
}

//show process detail
void showdetail() {
	//add your code here
	pnode *p,*p1;
	p=plink;
	for(; p;) { //print all pcb info
		printf("%d(p%d): 	",p->node->pid,p->node->prio);
		p1 = p->sub;
		for(; p1;) { //print sub pcb
			printf("%d(p%d) 	",p1->node->pid,p1->node->prio);
			p1 = p1->brother;
		}
		printf("\n");
		p = p->next;
	}
	printf("\n");
}
//don't change
int main() {
	int time = 0;
	initerror();
	short cflag,pflag;
	char cmdstr[32];
	proot = new pnode;		//进程节点 
	proot->node=new pcb;	//进程控制块 
	proot->node->pid=0;		//进程 id 
	proot->node->ppid=-1;	//父节点 id 
	proot->node->prio=0;	//优先级 
	proot->next=NULL;
	proot->sub=NULL;
	proot->brother=NULL;
	plink=proot;			//系统进程链接头系统进程链接头

	for(;;) {
		cflag=0;
		pflag=0;
		if(time%10==0){
			printf("\n******************************************");
			printf("\n*		进程演示系统	   	 *");
			printf("\n******************************************");
			printf("\n1.创建新的进程		    2.杀死运行进程");
			printf("\n3.查看详情	            4.退出系统");
			printf("\n******************************************\n");
		}
		time ++;
		printf("请选择(1~6):");

		scanf("%s",cmdstr);
		if(!strcmp(cmdstr,"4")) //exit the program
			break;
		if(!strcmp(cmdstr,"3")) {
			cflag = 1;
			pflag = 1;
			showdetail();
		}
		if(cmdstr[0]=='2'){
			int PID;
			cflag=1;
			pflag=1;
			printf("请选择销毁进程:");
			scanf("%d",&PID);
			droppc(PID);
		}
		if(cmdstr[0]=='1'){
			cflag=1;
			pflag=1;
			printf("请输入创建个数:");
			int num,i; scanf("%d",&num);
			for(i=0; i
#ifndef basic_h
#include 
#include 
#include 
#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!";//"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=0;
	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=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

你可能感兴趣的:(SWUST实验#,操作系统,#数据库,#计算机网络,系统架构)