资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

c语言中是什么调度函数 c语言调试函数

处理机调度算法的实现 用C语言

网络答案,经过验证:

成都创新互联是工信部颁发资质IDC服务器商,为用户提供优质的郑州服务器托管服务

#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))) /*优先级最大者,插入队首*/

{

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;

}

}

void input() /* 建立进程控制块函数*/

{

int i,num;

system("cls"); /*清屏*/

printf("\n 请输入进程数: ");

scanf("%d",num);

for(i=1;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 进程名\t 状态\t 优先数\t 需要运行时间\t 已经运行时间\n");

printf("|%s\t",pr-name);

printf("|%c\t",pr-state);

printf("|%d\t",pr-super);

printf("|%d\t\t",pr-ntime);

printf("|%d\t",pr-rtime);

printf("\n");

}

void check() /* 建立进程查看函数 */

{

PCB* pr;

printf("\n **** 当前正在运行的进程是:\n"); /*显示当前运行进程*/

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函数*/

}

}

void main() /*主函数*/

{

int len,h=0;

char ch;

input();

len=space();

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

{

ch=getchar()();

h++;

printf("-----------------------------------------------------");

printf("\n 现在是第%d次运行: \n",h);

p=ready;

ready=p-link;

p-link=NULL;

p-state='R';

check();

running();

printf("\n 按任意键继续......\n");

}

printf("\n\n 进程已经完成.\n");

}

c语言中什么是调用函数和被调用函数

调用函数就是计算机编译或运行时,使用某个函数来完成相关命令。对无参函数调用时则无实际参数表。实际参数表中的参数可以是常数、变量或其它构造类型数据及表达式。各实参之间用逗号分隔。

这两个定义是相对的,比如说你自己定义编写了一个函数,然后在后面的编写语句中要用到你之前编写的函数,你引用了,那个函数就是被调用函数,你正在写的那个主函数就是调用函数。

C语言模拟操作系统进程调度和管理

给,已经编译运行通过了,简单写的:

#includestdio.h

#includetime.h

#includestdlib.h

/*********************以下是全局数据结构和变量***********************/

/*PCB 结构*/

struct PCB{

int pname;

int pri;

int runtime;

int waittime;

struct PCB *next;

}pcb[7];

/* 运行指针*/

struct PCB *running;

/*高优先级就绪队列头指针*/

struct PCB *Hready;

/*低优先级队列头指针*/

struct PCB *Lready;

/*等待队列头指针*/

struct PCB *wait;

int sig=0;

/**************************以下是函数说明****************************/

/*利用循环实现延迟*/

void delay();

/*模拟进程3-9*/

void proc(struct PCB *running);

/*将node插入到head所指示的队列的尾部*/

void InsertIntoQueueTail(struct PCB ** head,struct PCB *node);

/*进程调度函数*/

int proc_switch();

/*进程等待函数*/

void proc_wait();

/*进程唤醒函数*/

int proc_wakeup();

/************************以下是函数定义及注释************************/

/*主函数*/

main()

{

int i;

/*初始化,创建进程3-9,置低优先级,等待时间为0,

依次插入低优先级队列*/

for(i = 0;i 7;i++){

pcb[i].pname = i+3;

pcb[i].pri = 0;

pcb[i].waittime = 0;

InsertIntoQueueTail(Lready,pcb[i]);

}

/*等待队列和高优先级队列为空*/

wait = NULL;

Hready=NULL;

printf("\nThe process_switch begin:\n");

/*模拟进程调度开始*/

for(;;)

{

switch(sig){

case 0:/*无进程等待调度,打印信息并返回*/

if(!proc_switch())

{

printf("No Process to run,press any key to return:\n");

getchar();

}

break;

case 1:proc_wait();

break;

case 3:

case 4:

case 5:

case 6:

case 7:

case 8:

case 9:proc(running);

break;

default:printf("\nerror!");

exit(-1);

}

}

}

/*功能:延迟一个时间片*/

/*入口参数:无*/

/*出口参数:无*/

void delay()

{

int i,j;

for(i=0;i20000;i++)

for(j=0;j10000;j++)

{

}

}

/*功能:进程3-9*/

/*入口参数:运行指针*/

/*出口参数:无*/

void proc(struct PCB * running)

{

int i;

srand( (unsigned)time( NULL ) );

/*显示当前运行的进程的id*/

printf("\nNow Process %d is running\n",running-pname);

/*当前进程执行running-runtime个时间片*/

for(i=running-runtime;i0;i--){

/*显示剩余的时间片*/

printf("%d time slice(s) left\n",i);

/*延迟*/

delay();

proc_wakeup();

/*产生一个1到1000的随机数,若该随机数小余100,当前进程等待,*/

if((rand()%1000+1)100){

printf("Process %d begins to wait.\n",running-pname);

sig=1;

return;

}

}

/*显示时间片耗尽,进程转为低优先级就绪状态*/

printf("Time slices for process %d exhausted.\n",running-pname);

InsertIntoQueueTail(Hready,running);

sig=0;

return;

}

/*功能:将一个节点插入队列尾部*/

/*入口参数:队列头指针地址head,待插入结点node*/

/*出口参数:无*/

void InsertIntoQueueTail(struct PCB **head,struct PCB *node)

{

struct PCB *p;

node-next=NULL;

/*被插入队列为空*/

if(*head==NULL){

*head=node;

return;

}

/*被插入队列不为空*/

else{

p=*head;

/*找到队列的最后一个结点*/

while(p-next!=NULL) p=p-next;

p-next=node;

}

}

/*功能:进程调度*/

/*入口参数:无*/

/*出口参数:若调度成功,返回1,否则返回0*/

int proc_switch()

{

/*若高优先级就绪队列和低优先级就绪队列均为空,则循环执行进程唤醒*/

while(Hready == NULL Lready == NULL)

if(!proc_wakeup()) return 0;

/*若高优先级就绪队列非空,则执行其第一个进程,分配2个时间片*/

if(Hready != NULL){

running = Hready;

Hready = Hready - next;

running-runtime = 2;

}

/*若高优先级就绪队列为空,则执行低优先级就绪队列的第一个进程,

分配5个时间片*/

else{

running = Lready;

Lready=Lready - next;

running - runtime = 5;

}

/*别调度进程的id赋给sig*/

sig = running - pname;

return 1;

}

/*功能:进程等待。将当前运行进程置高优先级,等待时间为20,

插入等待队列尾部*/

/*入口参数:无*/

/*出口参数:无*/

void proc_wait()

{

struct PCB *p;

running-pri=1;

running-waittime=20;

InsertIntoQueueTail(wait,running);

sig=0;

return;

}

/*功能:进程唤醒*/

/*入口参数:无*/

/*出口参数:若等待队列为空,则返回0,否则返回1*/

int proc_wakeup()

{

struct PCB *p,*last,*MoveToReady;

p = wait;

/*等待队列为空,返回0*/

if(p == NULL) return 0;

/*延迟*/

delay();

/*等待队列中每个进程的等待时间减1*/

while(p != NULL){

p - waittime -= 1;

p=p-next;

}

p=wait;

/*从等待队列中摘除等待时间为0的进程,插入到高优先级就绪队列的尾部*/

while(p!=NULL){

if(p - waittime == 0){

MoveToReady = p;

if (p == wait)

wait = p-next;

else

last - next = p-next;

p = p - next;

InsertIntoQueueTail(Hready,MoveToReady);

}

else{

p = p - next;

}

}

sig =0;

return 1;

}


当前名称:c语言中是什么调度函数 c语言调试函数
转载来于:http://cdkjz.cn/article/dopcegp.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220