Skip to content

Commit f6ddea7

Browse files
author
eminakgun
committedMay 20, 2024
Add initial fork implementation
1 parent 22bf3f7 commit f6ddea7

File tree

7 files changed

+242
-230
lines changed

7 files changed

+242
-230
lines changed
 

‎makefile ‎Makefile

+5-14
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ GCCPARAMS = -m32 -Iinclude -fno-use-cxa-atexit -nostdlib -fno-builtin -fno-rtti
66
ASPARAMS = --32
77
LDPARAMS = -melf_i386
88

9+
VirtualBoxVM = /mnt/c/Program\ Files/Oracle/VirtualBox/VirtualBoxVM.exe
10+
911
objects = obj/loader.o \
1012
obj/gdt.o \
1113
obj/memorymanagement.o \
@@ -19,23 +21,12 @@ objects = obj/loader.o \
1921
obj/hardwarecommunication/pci.o \
2022
obj/drivers/keyboard.o \
2123
obj/drivers/mouse.o \
22-
obj/drivers/vga.o \
23-
obj/drivers/ata.o \
24-
obj/gui/widget.o \
25-
obj/gui/window.o \
26-
obj/gui/desktop.o \
27-
obj/net/etherframe.o \
28-
obj/net/arp.o \
29-
obj/net/ipv4.o \
30-
obj/net/icmp.o \
31-
obj/net/udp.o \
32-
obj/net/tcp.o \
3324
obj/kernel.o
3425

3526

36-
run: mykernel.iso
37-
(killall VirtualBox && sleep 1) || true
38-
VirtualBox --startvm 'My Operating System' &
27+
run: clean mykernel.iso
28+
(killall $(VirtualBoxVM) && sleep 1) || true
29+
$(VirtualBoxVM) --startvm 'EminOS' &
3930

4031
obj/%.o: src/%.cpp
4132
mkdir -p $(@D)

‎ReadMe.txt

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
3+
On fork
4+
The child process and the parent process run in separate memory spaces. At the time of fork() both memory spaces have the same content.
5+
Memory writes, file mappings (mmap(2)), and unmappings (munmap(2)) performed by one of the processes do not affect the other.
6+
The child has its own unique process ID,.
7+
The child's parent process ID is the same as the parent's process ID.
8+
- RETURN VALUE
9+
On success, the PID of the child process is returned in the parent, and 0 is returned in the child. On failure, -1 is returned in the parent, no child process is created, and errno is set appropriately.

‎include/multitasking.h

+66-14
Original file line numberDiff line numberDiff line change
@@ -5,59 +5,111 @@
55
#include <common/types.h>
66
#include <gdt.h>
77

8+
using namespace myos;
9+
using namespace myos::common;
10+
11+
void printf(char* str);
12+
void printInt(int i);
13+
14+
815
namespace myos
916
{
17+
18+
enum ProcessState {
19+
READY,
20+
RUNNING,
21+
BLOCKED,
22+
TERMINATED
23+
};
24+
1025

1126
struct CPUState
1227
{
13-
common::uint32_t eax;
14-
common::uint32_t ebx;
15-
common::uint32_t ecx;
16-
common::uint32_t edx;
28+
uint32_t eax;
29+
uint32_t ebx;
30+
uint32_t ecx;
31+
uint32_t edx;
1732

18-
common::uint32_t esi;
19-
common::uint32_t edi;
20-
common::uint32_t ebp;
33+
uint32_t esi;
34+
uint32_t edi;
35+
uint32_t ebp;
2136

2237
/*
2338
common::uint32_t gs;
2439
common::uint32_t fs;
2540
common::uint32_t es;
2641
common::uint32_t ds;
2742
*/
28-
common::uint32_t error;
43+
uint32_t error;
2944

30-
common::uint32_t eip;
31-
common::uint32_t cs;
32-
common::uint32_t eflags;
33-
common::uint32_t esp;
34-
common::uint32_t ss;
45+
// already on stack:
46+
uint32_t eip;
47+
uint32_t cs;
48+
uint32_t eflags;
49+
uint32_t esp;
50+
uint32_t ss;
3551
} __attribute__((packed));
3652

53+
54+
struct PCB
55+
{
56+
uint32_t pid; // Process ID
57+
uint32_t ppid; // Parent Process ID
58+
ProcessState state;
59+
};
3760

3861
class Task
3962
{
4063
friend class TaskManager;
4164
private:
4265
common::uint8_t stack[4096]; // 4 KiB
4366
CPUState* cpustate;
67+
PCB pcb;
68+
4469
public:
4570
Task(GlobalDescriptorTable *gdt, void entrypoint());
4671
~Task();
72+
uint8_t* StackStartAddr(){return stack;};
73+
uint32_t GetPID(){return pcb.pid;};
74+
void SetCPUState(CPUState* cpustate_) {cpustate = cpustate_;};
4775
};
4876

4977

5078
class TaskManager
5179
{
80+
friend class SyscallHandler;
5281
private:
53-
Task* tasks[256];
82+
Task* tasks[256]; // aka process-table
5483
int numTasks;
5584
int currentTask;
5685
public:
5786
TaskManager();
5887
~TaskManager();
88+
Task* GetCurrent();
89+
Task* AddTask();
5990
bool AddTask(Task* task);
6091
CPUState* Schedule(CPUState* cpustate);
92+
void printTasks(){
93+
for (size_t i = 0; i < numTasks; i++)
94+
{
95+
printf("\nTask number: ");
96+
printInt(i);
97+
printf("\n\t");
98+
99+
printf("pid: ");
100+
printInt(tasks[i]->pcb.pid);
101+
printf("\n\t");
102+
103+
printf("ppid: ");
104+
printInt(tasks[i]->pcb.ppid);
105+
printf("\n\t");
106+
107+
printf("state: ");
108+
printInt(tasks[i]->pcb.state);
109+
printf("\n");
110+
}
111+
112+
}
61113
};
62114

63115

‎include/syscalls.h

+6-3
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@
88

99
namespace myos
1010
{
11+
12+
const uint32_t FORK_INT = 2;
1113

1214
class SyscallHandler : public hardwarecommunication::InterruptHandler
1315
{
14-
16+
private:
17+
TaskManager* taskManager;
1518
public:
16-
SyscallHandler(hardwarecommunication::InterruptManager* interruptManager, myos::common::uint8_t InterruptNumber);
19+
SyscallHandler(hardwarecommunication::InterruptManager* interruptManager, myos::common::uint8_t InterruptNumber, TaskManager* taskManager);
1720
~SyscallHandler();
1821

1922
virtual myos::common::uint32_t HandleInterrupt(myos::common::uint32_t esp);
20-
23+
void fork(uint32_t esp);
2124
};
2225

2326

0 commit comments

Comments
 (0)
Please sign in to comment.