模拟操作系统调度代码

Pthread.h

#ifndef _PTHREAD_H_
#define _PTHREAD_H_


typedef struct _Pthread
{
int pid;
int  rank;
int time;
char T;
struct tm* createtime;
struct _Pthread *next;
}Pthread;


//初始化队列
//Pthread * Init();
void Init(Pthread** head);
//创建进程,插入就绪队列
void Creat_Plan(Pthread **phead);


//建立运行队列
void Input_Run(Pthread* rhead, Pthread* data);




//运行进程,首先会把就绪进程加入到运行队列
Pthread* Run_Pthread(Pthread* head, Pthread* phead);


//删除就绪队列中的首个进程,并将其加入到运行队列中
void Delete_Pthread(Pthread** phead, Pthread* rhead);




//释放运行队列
Pthread*  Free(Pthread* rhead);




//排序
Pthread * Sort(Pthread* head, int rank);


//查看就绪的进程
void Select_Pthread(Pthread *phead);
//先来先执行
void Input_Run1(Pthread* rhead, Pthread* pdata);
//查找为节点
Pthread * Sort1(Pthread* head);
//创建进程,插入就绪队列
void Creat_Plan1(Pthread **phead);
#endif



Main.c

#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include"Pthread.h"


static int pid = 0;
Pthread *phead = NULL;
Pthread* rhead = NULL;


void Init(Pthread** head)
{
*head = (Pthread*)malloc(sizeof(Pthread));
if (*head == NULL)
{
printf("初始化出错\n");
return NULL;
}
memset(*head, 0, sizeof(Pthread));
(*head)->next = NULL;
return;
}


//创建进程,插入就绪队列
void Creat_Plan(Pthread **phead)
{


int ret = 0;
time_t t;
Pthread *tmp = NULL;
Pthread *data = NULL;
data = (Pthread*)malloc(sizeof(Pthread));
if (data == NULL)
{
ret = -1;
printf("data 内存开辟失败\n");
goto End;
}
memset(data, 0, sizeof(Pthread));
pid++;
data->pid = pid;
srand((unsigned)time(NULL));
data->rank = rand() % 100;
srand((unsigned)time(NULL));
data->time = 1 + (rand() % 10);
time(&t);
data->createtime = gmtime(&t);
data->T = 'W';
data->next = NULL;
if ((*phead)->next == NULL)
{
(*phead)->next = data;
goto End;
}
else
{
tmp = Sort(*phead, data->rank);
data->next = tmp->next;
tmp->next = data;
goto End;
}
End:
return;
}


//创建进程,插入就绪队列
void Creat_Plan1(Pthread **phead)
{


int ret = 0;
time_t t;
Pthread *tmp = NULL;
Pthread *data = NULL;
data = (Pthread*)malloc(sizeof(Pthread));
if (data == NULL)
{
ret = -1;
printf("data 内存开辟失败\n");
goto End;
}
memset(data, 0, sizeof(Pthread));
pid++;
data->pid = pid;
srand((unsigned)time(NULL));
data->rank = rand() % 100;
srand((unsigned)time(NULL));
data->time = 1 + (rand() % 10);
time(&t);
data->createtime = gmtime(&t);
data->T = 'W';
data->next = NULL;
if ((*phead)->next == NULL)
{
(*phead)->next = data;
goto End;
}
else
{
tmp = Sort1(*phead);


tmp->next = data;
goto End;
}
End:
return;
}


//建立运行队列
void Input_Run(Pthread* rhead, Pthread* pdata)
{


time_t t;
Pthread *tmp = NULL;
Pthread *data = NULL;
if (pdata == NULL)
{
printf("没有就绪队列加入到运行队列\n");
goto End;
}
data = (Pthread*)malloc(sizeof(Pthread));
if (data == NULL)
{


printf("data 内存开辟失败\n");
goto End;
}
memset(data, 0, sizeof(Pthread));


memcpy(data, pdata, sizeof(Pthread));
data->T = 'R';
if (rhead->next == NULL)
{
rhead->next = data;
goto End;
}
else
{
tmp = Sort1(rhead);
data->next = tmp->next;
tmp->next = data;
goto End;
}
End:
return;
}










//运行进程,首先会把就绪进程加入到运行队列
Pthread* Run_Pthread(Pthread* rhead, Pthread*phead)
{
int i = 0;
Pthread *tmp = NULL;
if (rhead->next == NULL)
{
printf("没有可以运行的进程\n");
goto End;
}
tmp = rhead->next;
rhead->next = tmp->next;
printf("进程号    进程级别   进程cpu时间     进程状态      进程创建时间\n");
for (; i<(tmp->time); i++)
{




printf("%d          %d           %d         %c", tmp->pid, tmp->rank, tmp->time,tmp->T);
printf("          %d:%d:%d:", tmp->createtime->tm_year + 1900, tmp->createtime->tm_mon + 1, tmp->createtime->tm_mday);
printf("%d:%d:%d\n", tmp->createtime->tm_hour + 8, tmp->createtime->tm_min, tmp->createtime->tm_sec);


}
free(tmp);
End:
Delete_Pthread(&phead, rhead);
return rhead;
}


//删除就绪队列中的首个进程,并将其加入到运行队列中
void Delete_Pthread(Pthread** phead, Pthread* rhead)
{
Pthread *tmp = NULL;
if ((*phead)->next == NULL || phead == NULL)
{
printf("没有可以就绪的进程,请创建进程\n");
goto End;
}
tmp = (*phead)->next;
(*phead)->next = tmp->next;
tmp->next = NULL;
Input_Run(rhead, tmp);
free(tmp);
End:
return;
}




