西北农林科技大学操作系统实验一------进程调度-代码阅读并调试实验

一. 读题完成任务

任务:
1、阅读下面源代码,完善程序中填空处内容。
2、阅读代码,写出调度算法、算法流程图和程序功能。
3、解释数据结构PCB的定义和作用。
4、为main()写出每行的注释。
5、调试并运行代码,写出结果。
#include <stdio.h>
#include <stdlib.h> 
#include <conio.h> 
#define getpch(type) (type*)malloc(sizeof(type)) 
#define NULL 0 
struct pcb { /* 定义进程控制块PCB */
	char name[10];//进程名称
	char state;//进程所处状态	
	int super;//优先级	
	int ntime;
	int rtime;
	struct pcb* link;
}*ready = NULL, * p;
typedef struct pcb PCB;

void sort() /* 建立对进程进行优先级排列函数*/
{
	PCB* first, * second;
	int insert = 0;
	if ((ready == NULL) || ((p->super) > (ready->super))) /*1若没有进程在进行,则将p直接插入                      */
	{
		p->link = ready;
		ready = p;
	}
	else /* 进程比较优先级,插入适当的位置中*/
	{
		first = ready;
		second = first->link;
		while (second != NULL)
		{
			if ((p->super) > (second->super)) /*若插入进程比当前进程优先数大,*/
			{ /*插入到当前进程前面*/
				p->link = second;
				first->link = p;
				second = NULL;
				insert = 1;
			}

			else /* 插入进程优先数最低,则插入到队尾*/
			{
				//2.
				second->link = p->link;
				second = second->link;
			}
		}
		if (insert == 0) first->link = p;
	}
}

void input() /* 建立进程控制块函数*/
{
	int i, num;
	clrscr(); /*清屏*/
	printf("\n 请输入进程号?");
	scanf("%d", &num);
	for (i = 0; i < num; i++)
	{
		printf("\n 进程号No.%d:\n", i);
		p = getpch(PCB);
		printf("\n 输入进程名:");
		scanf("%s", p->name);
		printf("\n 输入进程优先数:");
		scanf("%d", &p->super);
		printf("\n 输入进程运行时间:");
		scanf("%d", &p->ntime);
		printf("\n");
		p->rtime = 0; p->state = 'w';
		p->link = NULL;
		sort(); /* 调用sort函数*/
	}
}
int space()
{
	int l = 0; PCB* pr = ready;
	while (pr != NULL)
	{
		l++;
		pr = pr->link;
	}
	return(l);
}
void disp(PCB* pr) /*建立进程显示函数,用于显示当前进程*/
{
	printf("\n qname \t state \t super \t ndtime \t runtime \n");
	printf("|%s\t", pr->name);
	printf("|%c\t", pr->state);
	printf("|%d\t", pr->super);
	printf("|%d\t", pr->ntime);
	printf("|%d\t", pr->rtime);
	printf("\n");
}

