Bootstrap

操作系统——进程创建与撤销算法(C++)

操作系统——进程创建与撤销算法


1. 实验目的:

(1) 加深对进程概念的理解和进程创建与撤销算法;
(2) 进一步认识并发执行的性质

2. 实验内容:

(1) 系统级——以普通用户身份认识Windows的进程管理。通过Windows的“任务管理器”观察进程的创建、切换和撤销。
(2) 语言级——以普通程序猿身份认识高级语言VC++/JAVA/VB的进程创建与撤销工具。
(3) 模拟级——以OS设计师身份编程模拟实现进程创建与撤销功能,并在屏幕上观察进程活动的结果

3. 源程序:

(2)语言级:(C语言)

  1. 什么是原语(Primitive)
    原语:机器指令构成的一种实现特定功能的小程序,它的运行具有不可分割性。
  2. 原语特点:
    a. 贴近底层
    b. 最重要的
    c. 运行过程具有原子性(不可中断)
    d. 系统小程序
  3. 操作系统中的原语类别
    a. 进程控制用的原语: 实现进程管理和状态切换
    如:进程创建原语、进程撤销原语、阻塞原语、唤醒原语、
    进程挂起原语、进程激活原语、进程调度原语等。
    b. 进程通信用的原语:用于实现进程之间通信的,如:消息发送原语、消息接收原语等。
    c. 资源互斥与同步用的原语:解决资源互斥访问的,主要有P操作原语和V操作原语。
    d. 资源管理用的原语: 主要有请求资源的原语和释放资源的原语。

下列是具体的代码实现:

#include <string>`在这里插入代码片`
#include <Windows.h>
#include<iostream>
#include<stdlib.h>

using namespace std;

/*
new和delete取代malloc和free
用std::string来取代char*(定义在<string>头文件中)

*/

int Creat_Process(STARTUPINFO si, PROCESS_INFORMATION pi, std::string strCmdLine)
{
    if (!CreateProcess(NULL,
                       (LPSTR)strCmdLine.c_str(),
        //在c语言中没有string类型,故必须通过string类对象的成员函数c_str()
        //把string 对象转换成c中的字符串样式,c_str()返回的是一个临时指针,不能对其进行操作
        //strcpy(c,s.c_str());

                       NULL,
                       NULL,
                       FALSE,
                       0,
                       NULL,
                       NULL,
                       &si,
                       &pi
                      )
        )
    {
        system("pause");//等待用户按一个键,然后返回。
        return 0;
    }
    return 1;
}

int main()
{
    STARTUPINFO si;
	ZeroMemory(&si, sizeof(si));
	si.cb = sizeof(si);
	PROCESS_INFORMATION pi;
	ZeroMemory(&pi, sizeof(pi));
	std::string strCmdLine = "ping ";
	std::string input;

    cout<<"请输入要ping的命令:";
    cin>>input;
    strCmdLine += input;

	if(!Creat_Process(si, pi, strCmdLine))
    {
        cout<<endl;
        cout<<"进程创建失败 @_@ !"<<endl;
        exit(0);
    }
    else
    {
        cout<<endl;
        cout<<"进程创建成功 ^_^ !"<<endl;
    }
    WaitForSingleObject(pi.hProcess, INFINITE);
    if(!CloseHandle(pi.hProcess) and !CloseHandle(pi.hThread))
    {
        cout<<endl;
        cout<<"进程已关闭 ^_^ !"<<endl;
    }

    return 0;
}

