Home > 算法研究 > 操作系统,模拟进程管理之PCB块管理法,C语言实现

操作系统,模拟进程管理之PCB块管理法,C语言实现

首先大家祝福平安夜快乐啊,今天要发布的代码是一款C语言编写的模拟操作系统管理进程的程序调试环境TC,使用了PCB进行进程管理控制,建立三个基本的队列:等待、执行、阻塞进行模拟操作系统的进程管理,模拟进程的调度,模拟用户的创建、执行、阻塞、挂起、唤醒等操作

最近要准备准备操作系统考试,所以放一个程序跟大家分享


代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
*yctc cg
*/
#include "stdio.h"
#include "dos.h"
#include "stdlib.h"
#include "conio.h"
#define SEC 3
#define NULL 0
/*定义结构体*/
typedef struct PCB
{
  int PID;
  int UID;
  struct PCB * next;
}PCB;
PCB *really , *excute , *wait;
/*create queue header */
/*queue operation 入队*/
int enqueue(PCB *head , PCB *node)
{
   PCB *p;
   p = head;
   if(p -> next == NULL)
  {
    head -> next = node;
    return 1;
  }
   while(p)
   {
   if(p -> next == NULL)
  {
    p -> next = node;
    return 1;
  }
  else p = p -> next;
   }
}/*enquue*/
/*dequeue 出队列 */
PCB * dequeue(PCB *head)
{
  PCB *p;
  p = head;
  if(p -> next == NULL)
    {
    return NULL;
    }
  else
  {
    p = p -> next;
    head -> next = p -> next;
    p -> next = NULL;
    return p;
  }
    /*head to next*/
}/*dequeue*/
 
/*PCB operate*/
/*新建进程*/
int create()
{
    PCB *p;
    p = (PCB*)malloc(sizeof(PCB));
    p -> next = NULL;
    printf("input PID and UID to a new processn");
    scanf("%d %d",&p -> PID,&p -> UID);
    if(enqueue(really , p))
  printf("create a process: PID = %d UID = %dn", p -> PID , p -> UID);
    else
  printf("create Failedn");
}/*create*/
 
/*执行 fexcute*/
int fexcute()
{
   PCB *p = dequeue(really);
   if(p == NULL)
  {
    printf("NO process in queue n");
    return 0;
  }
   else
  {
    enqueue(excute , p);
    printf("add a process into excute queue process: PID = %d UID= %d n" ,p->PID , p->UID);
    return 1;
  }
}/*excute*/
 
int suspend()
{
  PCB *p = dequeue(excute);
   if(p == NULL)
  {
    printf("NO process in queue n");
    return 0;
  }
   else
  {
    enqueue(really , p);
    printf("add a process into really queue process: PID = %d UID= %d n" ,p->PID , p->UID);
    return 1;
  }
}
 
int wake()
{
  PCB *p = dequeue(wait);
   if(p == NULL)
  {
    printf("NO process in queue n");
    return 0;
  }
   else
  {
    enqueue(really , p);
    printf("add a process into wait really process: PID = %d UID= %d n" ,p->PID , p->UID);
    return 1;
  }
}
 
