操作系统简单算法

 

 



1                  实验  进程管理

1.1 实验目的

编写并调试一个模拟的进程调度程序,采用“最高优先数优先”调度算法对五个进程进行调度。

  “最高优先数优先”调度算法的基本思想是把 CPU 分配给就绪队列中优先数最高的进 程。静态优先数是在创建进程时确定的,并在整个进程运行期间不再改变。

动态优先数是指进程的优先数在创建进程时可以给定一个初始值,并且可以按一定原则修改优先数。例如:在进程获得一次 CPU 后就将其优先数减少 1,或者,进程等待的时间超 过某一时限时增加其优先数的值,等等。

1.2 实验思路

初始化pcb,输入进程信息,按照优先数从高到低排列,根据运行情况修改优先数

1.3 主要代码

void sort() /* 建立对进程进行优先级排列函数*/

{

      PCB *first, *second;

      int insert = 0;

      if ((ready == NULL) ||((p->super)>(ready->super))) /*优先级最大者,插入队首*/

      {

             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 =first->link;

                           second =second->link;

                    }

             }

             if (insert == 0) first->link =p;

      }

}

 

int space()

{

      int l = 0; PCB* pr = ready;

      while (pr != NULL)

      {

             l++;

             pr = pr->link;

      }

      return(l);

}

 

void destroy()/*建立进程撤消函数(进程运行结束,撤消进程)*/

{

      //printf("\n 进程 [%s] 已完成.\n", p->name);

      free(p);

}

 

 

// CTipDlg11message handlers

 

 

voidCTipDlg11::OnBnClickedButton1()

{

      USES_CONVERSION;

      // TODO: Add your control notificationhandler code here

      // 将各控件中的数据保存到相应的变量  

      UpdateData();

 

      // 将数据赋值给指针 

      //p = (pcb*)malloc(sizeof(pcb));

      p = new pcb();

      p->num = m_num;

      p->name = m_name;

      p->super = m_super;

      p->ntime = m_ntime;

      p->rtime = 0; p->state ="w";

      p->link = NULL;

      //sort();

      //int len;

      //len = space();

      sort();

 

      // 根据各变量的值更新相应的控件。和的编辑框会显示m_editSum的值  

      //UpdateData(FALSE);

}

 

 

voidCTipDlg11::OnBnClickedButton2()

{

      // TODO: Add your control notificationhandler code here

      int len,h=0;

      UpdateData(TRUE);

      len = space();

      while ((len != 0) && (ready !=NULL))

      {

             //ch = getchar();

             h++;

             CString stry,stry1;

             stry = "The executenumber:";

             stry1.Format(_T("%d"),h);

             stry += stry1;

             m_txt_1.AddString(stry);

             p = ready;

             ready = p->link;

             p->link = NULL;

             p->state = "R";

 

             //check.cpp

             PCB* pr;

             m_txt_1.AddString(_T(" **** 当前正在运行的进程是:")); /*显示当前运行进程*/

             //disp(p);

             m_txt_1.AddString(_T("qname  state  super ndtime  runtime"));

             //m_txt_1.AddString(p->name);

             CString str = p->name;

             str += "            ";

             str += p->state;

             CString str1;

             str1.Format(_T("%d"),p->super);//int->CString 其他的基本类型转化类似

             CString str2;

             str2.Format(_T("%d"),p->ntime);

             CString str3;

             str3.Format(_T("%d"),p->rtime);

             str += "            " ;

             str += str1;

             str += "            ";

             str += str2;

             str += "            ";

             str += str3;

             m_txt_1.AddString(str);

             pr = ready;

             m_txt_1.AddString(_T("\n ****当前就绪队列状态为:\n")); /*显示就绪队列状态*/

             while (pr != NULL)

             {

                    //disp(p);

                    m_txt_1.AddString(_T("qname  state  super ndtime  runtime"));

                    CString str = pr->name;

                    str += "            ";

                    str += pr->state;

                    CString str1;

                    str1.Format(_T("%d"),pr->super);//int->CString 其他的基本类型转化类似

                    CString str2;

                    str2.Format(_T("%d"),pr->ntime);

                    CString str3;

                    str3.Format(_T("%d"),pr->rtime);

                    str += "            ";

                    str += str1;

                    str += "            ";

                    str += str2;

                    str += "            ";

                    str += str3;

                    m_txt_1.AddString(str);

                    pr = pr->link;

             }

            

             //running();

             (p->rtime)++;

             if (p->rtime == p->ntime)

             {

                    CString strx;

                    strx = "进程[";

                    strx += p->name;

                    strx += "]已完成.";

                    m_txt_1.AddString(strx);

                    destroy(); /* 调用 destroy 函数*/

             }

             else

             {

                    (p->super)--;

                    p->state = "w";

                    sort(); /*调用 sort 函数*/

             }

      }

      UpdateData(FALSE);

}

