| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 693 人关注过本帖
标题:链表模拟简单的进程调度
取消只看楼主 加入收藏
世界模型
Rank: 4
等 级:业余侠客
威 望:1
帖 子:240
专家分:226
注 册:2010-9-12
结帖率:97.44%
收藏
已结贴  问题点数:60 回复次数:1 
链表模拟简单的进程调度
程序代码:
/*
*模拟简单的进程调度
*/
#include <stdio.h>
#include <stdlib.h>

typedef struct PCB
{
    char name[10];
    int  rtime;
    int  priorityNum;
    char state;
    struct PCB *next;
}PCB;

/*
*初始化PCB
*/
PCB *initPCB()
{
  PCB *head;

  head=(PCB *)malloc(sizeof(PCB));
  if(NULL==head)
  {
      exit(-1);
  }
  head->next=NULL;

  return head;
}

/*
*  新建一个节点,并返回
*/
PCB *inputValue()
{
    PCB *temp = NULL;
  
    temp = initPCB();

    printf("please input the information of process\n");
    printf("name:");
    scanf("%s",temp->name);
    printf("run time:");
    scanf("%d",&temp->rtime);
    printf("priorityNum:");
    scanf("%d",&temp->priorityNum);
    temp->state = 'R';
    printf("\n");

    return temp;
}

/*
*找到优先级最高的进程
*/
void findProcess( PCB * head, PCB * pCurrent )
{
  
    if( head->next == NULL )
    {
        head->next = pCurrent;
        return ;
    }
  
    /* 从大到小排 */
    PCB* p = head->next;
    PCB* q = head;      
    while( p != NULL )
    {
        if( (pCurrent->priorityNum) > (p->priorityNum) )
        {
            q->next = pCurrent;
            pCurrent->next = p;
            break;  
        }
        q = p;
        p = p->next;
    }
    q->next = pCurrent;
}

/*
*打印PCB
*/
void printPCB( PCB * head )
{
    PCB *temp;

    temp = head->next;
    while( temp!=NULL )
    {
        printf("\n process name: %s\n run time: %d\n priority num: %d\n process state:%c\n",
            temp->name,temp->rtime,temp->priorityNum,temp->state);
        temp = temp->next;
    }
    printf("\n\n");
}

/*
*运行进程
*/
void runProcess( PCB * head,PCB * selected )//选中一进程 优先数减一 运行时间减一 如果运行的时间为0 就将装太改为E 如果不为0 加入队列 重新比较 获得优先数最大进程 依次下去直到
{                                            //所有的进程状态都为E  求实现??????????????????????
    PCB *p,*q;

    while( selected->next != NULL )
    {
         selected->priorityNum--;
         selected->rtime--;

         if( selected->rtime == 0 )
        {
            selected->state = 'E';
            break;
        }
          
        /*将运行时间不为0的进程加入队列*/
         p = head->next;
        q = head;
        if( q == NULL )//空队列
        {
            q = selected;
        }
          if( (selected->priorityNum) > (p->priorityNum) )
        {
             q->next = selected;
             selected->next = p;
        }
    }

}

int main()
{
    PCB *head = NULL;
    PCB *temp = NULL;

    head = initPCB(); // 头结点为空

    for(int i=0;i<3;i++)
    {
      temp = inputValue();
      findProcess(head,temp);
      printf("=================\n");      
    }

 
    runProcess(head,temp);
    printPCB(head);  
  
    return 0;
}


2011-11-27 16:04
世界模型
Rank: 4
等 级:业余侠客
威 望:1
帖 子:240
专家分:226
注 册:2010-9-12
收藏
得分:0 
程序代码:
/*
*模拟简单的进程调度
*/
#include <stdio.h>
#include <stdlib.h>

typedef struct PCB
{
    char name[10];
    int  rtime;
    int  priorityNum;
    char state;
    struct PCB *next;
}PCB;

/*
*初始化PCB
*/
PCB *initPCB()
{
  PCB *head;

  head=(PCB *)malloc(sizeof(PCB));
  if(NULL==head)
  {
      exit(-1);
  }
  head->next=NULL;

  return head;
}

/*
*  新建一个节点,并返回
*/
PCB *inputValue()
{
    PCB *temp = NULL;
   
    temp = initPCB();

    printf("please input the information of process\n");
    printf("name:");
    scanf("%s",temp->name);
    printf("run time:");
    scanf("%d",&temp->rtime);
    printf("priorityNum:");
    scanf("%d",&temp->priorityNum);
    temp->state = 'R';
    printf("\n");

    return temp;
}

/*
*找到优先级最高的进程
*/
void findProcess( PCB * head, PCB * pCurrent )
{
   
    if( head->next == NULL )
    {
        head->next = pCurrent;
        return ;
    }
   
    /* 从大到小排 */
    PCB* p = head->next;
    PCB* q = head;       
    while( p != NULL )
    {
        if( (pCurrent->priorityNum) > (p->priorityNum) )
        {
            q->next = pCurrent;
            pCurrent->next = p;
            break;   
        }
        q = p;
        p = p->next;
    }
    q->next = pCurrent;
}

/*
*打印PCB
*/
void printPCB( PCB * head )
{
    PCB *temp;

    temp = head->next;
    while( temp!=NULL )
    {
        printf("\n process name: %s run time: %d priority num: %d process state:%c",
            temp->name,temp->rtime,temp->priorityNum,temp->state);
        temp = temp->next;
    }
    printf("\n");
}

/*
*运行进程
*/
void runProcess( PCB * head)
{
    PCB *processblock = NULL;
    PCB *pcb = head->next;
    while(pcb != NULL)
    {   
        pcb->rtime -= 1;
        pcb->priorityNum -= 1;
        if(pcb->rtime == 0)
        {
            pcb->state = 'E';
            pcb = pcb->next;
            if(pcb == NULL)
                return ;
        }
        else
        {
            processblock = pcb;
            pcb = pcb->next;
            processblock->next = NULL;
            findProcess(pcb,processblock);
            system("cls");
        }
        head->next = pcb;
        printPCB(head);
        getchar();
    }
    return ;
}

int main()
{
    PCB *head = NULL;
    PCB *temp = NULL;

    head = initPCB(); // 头结点为空

    for(int i=0;i<5;i++)
    {
      temp = inputValue();
      findProcess(head,temp);
      printf("=================\n");       
    }
   runProcess(head);
   return 0;
}


2011-11-27 20:03
快速回复:链表模拟简单的进程调度
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.038052 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved