Skip to content

Commit 48f0b62

Browse files
author
Loriah Pope
committed
Initial Commit
0 parents  commit 48f0b62

16 files changed

+526
-0
lines changed

#kernel.c#

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
#include "hardware.h"
5+
#include "drivers.h"
6+
#include "kernel.h"
7+
8+
9+
10+
/* You may use the definitions below, if you are so inclined, to
11+
define the process table entries. Feel free to use your own
12+
definitions, though. */
13+
14+
15+
typedef enum { RUNNING, READY, BLOCKED , UNINITIALIZED } PROCESS_STATE;
16+
17+
typedef struct process_table_entry {
18+
PROCESS_STATE state;
19+
int CPU_time_used;
20+
int quantum_start_time;
21+
} PROCESS_TABLE_ENTRY;
22+
23+
extern PROCESS_TABLE_ENTRY process_table[];
24+
25+
26+
/* A quantum is 40 ms */
27+
28+
#define QUANTUM 40
29+
30+
PROCESS_TABLE_ENTRY process_table[MAX_NUMBER_OF_PROCESSES];
31+
32+
33+
/* You may use this code for maintaining a queue of
34+
ready processes. It's simply a linked list structure
35+
with each element containing the PID of a ready process */
36+
37+
typedef struct ready_queue_elt {
38+
struct ready_queue_elt *next;
39+
PID_type pid;
40+
} READY_QUEUE_ELT;
41+
42+
43+
READY_QUEUE_ELT *ready_queue_head = NULL; /* head of the event queue */
44+
READY_QUEUE_ELT *ready_queue_tail = NULL;
45+
46+
47+
//places process id at end of ready queue
48+
49+
void queue_ready_process(PID_type pid)
50+
{
51+
// printf("QUEUEING ready PID %d\n", pid);
52+
READY_QUEUE_ELT *p = (READY_QUEUE_ELT *) malloc(sizeof(READY_QUEUE_ELT));
53+
p->pid = pid;
54+
p->next = NULL;
55+
if (ready_queue_tail == NULL)
56+
if (ready_queue_head == NULL) {
57+
ready_queue_head = ready_queue_tail = p;
58+
p->next = NULL;
59+
}
60+
else {
61+
printf("Error: ready queue tail is NULL but ready_queue_head is not\n");
62+
exit(1);
63+
}
64+
else {
65+
ready_queue_tail->next = p;
66+
ready_queue_tail = p;
67+
}
68+
}
69+
70+
71+
72+
73+
//removes and returns PID from front of ready queue
74+
75+
PID_type dequeue_ready_process()
76+
{
77+
if (ready_queue_head == NULL)
78+
if (ready_queue_tail == NULL)
79+
return IDLE_PROCESS; // indicates no active process is ready
80+
else {
81+
printf("Error: ready_queue_head is NULL but ready_queue_tail is not\n");
82+
exit(1);
83+
}
84+
else {
85+
READY_QUEUE_ELT *p = ready_queue_head;
86+
ready_queue_head = ready_queue_head->next;
87+
if (ready_queue_head == NULL)
88+
ready_queue_tail = NULL;
89+
return p->pid;
90+
}
91+
}
92+
93+
94+
/* This procedure is automatically called when the
95+
(simulated) machine boots up */
96+
97+
void initialize_kernel()
98+
{
99+
// Put any initialization code you want here.
100+
// Remember, the process 0 will automatically be
101+
// executed after initialization (and current_pid
102+
// will automatically be set to 0),
103+
// so the your process table should reflect that fact.
104+
}

Makefile

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Simple Makefile for Project 1
2+
3+
srcdir = .
4+
5+
CC = gcc
6+
EXE =
7+
CFLAGS = -m32
8+
9+
TARGETS = system$(EXE)
10+
11+
all: $(TARGETS)
12+
13+
system$(EXE): $(srcdir)/kernel.o $(srcdir)/drivers.o $(srcdir)/hardware.o
14+
$(CC) -o system$(EXE) $(CFLAGS) $(srcdir)/kernel.o $(srcdir)/hardware.o $(srcdir)/drivers.o
15+

ben_system

19.2 KB
Binary file not shown.

drivers.h

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
#define DISK_READ_OVERHEAD 50 /* 50 ms read overhead */
3+
4+
#define BLOCK_READ_TIME 1 /* 1 ms per disk block */
5+
6+
/* The disk read takes an overhead of 50ms + (1ms * size of data) */
7+
8+
/* This is the driver routine to call to issue a
9+
(blocking) disk read request. An interrupt will automatically
10+
occur when the read has completed */
11+
12+
extern void disk_read_req(PID_type pid, int size);
13+
14+
/* For our purposes, the reading the keyboard
15+
buffer takes 100 milliseconds */
16+
17+
#define KEYBOARD_READ_OVERHEAD 100
18+
19+
/* This is the driver routine to call to issue a
20+
(blocking) keyboard read. An interrupt will automatically
21+
occur when the read has completed */
22+
23+
extern void keyboard_read_req(PID_type pid);
24+
25+
26+
/* This is the driver routine to call to issue a
27+
(non-blocking) disk write. It returns immediately
28+
(i.e. the write is assumed to take 0 time). */
29+
30+
extern void disk_write_req(PID_type pid);
31+
32+