(3)模拟级的代码实现:

	#include<iostream>
	#include<stdlib.h>
	
	using namespace std;
	
	/*
	new和delete取代malloc和free
	用std::string来取代char*(定义在<string>头文件中)
	
	*/
	
	typedef struct {
	    long ID;
	    float TIME;//运行时间
	    int PRIORITY;//优先级
	}PCB;
	
	typedef struct Lnode
	{
	    PCB data;
	    struct Lnode *next;
	}Qlist,*List;
	
	typedef struct
	{
	    List front;
	    List rear;
	}LQ;
	
	
	void Display(LQ Q)
	{
	    List p;
	    p=Q.front->next;
	    while(p)
	    {
	        cout<<"(进程ID:"<<p->data.ID<<" -> 进程运行时间:"<<p->data.TIME<<" -> 进程优先级:"<<p->data.PRIORITY<<")"<<endl;
	        p=p->next;
	    }
	    cout<<endl;
	}
	
	void InitQueue(LQ &Q)
	{
	    Q.front=Q.rear=new Qlist;
	    Q.front->next=NULL;
	}
	
	void Enqueue(LQ &Q, List new_p, PCB e)
	{
	    List p=new Qlist;
	    p->data.ID=e.ID;
	    p->data.PRIORITY=e.PRIORITY;
	    p->data.TIME=e.TIME;
	    p->next=NULL;
	
	    if(Q.front == Q.rear)
	    {
	        Q.rear->next=p;
	        Q.rear=p;
	    }
	    else
	    {
	        p->next=new_p->next;
	        new_p->next=p;
	        new_p=p;
	        //cout<<"new_p:进程ID:"<<new_p->data.ID<<"进程运行时间:"<<new_p->data.TIME<<"进程优先级:"<<new_p->data.PRIORITY<<endl;
	        //cout<<"p:进程ID:"<<new_p->data.ID<<"进程运行时间:"<<new_p->data.TIME<<"进程优先级:"<<new_p->data.PRIORITY<<endl;
	    }
	    //cout<<"Enqueue";
	    //Display(Q);
	}
	
	int KILL(LQ &Q, PCB &e)
	{
	    if(Q.front==Q.rear)
	    {
	        cout<<"队空!"<<endl;
	        return 0;
	    }
	    List p;
	    p=Q.front->next;
	    e.ID=p->data.ID;
	    e.PRIORITY=p->data.PRIORITY;
	    e.TIME=p->data.TIME;
	
	    Q.front->next=p->next;
	    if(Q.rear==p)
	        Q.rear=Q.front;
	    delete p;
	
	    return 1;
	
	}
	
	void Creat_Process(LQ &Q)
	{
	    int n;
	    PCB pcb;
	    List p1, p1_next;
	
	    cout<<"输入要创建进程的个数:";
	    cin>>n;
	
	    for(int i=0; i<n; i++)
	    {
	        cout<<"进程ID:";
	        cin>>pcb.ID;
	
	        cout<<"进程运行时间:";
	        cin>>pcb.TIME;
	
	        cout<<"进程优先级:";
	        cin>>pcb.PRIORITY;
	
	        p1=Q.front;
	        if(Q.front == Q.rear)
	            Enqueue(Q, p1, pcb);
	        else
	        {
	            //cout<<"123"<<endl;
	            p1_next=p1->next;
	            while(p1)
	            {
	                while( p1_next && p1_next->data.PRIORITY == pcb.PRIORITY )//同一等级,先来后到,高等级优先
	                {
	                    p1=p1->next;
	                    p1_next=p1->next;
	                }
	                //cout<<"1728  "<<p1_next->data.PRIORITY<<endl;
	                if( p1_next && p1_next->data.PRIORITY > pcb.PRIORITY)
	                {
	                    //cout<<"kksadh"<<endl;
	                    Enqueue(Q, p1, pcb);
	                    break;
	                }
	                else
	                {
	                    //cout<<"11982"<<endl;
	                    Enqueue(Q, Q.rear, pcb);
	                    break;
	
	                }
	                //cout<<"1714sadh"<<endl;
	                p1=p1->next;
	                p1_next=p1->next;
	            }
	
	        }
	
	    }
	    //cout<<"Creat_Process"<<endl;
	    //Display(Q);
	
	}
	
	
	int main()
	{
	    LQ Qpcb;
	    InitQueue(Qpcb);
	    PCB data;
	    int n;
	    while(1)
	    {
	        cout<<"*************菜单栏**************"<<endl;
	        cout<<"  创建进程:1       撤销进程:2"<<endl;
	        cout<<"  输出就绪队列:3   退出操作:4"<<endl;
	        cout<<"*********************************"<<endl;
	        cout<<"输入操作序号:";
	        cin>>n;
	        switch(n)
	        {
	        case 1:
	            Creat_Process(Qpcb);break;
	        case 2:
	            KILL(Qpcb, data);
	            cout<<"撤销的进程的相关信息("<<"进程ID:"<<data.ID<<"进程运行时间:"<<data.TIME<<"进程优先级:"<<data.PRIORITY<<"("<<endl;
	            break;
	        case 3:
	            Display(Qpcb);break;
	        case 4:
	            break;
	        default:
	            cout<<"输入错误!!!"<<endl;
	        }
	        if(n == 4) break;
	
	    }
	    //Display(Qpcb);
	
	    return 0;
	}

3)模拟级运行结果:

在这里插入图片描述

;