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;
Initialize Queue
Pthread * Init ();
void Init (pthread** head);
Create a process, insert a ready queue
void Creat_plan (Pthread **phead);
Establishing a run queue
void Input_run (pthread* rhead, pthread* data);
Run the process, first the ready process is added to the run queue
pthread* Run_pthread (pthread* head, pthread* phead);
Delete the first process in the ready queue and add it to the run queue
void Delete_pthread (pthread** phead, pthread* rhead);
Releasing the Run queue
pthread* free (pthread* rhead);
Sort
Pthread * Sort (pthread* head, int rank);
View the Ready process
void Select_pthread (Pthread *phead);
Let's do it first.
void Input_run1 (pthread* rhead, pthread* pdata);
Find as Node
Pthread * SORT1 (pthread* head);
Create a process, insert a ready queue
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 ("Initialization error \ n");
return NULL;
}
memset (*head, 0, sizeof (Pthread));
(*head)->next = NULL;
Return
}
Create a process, insert a ready queue
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 memory failed \ 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
}
Create a process, insert a ready queue
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 memory failed \ 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
}
Establishing a run queue
void Input_run (pthread* rhead, pthread* pdata)
{
time_t T;
Pthread *tmp = NULL;
Pthread *data = NULL;
if (pdata = = NULL)
{
printf ("No ready queue joins to run queue \ n");
Goto End;
}
data = (pthread*) malloc (sizeof (Pthread));
if (data = = NULL)
{
printf ("Data memory failed \ 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
}
Run the process, first the ready process is added to the run queue
pthread* Run_pthread (pthread* rhead, Pthread*phead)
{
int i = 0;
Pthread *tmp = NULL;
if (Rhead->next = = NULL)
{
printf ("No process can run \ n");
Goto End;
}
TMP = rhead->next;
Rhead->next = tmp->next;
printf ("Process number process level process CPU time process state process creation time \ n");
for (; i< (tmp->time); i++)
{
printf ("%d%d%d", 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;
}
Delete the first process in the ready queue and add it to the run queue
void Delete_pthread (pthread** phead, pthread* rhead)
{
Pthread *tmp = NULL;
if ((*phead)->next = = NULL | | phead = = NULL)
{
printf ("There is no process to be ready, please create process \ n");
Goto End;
}
TMP = (*phead)->next;
(*phead)->next = tmp->next;
Tmp->next = NULL;
Input_run (Rhead, TMP);
Free (TMP);
End:
Return
}
Release queue
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 ("Release complete \ n");
return head;
}
Sort
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;
}
View the Ready process
void Select_pthread (Pthread *phead)
{
Pthread *tmp = NULL;
if (Phead->next = = NULL | | phead = = NULL)
{
printf ("Process not ready \ n");
Goto End;
}
TMP = phead->next;
printf ("Process number process level process CPU time process state process creation time \ n");
while (tmp! = NULL)
{
printf ("%d%d%d", 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
}
Find as Node
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;
}
Establishing a run queue
void Input_run1 (pthread* rhead, pthread* pdata)
{
time_t T;
Pthread *tmp = NULL;
Pthread *data = NULL;
if (pdata = = NULL)
{
printf ("No ready queue joins to run queue \ n");
Goto End;
}
data = (pthread*) malloc (sizeof (Pthread));
if (data = = NULL)
{
printf ("Data memory failed \ 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 ("= = Enter the corresponding number 0 as required to exit Oh ==\n");
printf ("= = ==\n");
printf ("= = ==\n");
printf ("= = ==\n");
printf ("= = 1: Create process ==\n");
printf ("= = 2: Add the Ready process to the run queue ==\n");
printf ("= = 3: Running process ==\n");
printf ("= = 4: View Ready process Queue ==\n");
printf ("= = 5: Kill process (including run and ready) ==\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 for high-level first service 2 representative first come first serve ==\n");
printf ("Please enter ... \ n");
scanf ("%d", &type1);
while (type1! = 0)
{
Switch (type1)
{
Case 1:
SHOW_UI ();
printf ("Please enter ... \ 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 ("Input error, please enter \ n");
Goto End;
Break
}
End:
SHOW_UI ();
printf ("Continue, continue please enter ... \ n");
scanf ("%d", &type);
}
printf ("Run end \ n");
System ("pause");
return 0;
Break
Case 2:
SHOW_UI ();
printf ("Please enter ... \ 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 ("Input error, please enter \ n");
Goto End2;
Break
}
End2:
SHOW_UI ();
printf ("Continue, continue please enter ... \ n");
scanf ("%d", &type2);
}
printf ("Run end \ n");
System ("pause");
return 0;
Break
Default
printf ("Input error, please enter \ n");
Goto End1;
Break
}
END1:
printf ("Continue, continue please enter ... \ n");
scanf ("%d", &type1);
}
return 0;
}
Operating system scheduling simulation