POSIX API (fjnish) / Scheduling intro
1
POSIX API (fjnish) / Scheduling intro 1 last time shells: program - - PowerPoint PPT Presentation
POSIX API (fjnish) / Scheduling intro 1 last time shells: program for users to run other programs fjles: open before use, read/write bytes, explicit close fjle descriptor: index into per-process table fork: copy table same index refers to
1
2
3
4
int pipe_fds[2]; pipe(pipe_fds); pid_t p = fork(); if (p == 0) { close(pipe_fds[0]); for (int i = 0; i < 10; ++i) { char c = '0' + i; write(pipe_fds[1], &c, 1); } exit(0); } close(pipe_fds[1]); char buffer[10]; ssize_t count = read(pipe_fds[0], buffer, 10); for (int i = 0; i < count; ++i) { printf("%c", buffer[i]); }
5
int pipe_fds[2]; pipe(pipe_fds); pid_t p = fork(); if (p == 0) { close(pipe_fds[0]); for (int i = 0; i < 10; ++i) { char c = '0' + i; write(pipe_fds[1], &c, 1); } exit(0); } close(pipe_fds[1]); char buffer[10]; ssize_t count = read(pipe_fds[0], buffer, 10); for (int i = 0; i < count; ++i) { printf("%c", buffer[i]); }
5
6
7
8
9
10
10
10
10
10
11
// Per-process state struct proc { uint sz; // Size of process memory (bytes) pde_t* pgdir; // Page table char *kstack; // Bottom of kernel stack for this process enum procstate state; // Process state int pid; // Process ID struct proc *parent; // Parent process struct trapframe *tf; // Trap frame for current syscall struct context *context; // swtch() here to run process void *chan; // If non-zero, sleeping on chan int killed; // If non-zero, have been killed struct file *ofile[NOFILE]; // Open files struct inode *cwd; // Current directory char name[16]; // Process name (debugging) };
12
// Per-process state struct proc { uint sz; // Size of process memory (bytes) pde_t* pgdir; // Page table char *kstack; // Bottom of kernel stack for this process enum procstate state; // Process state int pid; // Process ID struct proc *parent; // Parent process struct trapframe *tf; // Trap frame for current syscall struct context *context; // swtch() here to run process void *chan; // If non-zero, sleeping on chan int killed; // If non-zero, have been killed struct file *ofile[NOFILE]; // Open files struct inode *cwd; // Current directory char name[16]; // Process name (debugging) };
12
13
(xv6: EMBRYO)
(xv6: RUNNABLE)
(xv6: RUNNING)
(xv6: SLEEPING)
(xv6: ZOMBIE)
14
(xv6: EMBRYO)
(xv6: RUNNABLE)
(xv6: RUNNING)
(xv6: SLEEPING)
(xv6: ZOMBIE)
14
(xv6: EMBRYO)
(xv6: RUNNABLE)
(xv6: RUNNING)
(xv6: SLEEPING)
(xv6: ZOMBIE)
14
(xv6: EMBRYO)
(xv6: RUNNABLE)
(xv6: RUNNING)
(xv6: SLEEPING)
(xv6: ZOMBIE)
14
(xv6: EMBRYO)
(xv6: RUNNABLE)
(xv6: RUNNING)
(xv6: SLEEPING)
(xv6: ZOMBIE)
14
(xv6: EMBRYO)
(xv6: RUNNABLE)
(xv6: RUNNING)
(xv6: SLEEPING)
(xv6: ZOMBIE)
14
15
15
15
16
17
18
19
20
21
21
21
21
21
21
22
22
22
22
22
23
23
23
24
24
24
24
24
24
24
24
25
25
25
25
25
26
27
28
29