drivers.o

1.04 KB
Binary file not shown.

hardware.h

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
2+
typedef int BOOL;
3+
#define TRUE 1
4+
#define FALSE 0
5+
6+
/* A PID is just an int */
7+
8+
typedef int PID_type;
9+
10+
/* This is the variable (e.g. a special register) containing
11+
the PID of the process that is currently running */
12+
13+
extern PID_type current_pid;
14+
15+
16+
/* This defines the maximum number of processes that can
17+
be supported on this machine */
18+
19+
#define MAX_NUMBER_OF_PROCESSES 20
20+
21+
/* When there is no process ready to run, the current_pid
22+
should be set to the pid of the "idle process",
23+
i.e IDLE_PROCESS (with a pid of -1). */
24+
25+
#define IDLE_PROCESS -1
26+
27+
28+
/* Time is kept as an unsigned int, in units of
29+
milliseconds */
30+
31+
typedef unsigned int CLOCK_TIME; /* time in ms */
32+
33+
34+
/* These are the machine's registers that are used
35+
to store information about trap or interrupt
36+
that has occurred (see below). */
37+
38+
extern int R1, R2, R3, R4; /* registers for holding
39+
trap or interrupt info */
40+
41+
42+
/* This is the clock holding the current time (in ms).
43+
Only the hardware can change the value of it, do
44+
not write to it! */
45+
46+
extern CLOCK_TIME clock; /* system clock in ms */
47+
48+
/* A clock interrupt is generated by the hardware every 10 ms.
49+
Note that this may be shorter than the quantum given to
50+
each process. */
51+
52+
#define CLOCK_INTERRUPT_PERIOD 10
53+
54+
55+
56+
/* These are the interrupt numbers for various different interrupts (including
57+
a trap). IMPORTANT: An interrupt number corresponds to the element in the
58+
INTERRUPT_TABLE containing a pointer to the interrupt handler */
59+
60+
61+
/* A Trap is interrupt 0. Note the description below about the state of
62+
the registers when a trap occurs, in order to determine why the trap
63+
occurred (disk read request, keyboard request, etc.) */
64+
65+
#define TRAP 0
66+
67+
68+
/* The clock interrupt is interrupt 1. */
69+
70+
#define CLOCK_INTERRUPT 1
71+
72+
73+
/* A disk interrupt, issued by the hardware when a requested disk read
74+
operation has completed, is interrupt 2. The PID of process that requested the
75+
disk read is placed in R1 by the hardware */
76+
77+
#define DISK_INTERRUPT 2
78+
79+
80+
/* A keyboard interrupt, issued by the hardware when a requested keyboard read
81+
operation has completed, is interrupt 2. The PID of process that requested the
82+
keyboard read is placed in R1 by the hardware */
83+
84+
#define KEYBOARD_INTERRUPT 3 /* occurs when requested keyboard buffer is available */
85+
86+
87+
/* The various kinds of TRAPs are listed below. When a trap occurs (via
88+
interrupt 0, see above) register R1 will contain one of the following
89+
values. Register R2 will, in some of the cases as described below,
90+
contain a value as well. */
91+
92+
#define DISK_READ 0 /* Size of data should be placed in R2 */
93+
#define DISK_WRITE 1 /* non-blocking write to disk */
94+
#define KEYBOARD_READ 2 /* Blocking read of keyboard buffer */
95+
#define FORK_PROGRAM 3 /* PID of new process will be in R2 */
96+
#define END_PROGRAM 4 /* PID of ending processing will be in R2 */
97+
98+
99+
/* The interrupt table, INTERRUPT_TABLE, is an array of pointers
100+
to functions (with no arguments and no return type). For
101+
example, if the handler for a trap is the function defined by
102+
103+
void handle_trap() { ... }
104+
105+
then the appropriate element of the interrupt table can be assigned
106+
as follows:
107+
108+
INTERRUPT_TABLE[TRAP] = handle_trap;
109+
110+
When a trap occurs (due to a system call by the running process),
111+
the function that INTERRUPT_TABLE[TRAP] points to will automatically
112+
be called. */
113+
114+
typedef void (*FN_TYPE)();
115+
116+
extern FN_TYPE INTERRUPT_TABLE[];
117+

hardware.o

6.55 KB
Binary file not shown.

0 commit comments

Comments
 (0)