void check() /* 建立进程查看函数 */
{
	PCB* pr;
	printf("\n **** 当前正在运行的进程是:%s", p->name); /*显示当前运行进程*/
	disp(p);
	pr = ready;
	printf("\n ****当前就绪队列状态为:\n"); /*显示就绪队列状态*/
	while (pr != NULL)
	{
		disp(pr);
		pr = pr->link;
	}
}
void destroy() /*建立进程撤消函数(进程运行结束,撤消进程)*/
{
	printf("\n 进程 [%s] 已完成.\n", p->name);
	free(p);
}
void running() /* 建立进程就绪函数(进程运行时间到,置就绪状态*/
{
	(p->rtime)++;
	if (p->rtime == p->ntime)
		destroy(); /* 调用destroy函数*/
	else
	{
		(p->super)--;
		p->state = 'w';
		sort(); /*调用sort函数*/
	}
}
int main() /*主函数*/
{
	int len, h = 0;
	char ch;//初始定义len,h,ch变量	
	input();//输入程序PCB信息
	len = space();
	while ((len != 0) && (ready != NULL))
	{
		ch = getchar();
		h++;
		printf("\n The execute number:%d \n", h);
		p = ready;
		ready = p->link;//将程序加入就绪队列
		p->link = NULL;
		p->state = 'R';//将程序状态设为‘R’
		check();
		running();//程序模拟运行
		printf("\n 按任一键继续......");
		ch = getchar();
	}
	printf("\n\n 进程已经完成.\n");
	ch = getchar();
	return 0;
  1. 见程序代码
  2. 阅读代码,写出调度算法、算法流程图和程序功能。
    调度算法:非抢占式优先调度算法
    程序流程图
    流程图
    程序功能
    此程序模拟了进程显示及调度过程,通过建立相应进程进行模拟运行,并且比较优先数进行优先先后运行,此代码模拟了非抢占式优先调度算法
    
    1. 解释数据结构PCB的定义和作用。
      PCB:进程控制块
      使一个在多道程序环境下不能独立运行的程序(含数据)成为一个能独立运行的单位,一个能与其他进程并发执行的进程。
      (1)作为独立运行基本单位的标志
      (2)能实现间断性运行方式
      (3)提供进程管理所需要的信息
      (4)提供进程调度所需要的信息
      (5)实现与其他进程的同步与通信
    2. 为main()写出每行的注释。
      见程序代码
    3. 本程序模拟了不同进程竞争发生的情况,优先级高的优先执行,然后并发执行

    二.设计程序

    1. 第一个实验

    编写并调试一个模拟的进程调度程序,采用“最高优先数优先”调度算法对五个进程进行调度。
      最高优先数优先调度算法的基本思想是把CPU分配给就绪队列中优先数最高的进程。
      静态优先数是在创建进程时确定的,并在整个进程运行期间不再改变。
    动态优先数是指进程的优先数在创建进程时可以给定一个初始值,并且可以按一定原则修改优先数。例如:在进程获得一次CPU后就将其优先数减少1。或者,进程等待的时间超过某一时限时增加其优先数的值,等等。


#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#define getpch(type) (type*)malloc(sizeof(type))



/*定义PCB模块*/
struct pcb  
{
    char name[10];
    char state;
    int super;//优先数  
    int ntime;//预计运行完成时间
    int rtime;//运行时间
    int wait;
    struct pcb* link;//链接下一个进程
}*ready=NULL, *p;//
typedef struct pcb PCB;


void sort() /* 对进程根据优先级进行排序*/
{
    PCB *first, *second;
    int insert=0;
   
    if((ready==NULL)||((p->super)>(ready->super))) /*1若没有进程在进行,则将p直接插入*/
    {
        p->link=ready;
        ready=p;
    }
    else 
    {
        first=ready;
        second=first->link;
        while(second!=NULL)
        {
            if((p->super)>(second->super)) 
            {
                p->link=second;
                first->link=p;
                second=NULL;
                insert=1;
            }

            else 
            {
                first = second;
                second=second->link;
            }
        }
        if(insert==0) first->link=p;
    }
}


void input()
{
    int i,num;
    system("cls");
    printf("\n �������������:");
    scanf("%d",&num);
    for(i=0; i<num; i++)
    {
        printf("\n ���̺�No.%d:\n",i);
        p=getpch(PCB);
        printf("\n ���������:");
        scanf("%s",p->name);
        printf("\n �������������:");
        scanf("%d",&p->super);
        printf("\n �����������ʱ��:");
        scanf("%d",&p->ntime);
        printf("\n");
        p->rtime=0;
        p->state='w';//
        p->wait=0;
        p->link=NULL;
        sort();
    }
}


int space()
{
    int l=0;
    PCB* pr=ready;
    while(pr!=NULL)
    {
        l++;
        pr=pr->link;
    }
    return(l);
}
void disp(PCB * pr) /*显示进程信息*/
{
    printf("\n qname \t state \t super \t ndtime runtime \t wait \n");
    printf("|%s\t",pr->name);
    printf("|%c\t",pr->state);
    printf("|%d\t",pr->super);
    printf("|%d\t",pr->ntime);
    printf("|%d\t",pr->rtime);
     printf("|%d\t",pr->wait);
    printf("\n");
}

void check() 
{
    PCB* pr;
    printf("\n **** ��ǰ�������еĽ�����:%s",p->name);
    disp(p);
    pr=ready;
    printf("\n ****��ǰ��������״̬Ϊ:\n"); 
    {
        disp(pr);
        pr=pr->link;
    }
}
void destroy() 
{
    printf("\n ���� [%s] �����.\n",p->name);
    free(p);
}

void running()
{
    (p->rtime)++;
    if(p->rtime==p->ntime)
        destroy(); 
    else
    {
        (p->super)--;
        p->state='w';
        sort();
    }
}


void running1()
{
    (p->rtime)++;
    if(p->rtime==p->ntime)
        destroy();
    else
        sort();
}


void running2()
{
    (p->rtime)++;
    if(p->rtime==p->ntime)
        destroy(); 

    else
    {
        
        PCB* pt=ready;
        while(pt!=NULL )
        {
            pt->wait++;
            if(pt->wait >= 3)
            {
                pt->super++;
                pt->wait=0;
            }
            pt=pt->link;

        }

        sort();

        if(ready ->link == NULL)
            printf("CCC\n");

        pt=ready;
        PCB *ptr;
        while(pt != NULL )
        {
            ptr = pt->link;
            while(ptr!=NULL)
           {
               if(ptr -> super > pt ->super)
               {
                   PCB *temp;
                   strcpy(temp ->name, pt ->name);
                   temp -> ntime = pt ->ntime;
                   temp -> rtime = pt ->rtime;
                   temp ->state = pt ->state;
                   temp ->super = pt ->super;
                   temp ->wait = pt ->wait;

                   strcpy(pt ->name, ptr ->name);
                   pt -> ntime = ptr ->ntime;
                   pt -> rtime = ptr ->rtime;
                   pt ->state = ptr ->state;
                   pt ->super = ptr ->super;
                   pt ->wait = ptr ->wait;

                   strcpy(ptr ->name, temp ->name);
                   ptr -> ntime = temp ->ntime;
                   ptr -> rtime = temp ->rtime;
                   ptr ->state = temp ->state;
                   ptr ->super = temp ->super;
                   ptr ->wait = temp ->wait;
               }//优先数大的与优先数小的进行交换
               ptr = ptr->link;
           }
           pt = pt->link;
        }
    }
}

int main()
{
    int len,h=0;
    //
    char ch;

    input();

    len=space();

    int way;
    printf("���������з�ʽ:\n   0--��̬���ȼ�\n   1--��̬���ȼ�(����һ�����ȼ�-1)\n   2--��̬���ȼ�(�ȴ�ʱ�䳬��3���ȼ�+1)\n");
    scanf("%d", &way);
    while((len!=0)&&(ready!=NULL))
    {

        ch=getchar();
        h++;

        printf("\n The execute number:%d \n",h);
        p=ready;

        ready=p->link;

        p->link=NULL;

        p->state='R';
        //
        check();
        if(way == 0)
            running1();
        else if(way == 1)
            running();
        else if(way == 2)
            running2();
        printf("\n ����һ������......");
        ch=getchar();
    }

    printf("\n\n �����Ѿ����.\n");

    ch=getchar();
    return 0;
}
  1. 第二个实验
#include "stdio.h"
#include <stdlib.h>
#include <conio.h>
#define getpch(type) (type*)malloc(sizeof(type))
//#define NULL 0
struct pcb   /* 定义进程控制块PCB */
{
    char name[10];//进程名
    char state;//进程状态(R:就绪状态)
    //int super;//优先级
    int ntime;//运行时间
    int rtime;//已运行了多长时间
    struct pcb* link;//连接下一个PCB
}*ready=NULL, *p;//ready是就绪队列
typedef struct pcb PCB;

void Insert() /* 建立对进程进行优先级排列函数*/
{
    //尾插法
    PCB* ptr;
    ptr = ready;
    if(ready == NULL)
        ready = p;
    else
    {
        while(ptr -> link!= NULL)
          ptr = ptr -> link;
       ptr -> link = p;
    }

}

// 建立进程控制块函数
void input()
{
    int i,num;
    //clrscr(); /*清屏(系统函数)*/
    system("cls");
    printf("\n 请输入进程总数:");
    scanf("%d",&num);
    for(i=0; i<num; i++)
    {
        printf("\n 进程号No.%d:\n",i);
        //分配内存
        p=getpch(PCB);
        printf("\n 输入进程名:");
        //p是PCB的一个指针
        scanf("%s",p->name);
        //printf("\n 输入进程优先数:");
       // scanf("%d",&p->super);
        printf("\n 输入进程运行时间:");
        scanf("%d",&p->ntime);
        printf("\n");
        p->rtime=0;
        p->state='w';//
        p->link=NULL;
        Insert(); /* 调用sort函数*/
    }
}

//就绪队列的长度
int space()
{
    int l=0;
    PCB* pr=ready;
    while(pr!=NULL)
    {
        l++;
        pr=pr->link;
    }
    return(l);
}
void disp(PCB * pr) /*建立进程显示函数,用于显示当前进程*/
{
    printf("\n qname\tstate\t ndtime\truntime\n");
    printf("|%s\t",pr->name);
    printf("|%c\t",pr->state);
    //printf("|%d\t",pr->super);
    printf("|%d\t",pr->ntime);
    printf("|%d\t",pr->rtime);
    printf("\n");
}

void check() /* 建立进程查看函数 */
{
    if(p -> rtime >= p->ntime)
        return;
    PCB* pr;
    printf("\n **** 当前正在运行的进程是:%s",p->name); /*显示当前运行进程*/
    //显示执行进程的各种属性
    disp(p);
    pr=ready;
    printf("\n ****当前就绪队列状态为:\n"); /*显示就绪队列状态*/
    while(pr!=NULL)
    {
        disp(pr);
        pr=pr->link;
    }
}
void destroy() /*建立进程撤消函数(进程运行结束,撤消进程)*/
{
    printf("\n 进程 [%s] 已完成.\n",p->name);
    free(p);
}
void running() /* 建立进程就绪函数(进程运行时间到,置就绪状态*/
{
    (p->rtime)++;
    if(p->rtime==p->ntime)
        destroy(); /* 调用destroy函数*/
   else if(p -> rtime > p->ntime)
      printf("CPU处于空闲状态!\n");
}
int main() /*主函数*/
{
    //len就绪队列的长度,h进程编号
    int len,h=0;
    //
    char ch;
    //输入新添加的n个进程
    input();
    //就绪队列的长度
    len=space();
    //遍历就绪队列,调度算法
    while((len!=0)&&(ready!=NULL))
    {
        //吞掉缓冲区的“\n"??
        ch=getchar();
        /*p成为就绪队列队首元素(要执行的进程),以下三条语句用于将队首元素
        (要执行的进程从就绪队列中取出)*/
        p=ready;
        //就绪队列队首前进一格
        ready=p->link;
        //删除队首元素(要执行的进程)
        p->link=NULL;
        //把队首元素(要执行的进程)设置为就绪状态
        p->state='R';

        //时间片为1的进行运行
        int n = 0;
        while(n++ < 5)
          {
              h++;
              //输出执行的时间片数,从1开始
             printf("\n The execute number:%d \n",h);
              //查看正在执行进程及就绪队列的信息
             check();
             running();
          }
        if(p->rtime < p->ntime)
            Insert();
        printf("\n 按任一键继续......");
        ch=getchar();
    }
    //最后输出
    printf("\n\n 进程已经完成.\n");
    //吞掉缓冲区的字符
    ch=getchar();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44029810/article/details/107322999