1.4 运行截图

 


2                  实验1 作业管理

2.1 实验目的

编写并调试一个单道处理系统的作业等待模拟程序。

  作业等待算法:分别采用先来先服务(FCFS)

  对调度算法都要求打印每个作业开始运行时刻、完成时刻、周转时间、带权周转时间,以及这组作业的平均周转时间及带权平均周转时间。

 

2.2 实验思路

初始化jcb,按照先后提交顺序排列

2.3 主要代码

int space1()

{

    int l = 0; JCB* pr = ready;

    while (pr != NULL)

    {

        l++;

        pr =pr->next;

    }

    return(l);

}

 

 

// CTipDlg00 message handlers

 

 

voidCTipDlg00::OnBnClickedButton1()

{

    USES_CONVERSION;

    UpdateData(TRUE);

    // TODO: Add your control notification handler code here

    p = newjcb();

    p->name =m_name;

    //getch();

    p->reachtime= j;//作业默认到达时间

    p->needtime =m_ntime;

    p->state = "W";

    p->next = NULL;

    if (ready == NULL

        ready = q =p;

    else

    {

        q->next =p;

        q = p;

    }

    j++;

}

 

 

 

 

voidCTipDlg00::OnBnClickedButton2()

{

    UpdateData(TRUE);

    // TODO: Add your control notification handler code here

    int i,iden,n;

    n = space1();

    for (i = 0; i<n; i++)

    {

        p = ready;iden = 1;

        do{

            if (p->state == "W"&&p->reachtime <= times) 

                iden= 0;

            if (iden)

                p =p->next;

        } while (p != NULL&&iden);

        if (iden)

        {

            i--;

            CString stry, stry1;

            stry = "没有满足要求的进程,需等待";

            m_txt.AddString(stry);

            times++;

            if (times > 100)

            {

                stry= "时间过长";

                m_txt.AddString(stry1);

            }

               

        }

        else

        {

            //调用running()函数

            if (p == ready)          //先将要运行的作业从队列中分离出来

            {

                ready= p->next;

                p->next= NULL;

            }

            else

            {

                q =ready;

                while (q->next != p)

                    q= q->next;

                q->next= p->next;

            }

            p->starttime= times;    //计算作业运行后的完成时间,周转时间等等

            p->state= "R";

            p->finishtime= p->starttime + p->needtime;

            p->cycletime= (float)(p->finishtime -p->reachtime);

            p->cltime= (float)(p->cycletime / p->needtime);

            T1 +=p->cycletime;

            T2 +=p->cltime;

                  //调用disp()函数,显示作业运行情况

            CString cstrx;

            cstrx =p->name;

            cstrx +="作业正在运行,估计其运行情况:";

            m_txt.AddString(cstrx);

            CString cstrx1,cstrx11;

            cstrx11.Format(_T("%d"), p->starttime);

            cstrx1 ="开始运行时刻:";

            cstrx1+= cstrx11;

            m_txt.AddString(cstrx1);

 

            CString cstrx2, cstrx21;

            cstrx21.Format(_T("%d"), p->finishtime);

            cstrx2 ="完成时刻:";

            cstrx2+= cstrx21;

            m_txt.AddString(cstrx2);

 

            CString cstrx3, cstrx31;

            cstrx31.Format(_T("%f"), p->cycletime);

            cstrx3 ="周转时间:";

            cstrx3+= cstrx31;

            m_txt.AddString(cstrx3);

 

            CString cstrx4, cstrx41;

            cstrx41.Format(_T("%f"), p->cltime);

            cstrx4 ="周转时间:";

            cstrx4+= cstrx41;

            m_txt.AddString(cstrx4);

 

            times +=p->needtime;

            p->state= 'F';

            //printf("\n%s has been finished!\npress any key tocontinue...\n", p->name);

            free(p);          //释放运行后的作业

            //getch();

        }

    }

    //final()

    float s, t;

    t = T1 / n;

    s = T2 / n;

 

    CString cstrz;

    cstrz = "作业已经全部完成!";

    m_txt.AddString(cstrz);

 

    CString cstrz1,cstrz2,cstrz3;

    cstrz1.Format(_T("%d"), n);

    cstrz2 = "个作业的平均周转时间是:";

    cstrz1 +=cstrz2;

    cstrz3.Format(_T("%f"), t);

    cstrz1 +=cstrz3;

    m_txt.AddString(cstrz1);

 

    CString cstrz4,cstrz5,cstrz6;

    cstrz4.Format(_T("%d"), n);

    cstrz5 = "个作业的平均带权周转时间是";

    cstrz4 +=cstrz5;

    cstrz6.Format(_T("%f"), s);

    cstrz4 +=cstrz6;

    m_txt.AddString(cstrz4);

    UpdateData(FALSE);

}

 

2.4 运行截图


3                  实验1 存储管理

3.1 实验目的

设计一个可变式分区分配的存储管理方案,并模拟实现分区的分配和回收过程。 

对分区的管理法是算法:

首次适应算法

3.2 实验思路

3.3 主要代码

int Initblock()//开创带头结点的内存空间链表

{

    block_first = (LinkList)malloc(sizeof(LNode));

    block_last = (LinkList)malloc(sizeof(LNode));

    block_first->prior= NULL;

    block_first->next= block_last;

    block_last->prior= block_first;

    block_last->next= NULL;

    block_last->data.address= 0;

    block_last->data.size= MAX_length;

    block_last->data.ID= 0;

    block_last->data.state= Free;

    return 1;

}

 

//主存回收

int free(intID)

{

    LNode *p = block_first;

    while (p)

    {

        if (p->data.ID == ID)

        {

            p->data.state= Free;

            p->data.ID= Free;

            if (p->prior->data.state == Free)//与前面的空闲块相连

            {

                p->prior->data.size+= p->data.size;

                p->prior->next= p->next;

                p->next->prior= p->prior;

            }

            if (p->next->data.state == Free)//与后面的空闲块相连

            {

                p->data.size+= p->next->data.size;

                p->next->next->prior= p;

                p->next= p->next->next;

            }

            break;

        }

        p =p->next;

    }

    return 1;

}

 

int First_fit(intID, intrequest)//传入作业名及申请量

{

    //为申请作业开辟新空间且初始化

    LinkList temp = (LinkList)malloc(sizeof(LNode));

    temp->data.ID= ID;

    temp->data.size= request;

    temp->data.state= Busy;

 

    DuLNode *p = block_first->next;

    while (p)

    {

        if (p->data.state == Free&& p->data.size == request)

        {

            //有大小恰好合适的空闲块

            p->data.state= Busy;

            p->data.ID= ID;

            return 1;

            break;

        }

        if (p->data.state == Free&& p->data.size>request)

        {

            //有空闲块能满足需求且有剩余"

            temp->prior= p->prior;

            temp->next= p;

            temp->data.address= p->data.address;

            p->prior->next= temp;

            p->prior= temp;

            p->data.address= temp->data.address + temp->data.size;

            p->data.size-= request;

            return 1;

            break;

        }

        p =p->next;

    }

    return 0;

}

 

voidCTipDlg2::OnBnClickedButton1()

{

    USES_CONVERSION;

    // TODO: Add your control notification handler code here

    // 将各控件中的数据保存到相应的变量  

    UpdateData();

   

    int id, request;

    id = m_id;

    request =m_request;

    CString str;

    if (First_fit(id, request) == 1)

    {

        str = "分配成功!";

        m_txt.AddString(str);

    }

    else

    {

        str = "内存不足,分配失败!";

        m_txt.AddString(str);

    }

}

 

 

 

 

voidCTipDlg2::OnBnClickedButton2()

{

    USES_CONVERSION;

    // TODO: Add your control notification handler code here

    // 将各控件中的数据保存到相应的变量  

    UpdateData();

    int id1;

    id1 = m_id1;

    free(id1);

}

 

 

voidCTipDlg2::OnBnClickedButton7()

{

    USES_CONVERSION;

    // TODO: Add your control notification handler code here

    // 将各控件中的数据保存到相应的变量  

    UpdateData();

    Initblock(); //开创空间表

}

 

 

voidCTipDlg2::OnBnClickedButton6()

{

    USES_CONVERSION;

    // TODO: Add your control notification handler code here

    // 将各控件中的数据保存到相应的变量  

    UpdateData();  

    LNode *p = block_first->next;

    while (p)

    {

        CString str1;

        str1 = "分区号:";

        if (p->data.ID == Free)

        {

            CString strid;

            strid = "Free";

            str1 +=strid;

            m_txt.AddString(str1);

        }

        else

        {

            CString strid;

            strid.Format(_T("%d"), p->data.ID);

            str1 +=strid;

            m_txt.AddString(str1);

        }

        CString str2,str20;

        str2 = "起始地址:";

        str20.Format(_T("%d"), p->data.address);

        str2 +=str20;

        m_txt.AddString(str2);

 

        CString str3, str30;

        str3 = "分区大小:";

        str30.Format(_T("%d"), p->data.size);

        str3 +=str30;

        str3 += " KB";

        m_txt.AddString(str3);

 

        CString str4;

        str4 = "状态:";

        if (p->data.state == Free)

        {

            CString strstate;

            strstate= "空闲";

            str4 +=strstate;

            m_txt.AddString(str4);

        }

        else

        {

            CString strstate;

            strstate= "已分配";

            str4 +=strstate;

            m_txt.AddString(str4);

        }

        CString str5;

        str5 = "--------------";

        m_txt.AddString(str5);

        p =p->next;

    }

}

3.4 运行截图


4                  实验1 死锁避免

4.1 实验目的

银行家算法避免死锁

4.2 实验思路

输入进程数目,输入资源种类,输入每个进程最多所需的资源数,输入各个进程已分配的各资源数,安全性算法,银行家算法。

4.3 主要代码

bool Safe()                                    /*安全性算法*/

{

    int i, j, k, l = 0;

    int Work[MAXRESOURCE];                   /*工作数组*/

    for (i = 0; i<n; i++)

        Work[i] =AVAILABLE[i];

    for (i = 0; i<m; i++)

    {

        FINISH[i] = false;

    }

    for (i = 0; i<m; i++)

    {

        if (FINISH[i] == true)

        {

            continue;

        }

        else

        {

            for (j = 0; j<n; j++)

            {

                if (NEED[i][j]>Work[j])

                {

                    break;

                }

            }

            if (j == n)

            {

                FINISH[i]= true;

                for (k = 0; k<n; k++)

                {

                    Work[k]+= ALLOCATION[i][k];

                }

                p[l++]= i;

                i =-1;

            }

            else

            {

                continue;

            }

        }

       

        if (l == m)

        {

            ll = l;

            returntrue;

        }

    }

    returnfalse;

}

 

 

void CTipDlg3::OnBnClickedButton1()

{

    USES_CONVERSION;

    // TODO: Add your control notification handler code here

    // 将各控件中的数据保存到相应的变量  

    UpdateData();

    m = m_m;

    n = m_n;

}

 

 

void CTipDlg3::OnBnClickedButton2()

{

    USES_CONVERSION;

    // TODO: Add your control notification handler code here

    // 将各控件中的数据保存到相应的变量  

    UpdateData();

    for (int j = 0; j < n; j++)

    {

        MAX[max_i][j]= (int)(m_max[j])-48;

    }

    max_i++;

}

 

 

void CTipDlg3::OnBnClickedButton6()

{

    USES_CONVERSION;

    // TODO: Add your control notification handler code here

    // 将各控件中的数据保存到相应的变量  

    UpdateData();

    for (int j = 0; j < n; j++)

    {

        ALLOCATION[allocation_i][j]= (int)(m_allocation[j]) - 48;

        NEED[allocation_i][j]= MAX[allocation_i][j] - ALLOCATION[allocation_i][j];

    }

    allocation_i++;

}

 

 

void CTipDlg3::OnBnClickedButton7()

{

    USES_CONVERSION;

    // TODO: Add your control notification handler code here

    // 将各控件中的数据保存到相应的变量  

    UpdateData();

    for (int i = 0; i<n; i++)

    {

        AVAILABLE[i]= (int)(m_available[i]) - 48;

    }

    Safe();

    if (ll == m)

    {

        CStringstr,str1;

        str = "系统是安全的";

        m_txt.AddString(str);

        str1 = "安全序列:";

       

        CStringsequence;

        for (int i = 0; i<ll; i++)

        {

            CStringtest;

            test.Format(_T("%d"), p[i]);

            sequence+= test;

            if (i != ll - 1)

            {

                sequence+= "-->";

            }

        }

        str1 +=sequence;

        m_txt.AddString(str1);

    }

    else

    {

        CString error;

        error = "系统是不安全的";

        m_txt.AddString(error);

    }

}

   

 

 

void CTipDlg3::OnBnClickedButton8()

{

    USES_CONVERSION;

    // TODO: Add your control notification handler code here

    // 将各控件中的数据保存到相应的变量  

    UpdateData();

    ll = 0;

       //银行家算法

    cusneed = (int)(m_cusneed[0]) - 48;

    for (int i = 0; i<n; i++)

    {

        REQUEST[cusneed][i]= (int)(m_request[i]) - 48;

    }

 

    for (int i = 0; i<n; i++)

    {

        if (REQUEST[cusneed][i]>NEED[cusneed][i])

        {

            CStringpp;

            pp = "您输入的请求数超过进程的需求量!请重新输入!";

            m_txt.AddString(pp);

            continue;

        }

        if (REQUEST[cusneed][i]>AVAILABLE[i])

        {

            CStringpp;

            pp = "您输入的请求数超过系统有的资源数!请重新输入!";

            m_txt.AddString(pp);

            continue;

        }

    }

    for (int i = 0; i<n; i++)

    {

        AVAILABLE[i]-= REQUEST[cusneed][i];

        ALLOCATION[cusneed][i]+= REQUEST[cusneed][i];

        NEED[cusneed][i]-= REQUEST[cusneed][i];

    }

    if (Safe())

    {

        CString pp;

        pp = "同意分配请求!";

        m_txt.AddString(pp);

    }

    else

    {

        CString pp;

        pp = "您的请求被拒绝!";

        m_txt.AddString(pp);

        for (int i = 0; i<n; i++)

        {

            AVAILABLE[i]+= REQUEST[cusneed][i];

            ALLOCATION[cusneed][i]-= REQUEST[cusneed][i];

            NEED[cusneed][i]+= REQUEST[cusneed][i];

        }

    }

    for (int i = 0; i<m; i++)

    {

        FINISH[i] = false;

    }

}

4.4 运行截图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值