int block()
{
  PCB *p = dequeue(excute);
   if(p == NULL)
  {
    printf("NO process in queue n");
    return 0;
  }
   else
  {
    enqueue(wait , p);
    printf("add a process into wait queue process: PID = %d UID= %d n" ,p->PID , p->UID);
    return 1;
  }
}/*block*/
/*输出队列 outputqueue*/
int outputqueue(PCB *head)
{
   PCB *p;
   if(head -> next == NULL)
    {/*队列为空*/
  printf("queue is null n");
  return 1;
    }
   p = head -> next; /*node pointer*/
   while(p)
    {/*打印process id UID*/
  printf("PID = %d UID = %d n" , p -> PID , p -> UID);
  p = p -> next;
    }
    return 0;
}
/*output输出*/
int output()
{
  printf("REALLLY QUEUE:n");
  outputqueue(really);
  printf("EXCUTE QUEUE: n");
  outputqueue(excute);
  printf("WAIT QUEUE: n");
  outputqueue(wait);
}/*output*/
/*init 初始化*/
int init()
{
  PCB *p;
  clrscr();
  really = (PCB*)malloc(sizeof(PCB));
  really -> next=NULL;
  excute = (PCB*)malloc(sizeof(PCB));
  excute -> next=NULL;
  wait = (PCB*)malloc(sizeof(PCB));
  wait -> next = NULL;
  printf("____________PROCESS SECHUDLE__________n");
  printf("now initing.....................n");
  printf("input PID and UID as integer , 0 0 as overn");
  while(1)
  {
    p = (PCB*)malloc(sizeof(PCB));
    p -> next = NULL;
    scanf("%d %d",&p -> PID , &p -> UID);
    if(p -> PID == 0 && p -> UID == 0)
        break;
    else
      {
        if(enqueue(really , p))
          {
        printf("new process PID = %d UID = %d added!n",p -> PID , p -> UID);
          }
        else return 0;
        }
  }
  return 1;
}/*init*/
/*运行一个process*/
int run()
{
  PCB *p = excute;
  int s = SEC;
  if(excute -> next == NULL)
  {
   printf("no process in excute queue n");
   return 0;
  }
  else
  {
    p = excute -> next;
    printf("system will sleep %ds as process runningn",s);
    sleep(3);/*sleep as process runing time*/
    printf("process: PID = %d UID= %d excute successed..n" , p -> PID , p -> UID );
    excute -> next = p -> next;
    free(p);
  }
}/*run*/
/*离开*/
int leave()
{
  PCB *p,*t;
  while(really->next || excute->next || wait->next)
  {
    p = really -> next;
    while(p)
    {
      t = p -> next;
      free(p);
      p = t;
    }
    really -> next = NULL;
    p = wait -> next;
    while(p)
    {
      t = p -> next;
      free(p);
      p = t;
    }
    wait -> next = NULL;
    p = excute -> next;
    while(p)
    {
      t = p -> next;
      free(p);
      p = t;
    }
    excute -> next = NULL;
  }
  exit(0);
}/*leace*/
 
int help()
{
  printf("_____________________HELP MENU_____________________n");
  printf("t-h HELP show help optionn");
  printf("t-c CREATE create a new process , and put to really queuen");
  printf("t-b BLOCK block a process in excute queuen");
  printf("t-w WAKE wake a process in wait queuen");
  printf("t-e EXCUTE excute a process in really queuen");
  printf("t-s SUSPEND suspend a process in excute queuen");
  printf("t-o OUTPUT output all processes in queuesn");
  printf("t-r RUN excute a process in excute queuen");
  printf("t-x EXIT exit this programn");
  printf("___________________________________________________n");
  printf("t type 'H' will show this menun");
}/*help*/
 
int main()
{
  char COMMAND = NULL;
  if( init() != 1)
    {
      printf("init falied ! n ");
      getch();
      exit(0);
    }
  else
    {
      printf("init...OKn");
      output();
      help();
    }
  while(1)
  {
    /*当三队列都不空 执行调度 */
    printf(">");
    scanf("%c",&COMMAND);
    switch(COMMAND)
    {
      case 'n': break;
      case 'H':
      case 'h': help(); break;
      case 'C':
      case 'c': create(); break;
      case 'B':
      case 'b': block(); break;
      case 'W':
      case 'w': wake(); break;
      case 'S':
      case 's': suspend(); break;
      case 'E':
      case 'e': fexcute(); break;
      case 'O':
      case 'o': output(); break;
      case 'X':
      case 'x': leave(); break;
      case 'R':
      case 'r': run(); break;
    }
 }
}/*main*/
Categories: 算法研究 Tags: ,
  1. xq445566
    April 22nd, 2009 at 19:58 | #1

    very good

  2. April 22nd, 2009 at 22:03 | #2

    谢谢

  1. No trackbacks yet.