1. 实验目的

1、 加深对进程概念的理解和进程创建与撤消算法;

2、 进一步认识并发执行的实质。

  1. 实验内容:

本实验完成如下三个层次的任务:

(1)系统级—以普通用户身份认识windows的进程管理。通过windows的“任务管理器”观察进程的状态,进行进程的创建、切换和撤销。

(2)语言级—以普通程序员身份认识高级语言(不限语言,VC++/Java/VB……都可)的进程创建与撤销工具。

(3)模拟级—以OS设计师身份编程模拟实现进程创建与撤销功能,并在屏幕上观察进程活动的结果。

3、源程序:

(1)无

(2)

利用Java中ProcessBuilder类的方法创建进程,打开cmd,打印输出c盘下的目录文件夹

import java.io.File;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class test {
    public static void main(String[] args) {
        try {
            List<String> list = new ArrayList<String>();
            ProcessBuilder pb = null;
            Process p = null;
            String line = null;
            BufferedReader stdout = null;
            //list the files and directorys under C:\
            list.add("CMD.EXE");
            list.add("/C");
            list.add("dir");
            pb = new ProcessBuilder(list);
            pb.directory(new File("C:\\"));
            p = pb.start();
            stdout = new BufferedReader(new InputStreamReader(p
                    .getInputStream()));
            while ((line = stdout.readLine()) != null) {
                System.out.println(line);
            }
            stdout.close();
            //echo the value of NAME
            pb = new ProcessBuilder();
            pb.command(new String[]{"CMD.exe", "/C", "echo %NAME%"});
            pb.environment().put("NAME", "TEST");
            p = pb.start();
            stdout = new BufferedReader(new InputStreamReader(p
                    .getInputStream()));
            while ((line = stdout.readLine()) != null) {
                System.out.println(line);
            }
            stdout.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

(3)

import java.util.Scanner;
class process {
    //    一个进程所需要的变量
    String name;//进程名称
    int id;//进程id
    int PRIORITY;//进程优先级
    float time;//进程运行时间
    int PCB;//控制储存信息
    public process(int num){
        CreateProcess(num);
    }
    public process(){};
    public static process initProcess(){
        process process=new process();
        process.id=0;
        process.name="主进程";
        process.PRIORITY=1;
        process.PCB=10;
        process.time=1;
        return process;
    }
    //    创建进程
    public void CreateProcess(int numprocesses) {
        Scanner in = new Scanner(System.in);
        this.id = numprocesses;
        System.out.println("请输入进程名称:");
        this.name = in.nextLine();
        System.out.println("请输入进程的内存大小:");
        this.PCB = in.nextInt();
        System.out.println("请输入进程的优先级:");
        this.PRIORITY = in.nextInt();
        System.out.println("请输入进程的运行时间:");
        this.time=in.nextInt();
        return ;
    }
//    查看进程
    public static void readProcess(process process[]){
//        查看进程的有关信息
        for (int i = 0; i < process.length; i++) {
            if(process[i]!=null) {
                System.out.println("进程id为:" + process[i].id
                        + "  进程名称为:" + process[i].name + "  进程的所占内存为:" + process[i].PCB + "  进程的有先级为:" +
                        process[i].PRIORITY + "  进程运行的时间为:" + process[i].time + "\n");
            }
            else return;
        }
    }
    public static void CancelProcess(process process[]){
        Scanner in=new Scanner(System.in);
        System.out.println("请输入要取消的进程的id:");
        int id;
        id=in.nextInt();
        process[id]=null;
        while (process[id+1]!=null){
            process[id]= process[id+1];
            process[id].id=process[id].id-1;
            id++;
        }
        process[id]=null;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public int getPRIORITY() {
        return PRIORITY;
    }
    public void setPRIORITY(int PRIORITY) {
        this.PRIORITY = PRIORITY;
    }
    public float getTime() {
        return time;
    }
    public void setTime(float time) {
        this.time = time;
    }
}
public class MyList {
    process[] valueArray={};
    int length;
    int size;
    int index;
    int defaultLength=10;
    public void initArray(){
        length=defaultLength;
        valueArray=new process[length];
        size=0;
        index=0;
    }
    public MyList(){
        initArray();
    }
    public void grow(){
        System.out.println("扩容前:"+length);
        int newLength=2*length;
        length=newLength;
        process[] newArray=new process[length];
        for (int i = 0; i < valueArray.length; i++) {
            newArray[i]=valueArray[i];
        }
        valueArray=newArray;
        System.out.println("扩容后:"+valueArray.length);
    }
}
public class PrintSth {
    public static void meau(){
        System.out.println("------------------模拟进程的创建与撤销功能----------------------\n");
        System.out.println("1.创建进程\n");
        System.out.println("2.查看现有进程\n");
        System.out.println("3.撤销进程\n");
        System.out.println("4.退出主进程\n");
        System.out.println("---------------------------------------------------------------\n");
        System.out.println("请输入操作指令:");
    }
}
public class Study_SimulationProcess {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
//        用户给的参数
        int command = 0;
        //当程序启动时,模拟主进程启动
        int mainProcess = 1;
        //模拟内存大小
        int MemorySize = 100;
//        创建进程数组
        MyList arr=new MyList();
//        当前进程数
        int numProcess=1;
        process initProcess= process.initProcess();
        arr.valueArray[0]=initProcess;
        while (command != 4) {
            PrintSth.meau();
            command = in.nextInt();
            if (command < 1 || command > 4) {
                command = 0;
                System.out.println("输入指令有误请重新输入:");
            } else if (command == 1) {
                process process=new process(numProcess);
                arr.valueArray[numProcess]=process;
                numProcess++;
            } else if (command == 2) {
            process.readProcess(arr.valueArray);
            } else if (command == 3) {
              process.CancelProcess(arr.valueArray);
            } else if (command == 4) {
                System.out.println("模拟测试结束!");
                return;
            }
        }
    }
}

4、测试数据

(1)无

(2)无

(3)

a.创建进程:

进程名: QQ 进程内存大小:20 进程优先级:3 进程运行时间: 89

创建条件符合,可以创建

进程名:WeChat 进程内存大小20 进程优先级:3 进程运行时间:100

创建条件符合,可以创建

进程名:IDEA 进程内存大小:20 进程优先级:3 进程运行时间:1000

创建条件符合,可以创建

进程名:Pycharm 进程内存大小100 进程优先级3 进程运行时间:1222

创建内存过大创建失败(系统设置内存大小为100)

b.撤销进程:

输入撤销id  2

撤销条件满足,撤销成功

c.退出主线程:

退出主线程成功,退出程序

5、运行结果:

(1)用户角度,利用任务管理器对进程进行管理,chrome就是创建演示的进程,右键相应的进程可以对其进行管理。

当前状态:

切换前:

切换后:

(2)

(3)

a.测试进程的创建

测试内存过大的情况下,无法创建成

最后退出程序

6、出现问题及解决方法

出现的问题主要是对于Java的数据结构有些遗忘,以及考虑用什么数据结构来保存一系列的进程,后来觉得用自定义的类的数组来保存进程!