Skip to content

Commit 87ce956

Browse files
committed
ch6
2 parents 332e27e + 1db741e commit 87ce956

File tree

7 files changed

+101
-3
lines changed

7 files changed

+101
-3
lines changed

os/loader.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,6 @@ int load_init_app()
6464
argv[0] = INIT_PROC;
6565
argv[1] = NULL;
6666
p->trapframe->a0 = push_argv(p, argv);
67+
add_task(p);
6768
return 0;
6869
}

os/proc.c

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "loader.h"
44
#include "trap.h"
55
#include "vm.h"
6+
#include "queue.h"
67

78
struct proc pool[NPROC];
89
__attribute__((aligned(16))) char kstack[NPROC][PAGE_SIZE];
@@ -11,6 +12,7 @@ __attribute__((aligned(4096))) char trapframe[NPROC][TRAP_PAGE_SIZE];
1112
extern char boot_stack_top[];
1213
struct proc *current_proc;
1314
struct proc idle;
15+
struct queue task_queue;
1416

1517
int threadid()
1618
{
@@ -39,6 +41,7 @@ void proc_init()
3941
idle.kstack = (uint64)boot_stack_top;
4042
idle.pid = IDLE_PID;
4143
current_proc = &idle;
44+
init_queue(&task_queue);
4245
}
4346

4447
int allocpid()
@@ -47,6 +50,24 @@ int allocpid()
4750
return PID++;
4851
}
4952

53+
struct proc *fetch_task()
54+
{
55+
int index = pop_queue(&task_queue);
56+
if (index < 0) {
57+
debugf("No task to fetch\n");
58+
return NULL;
59+
}
60+
debugf("fetch task %d(pid=%d) from task queue\n", index,
61+
pool[index].pid);
62+
return pool + index;
63+
}
64+
65+
void add_task(struct proc *p)
66+
{
67+
push_queue(&task_queue, p - pool);
68+
debugf("add task %d(pid=%d) to task queue\n", p - pool, p->pid);
69+
}
70+
5071
// Look in the process table for an UNUSED proc.
5172
// If found, initialize state required to run in the kernel.
5273
// If there are no free procs, or a memory allocation fails, return 0.
@@ -98,19 +119,27 @@ void scheduler()
98119
{
99120
struct proc *p;
100121
for (;;) {
101-
int has_proc = 0;
122+
/*int has_proc = 0;
102123
for (p = pool; p < &pool[NPROC]; p++) {
103124
if (p->state == RUNNABLE) {
104125
has_proc = 1;
105-
debugf("swtich to proc %d", p - pool);
126+
tracef("swtich to proc %d", p - pool);
106127
p->state = RUNNING;
107128
current_proc = p;
108129
swtch(&idle.context, &p->context);
109130
}
110131
}
111132
if(has_proc == 0) {
112133
panic("all app are over!\n");
134+
}*/
135+
p = fetch_task();
136+
if (p == NULL) {
137+
panic("all app are over!\n");
113138
}
139+
tracef("swtich to proc %d", p - pool);
140+
p->state = RUNNING;
141+
current_proc = p;
142+
swtch(&idle.context, &p->context);
114143
}
115144
}
116145

@@ -133,6 +162,7 @@ void sched()
133162
void yield()
134163
{
135164
current_proc->state = RUNNABLE;
165+
add_task(current_proc);
136166
sched();
137167
}
138168

@@ -186,6 +216,7 @@ int fork()
186216
np->trapframe->a0 = 0;
187217
np->parent = p;
188218
np->state = RUNNABLE;
219+
add_task(np);
189220
return np->pid;
190221
}
191222

@@ -266,6 +297,7 @@ int wait(int pid, int *code)
266297
return -1;
267298
}
268299
p->state = RUNNABLE;
300+
add_task(p);
269301
sched();
270302
}
271303
}

os/proc.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ struct proc {
4343
uint64 max_page;
4444
struct proc *parent; // Parent process
4545
uint64 exit_code;
46-
struct file *files[FD_BUFFER_SIZE]; //File descriptor table, using to record the files opened by the process
46+
struct file *files
47+
[FD_BUFFER_SIZE]; //File descriptor table, using to record the files opened by the process
4748
};
4849

4950
int cpuid();
@@ -56,6 +57,8 @@ void yield();
5657
int fork();
5758
int exec(char *, char **);
5859
int wait(int, int *);
60+
void add_task(struct proc *);
61+
struct proc *pop_task();
5962
struct proc *allocproc();
6063
int fdalloc(struct file *);
6164
int init_stdio(struct proc *);

os/queue.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "queue.h"
2+
#include "defs.h"
3+
4+
void init_queue(struct queue *q)
5+
{
6+
q->front = q->tail = 0;
7+
q->empty = 1;
8+
}
9+
10+
void push_queue(struct queue *q, int value)
11+
{
12+
if (!q->empty && q->front == q->tail) {
13+
panic("queue shouldn't be overflow");
14+
}
15+
q->empty = 0;
16+
q->data[q->tail] = value;
17+
q->tail = (q->tail + 1) % NPROC;
18+
}
19+
20+
int pop_queue(struct queue *q)
21+
{
22+
if (q->empty)
23+
return -1;
24+
int value = q->data[q->front];
25+
q->front = (q->front + 1) % NPROC;
26+
if (q->front == q->tail)
27+
q->empty = 1;
28+
return value;
29+
}

os/queue.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef QUEUE_H
2+
#define QUEUE_H
3+
#define QUEUE_SIZE (1024)
4+
5+
// TODO: change the queue to a priority queue sorted by priority
6+
7+
struct queue {
8+
int data[QUEUE_SIZE];
9+
int front;
10+
int tail;
11+
int empty;
12+
};
13+
14+
void init_queue(struct queue *);
15+
void push_queue(struct queue *, int);
16+
int pop_queue(struct queue *);
17+
18+
#endif // QUEUE_H

os/syscall.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,18 @@ uint64 sys_wait(int pid, uint64 va)
142142
return wait(pid, code);
143143
}
144144

145+
uint64 sys_spawn(uint64 va)
146+
{
147+
// TODO: your job is to complete the sys call
148+
return -1;
149+
}
150+
151+
uint64 sys_set_priority(long long prio)
152+
{
153+
// TODO: your job is to complete the sys call
154+
return -1;
155+
}
156+
145157
uint64 sys_openat(uint64 va, uint64 omode, uint64 _flags)
146158
{
147159
struct proc *p = curr_proc();
@@ -235,6 +247,8 @@ void syscall()
235247
break;
236248
case SYS_unlinkat:
237249
ret = sys_unlinkat(args[0],args[1],args[2]);
250+
case SYS_spawn:
251+
ret = sys_spawn(args[0]);
238252
break;
239253
default:
240254
ret = -1;

os/syscall_ids.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@
277277
#define SYS_io_pgetevents 292
278278
#define SYS_rseq 293
279279
#define SYS_kexec_file_load 294
280+
#define SYS_spawn 400
280281
#define SYS_pidfd_send_signal 424
281282
#define SYS_io_uring_setup 425
282283
#define SYS_io_uring_enter 426

0 commit comments

Comments
 (0)