//释放队列
Pthread*  Free(Pthread* head)
{
Pthread* tmp = NULL;
Pthread* emp = NULL;
if (head->next == NULL)
{
goto End;
}
tmp = head->next;
while (tmp != NULL)
{
emp = tmp->next;
free(tmp);
tmp = emp;
}
head->next = tmp;
End:
printf("释放完毕\n");
return head;
}




//排序
Pthread * Sort(Pthread* head, int rank)
{
Pthread *tmp = NULL;
Pthread *emp = NULL;
if (head->next == NULL)
{
return head;
}
tmp = head->next;
emp = head;
while (tmp != NULL)
{
if (rank > tmp->rank)
goto End;
tmp = tmp->next;
emp = emp->next;


}
End:
return emp;
}


//查看就绪的进程
void Select_Pthread(Pthread *phead)
{
Pthread *tmp = NULL;
if (phead->next == NULL || phead == NULL)
{
printf("没有就绪的进程\n");
goto End;
}
tmp = phead->next;
printf("进程号    进程级别   进程cpu时间     进程状态      进程创建时间\n");
while (tmp != NULL)
{
printf("%d          %d           %d         %c", tmp->pid, tmp->rank, tmp->time, tmp->T);
printf("          %d:%d:%d:", tmp->createtime->tm_year + 1900, tmp->createtime->tm_mon + 1, tmp->createtime->tm_mday);
printf("%d:%d:%d\n", tmp->createtime->tm_hour + 8, tmp->createtime->tm_min, tmp->createtime->tm_sec);
tmp = tmp->next;
}
End:


return;
}
//查找为节点
Pthread * Sort1(Pthread* head)
{
Pthread* tmp = NULL;
Pthread* emp = NULL;


tmp = head->next;
emp = head;
while (tmp != NULL)
{
tmp = tmp->next;
emp = emp->next;
}
return emp;
}


//建立运行队列
void Input_Run1(Pthread* rhead, Pthread* pdata)
{


time_t t;
Pthread *tmp = NULL;
Pthread *data = NULL;
if (pdata == NULL)
{
printf("没有就绪队列加入到运行队列\n");
goto End;
}
data = (Pthread*)malloc(sizeof(Pthread));
if (data == NULL)
{


printf("data 内存开辟失败\n");
goto End;
}
memset(data, 0, sizeof(Pthread));


memcpy(data, pdata, sizeof(Pthread));
data->T = 'R';
if (rhead->next == NULL)
{
rhead->next = data;
goto End;
}
else
{
tmp = Sort1(rhead);
data->next = tmp->next;
tmp->next = data;
goto End;
}
End:
return;
}
void Show_UI()
{
printf("=============================================================\n");
printf("=============================================================\n");
printf("==        根据需求输入相应的数字 0表示退出哦               ==\n");
printf("==                                                         ==\n");
printf("==                                                         ==\n");
printf("==                                                         ==\n");
printf("==                 1:创建进程                              ==\n");
printf("==                 2:把就绪进程加入到运行队列              ==\n");
printf("==                 3:运行进程                              ==\n");
printf("==                 4:查看就绪进程队列                      ==\n");
printf("==                 5:杀死进程(包括运行和就绪)              ==\n");
printf("==                                                         ==\n");
printf("==                                                         ==\n");
printf("==                                                         ==\n");
printf("=============================================================\n");
return;
}


int main(void)
{


/*phead = Init();
rhead = Init();*/


int type1;
int type;
int type2;
Init(&phead);
Init(&rhead);
printf("==      1代表级别高者先服务   2代表先来先服务       ==\n");
printf("请输入.....\n");
scanf("%d", &type1);
while (type1 != 0)
{
switch (type1)
{
case 1:
Show_UI();
printf("请输入.....\n");
scanf("%d", &type);
while (type != 0)
{


switch (type)
{
case 1:
Creat_Plan(&phead);
break;
case 2:
Delete_Pthread(&phead, rhead);
break;
case 3:
rhead = Run_Pthread(rhead, phead);
break;
case 4:
Select_Pthread(phead);
break;
case 5:
phead = Free(phead);
rhead = Free(rhead);
break;
default:
printf("输入有误,请从新输入\n");
goto End;
break;
}
End:
//Show_UI();
printf("是否继续,继续请输入......\n");
scanf("%d", &type);
}
printf("运行结束\n");
system("pause");
return 0;
break;
case 2:
Show_UI();
printf("请输入.....\n");
scanf("%d", &type2);
while (type2 != 0)
{


switch (type2)
{
case 1:
Creat_Plan1(&phead);
break;
case 2:
Delete_Pthread(&phead, rhead);
break;
case 3:
rhead = Run_Pthread(rhead, phead);
break;
case 4:
Select_Pthread(phead);
break;
case 5:
phead = Free(phead);
rhead = Free(rhead);
break;
default:
printf("输入有误,请从新输入\n");
goto End2;
break;
}
End2:
//Show_UI();
printf("是否继续,继续请输入......\n");
scanf("%d", &type2);
}
printf("运行结束\n");
system("pause");
return 0;
break;
default:
printf("输入有误,请从新输入\n");
goto End1;
break;
}
End1:
printf("是否继续,继续请输入......\n");
scanf("%d", &type1);

}
return 0;
}

你可能感兴趣的:(模拟操作系统调度代码)