进程管理 C语言

能够分析并用高级语言实现进程的创建和撤销。

根据可能使用的进程调度算法,完善 PCB 设计实现进程撤销,与进程创建程序构成完整的进程管理程序
环境 Devc++
测试原理:创建进程,每个进程包括进程 id ,父进程 id ,进程优先级
撤销进程,撤销父进程,子进程也会被撤销

basic.h

#pragma once
#ifndef basic_h
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define basic_h

const char* errormsg[256];

struct pcb
{
	int pid;
	int ppid;
	int prio;
	int state;
	int lasttime;
	int tottime;
};

struct pnode
{
	pcb* node;
	pnode* sub;
	pnode* brother;
	pnode* next;
};

struct semphore {
	char name[5];
	int count;
	int curpid;
	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!";
}

char* substr(char* s, int start, int end) {
	char* s1;
	int i;
	int len = strlen(s);
	if (start<0 || end >= len || start>end)
		return NULL;
	s1 = (char*)malloc(end - start + 3);
	if (s1) {
		for (i = 0; i <= end - start; i++)
			s1[i] = s[i + start];
		s1[i] = '\0';
		return s1;
	}
}

int instr(char* s, char c) {
	int i;
	for (i = 0; i < strlen(s); i++)
		if (c == s[i])
			return i;
	return -1;
}

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;
		if(a)
			a[i] = atoi(s2);
		if (c == ',')
			s1 = substr(s1, x1 + 1, strlen(s1) - 1);
	}
	return a;
}
#endif

Create_Delete.cpp

#include "basic.h"

pnode* proot;
pnode* plink;
//创建进程
int createpc(int* para) {
	pnode* p, * p1, * pp = NULL;
	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;
			break;
		}
	}
	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->brother = NULL;
	p1->next = NULL;
	if (!pp->sub)
		pp->sub = p1;
	else {
		p = pp->sub;
		while (p->brother != NULL)
			p = p->brother;
		p->brother = p1;
	}
	p = plink;
	while (p->next != NULL)
		p = p->next;
	p->next = p1;
	return 0;
}
//撤销进程树
void delT(pnode* pdlt) {
	for (pnode* p = plink; p; p = p->next){
	
		if (p->next != NULL && p->next == pdlt){
			p->next = pdlt->next;
			break;
		}
	}
	if (pdlt->brother != NULL){
		delT(pdlt->brother);
	}
	if (pdlt->sub != NULL){
		delT(pdlt->sub);
	}
	delete pdlt;
}
//撤销进程PCB链
void deletepc(int num) {
	pnode* p, * pt = NULL, * pp = NULL;
	bool bInside = false;
	if (num == 0) {
		return;
	}
	for (p = plink; p != NULL; p = p->next) {
		if (p->node->pid == num) {
			pt = p;
			bInside = true;
			break;
		}
	}
	if (!bInside) {
		return;
	}
	for (p = plink; p != NULL; p = p->next) {
		if (p->node->pid == pt->node->ppid) {
			pp = p;
			break;
		}
	}
	if (pp != NULL) {
		if (pp->sub == pt) {
			pp->sub = pt->brother;
			pt->brother = NULL;
		}
		else {
			for (p = pp->sub; p != NULL; p = p->brother) {
				if (p->brother == pt) {
					p->brother = pt->brother;
					pt->brother = NULL;
					break;
				}
			}
		}
	}
	delT(pt);
}
//显示当前进程
void showdetail() {
	pnode* p, * p1;
	p = plink;
	while(p != NULL) {
		printf("%d(prio %d):		", p->node->pid, p->node->prio);
		p1 = p->sub;
		while (p1 != NULL) {
			printf("%d(prio %d):		", p1->node->pid, p1->node->prio);
			p1 = p1->brother;
		}
		printf("\n");
		p = p->next;
	}
	printf("\n");
}

int main() {
	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->brother = NULL;
	proot->next = NULL;
	proot->sub = NULL;
	plink = proot;
	while(1) {
		cflag = 0;
		pflag = 0;
		printf("cmd:");
		scanf("%s", cmdstr);
		cmdstr[strlen(cmdstr)] = '\0';
		if (!strcmp(cmdstr, "exit"))
			break;
		else if (!strcmp(cmdstr, "showdetail")) {
			cflag = 1;
			pflag = 1;
			showdetail();
		}
		else if (!strcmp(cmdstr, "deletepc")) {
			int number = 0;
			scanf("%d", &number);
			deletepc(number);
		}
		else {
			char* s, * s1;
			int* para;
			s = strstr(cmdstr, "createpc");
			if (s != NULL) {
				cflag = 1;
				s1 = substr(cmdstr, instr(cmdstr, '(') + 1, strlen(cmdstr) - 2);
				para = strtoarray(s1);
				createpc(para);
				pflag = 1;
			}
		}
	}
	delete proot;
	return 0;
}

在cmd:createpc(1,0,1) 第一个1表示序列 第二个0 表示父进程是0,第三个1表示为0的子进程。
showdetail 表示全部展示
deletepc 回车后 输入数字 表示删除相应的进程 其后的子进程一起删除

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值