-
Notifications
You must be signed in to change notification settings - Fork 1
/
pcb.go
65 lines (53 loc) · 1.01 KB
/
pcb.go
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
package emulator
import (
"os"
"syscall"
)
var (
GPcb *Pcb
)
func GetPcb() *Pcb {
if GPcb == nil {
GPcb = NewPcb()
}
return GPcb
}
type Pcb struct {
fds map[uintptr]*VirtualFile
pid int
}
func NewPcb() *Pcb {
return &Pcb{
fds: map[uintptr]*VirtualFile{
uintptr(syscall.Stdin): NewVirtualFile("stdin", "", os.Stdin),
uintptr(syscall.Stdout): NewVirtualFile("stdout", "", os.Stdout),
uintptr(syscall.Stderr): NewVirtualFile("stderr", "", os.Stderr),
},
pid: os.Getpid(),
}
}
func (p *Pcb) GetPid() int {
return p.pid
}
func (p *Pcb) AddFd(name, nameInSystem string, fo *os.File) uintptr {
var x = NewVirtualFile(name, nameInSystem, fo)
p.fds[x.Description] = x
return x.Description
}
func (p *Pcb) GetFdDetail(fd uintptr) *VirtualFile {
ob, exist := p.fds[fd]
if !exist {
return nil
}
return ob
}
func (p *Pcb) HasFd(fd uintptr) bool {
_, exist := p.fds[fd]
return exist
}
func (p *Pcb) Remove(fd uintptr) {
if p.HasFd(fd) {
p.fds[fd].CloseResource()
delete(p.fds, fd)
}
}