Skip to content

Commit 690502f

Browse files
committed
Add simple multiprocessing example and changed scheduling algorithm to RR
1 parent 2afec56 commit 690502f

File tree

6 files changed

+36
-18
lines changed

6 files changed

+36
-18
lines changed

src/kernel/process/allocation.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,21 +123,23 @@ static int create_infant_process_argv_stack(int user_ds, int user_sp,
123123
return user_sp;
124124
}
125125

126-
static int get_cold_pid() {
127-
int pid = -1;
126+
static int get_cold_pid_to_allocate() {
127+
static int pid = 0;
128+
pid = 0; // BUG: What PID wrap around doesn't work?
128129
for (int i = 0; i < MAX_PROCESS; ++i) {
129-
if(i==PID_KERNEL) continue;
130-
if(processes[i].state == STATE_COLD) {
131-
pid = i;
132-
break;
130+
pid = (pid+1)%MAX_PROCESS;
131+
if (pid!=PID_KERNEL && processes[pid].state == STATE_COLD) {
132+
// return a PID to allocate
133+
return pid;
133134
}
134135
}
135-
return pid;
136+
// no free PID found.
137+
return -1;
136138
}
137139

138140
int process_create(unsigned int ppid, int argc, char *argv[]) {
139141
// returnd pid >= 0 if success
140-
int pid = get_cold_pid();
142+
int pid = get_cold_pid_to_allocate();
141143
if(pid < 0) return pid;
142144
struct Process *process = &processes[pid];
143145
// reset state

src/kernel/process/process.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ int syscall_1_process_wait(int pid, int blocked_on_pid) {
4747
int syscall_1_process_fork(int user_pid, int op) {
4848
switch (op) {
4949
case SYSCALL_PROCESS_SUB_FORK_MARK_READY:
50-
// print_log("FORK REQUESTED:: %d", user_pid);
5150
return process_fork_mark_ready(user_pid);
5251
case SYSCALL_PROCESS_SUB_FORK_CHECK_READY:
5352
return process_fork_check_ready(user_pid);

src/kernel/process/scheduler.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void process_scheduler_stash_state() {
4646

4747
int process_scheduler_get_next_pid(int lastpid) {
4848
// execute one of the scheduling algorithm
49-
return process_scheduler_largestpid(lastpid);
49+
return process_scheduler_rr(lastpid);
5050
}
5151

5252
static void handle_fork(unsigned int ppid, struct Process *process) {
@@ -120,8 +120,6 @@ void process_scheduler(int *_e_ip, int *_e_cs, int *_e_sp, int *_e_ss) {
120120
}
121121

122122
int process_waitpid(unsigned int pid, unsigned int blocked_on_pid) {
123-
// TODO: this implementation has flaws.
124-
// We need to do better something.
125123
// It currently allows blocking on any pid, and rely on syscall client
126124
// for yield.
127125
struct Process *other_process = get_process(blocked_on_pid);

src/usr/local/src/ls.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// list directory
22
#include <stdio.h>
33
#include <dirent.h>
4-
#include <process.h>
54

65
void process() {
76
struct DIR dir;
@@ -14,9 +13,6 @@ void process() {
1413
}
1514

1615
int main(int argc,char *argv[]) {
17-
18-
int p = fork();
19-
printf("fork() = %d\n", p);
2016
process();
2117
return 0;
2218
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// multiprocessing example
2+
#include <stdio.h>
3+
#include <process.h>
4+
5+
6+
int main(int argc,char *argv[]) {
7+
printf("main process, executing fork()\n");
8+
int pid = fork();
9+
printf("fork()=%d\n", pid);
10+
int am_i_child;
11+
if(pid == 0) {
12+
// child process
13+
am_i_child=1;
14+
printf("child process: says Hi\n");
15+
} else {
16+
am_i_child=0;
17+
// parent process
18+
printf("parent process: says Hi\n");
19+
printf("will for child process to exit\n");
20+
waitpid(pid);
21+
}
22+
printf("process exiting. Am I child process=%s\n", (am_i_child)?"Yes":"No");
23+
24+
return 0;
25+
}

src/usr/local/src/sh.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,6 @@ int main(int argc, char *argv[]) {
8686
banner();
8787
cmd_help();
8888
int c = 0;
89-
strcpy(command, "run ls");
90-
handle_command(command);
9189
while (1) {
9290
printf("[%d] $ ", last_status_code);
9391
gets(command);

0 commit comments

Comments
 (0)