Intro
the job of an operating system 操作系统的作用
- share a computer among multiple programs and to provide a more useful set of services than the hardware alone supports. 帮助多个程序共享电脑硬件资源,提供更为有效的服务
- manages and abstracts the low-level hardware 管控和抽象底层硬件
constituion of a process 进程组成成分
- instructions 指令: implement the program's computation
- data 数据: the variables on which the computation acts
- stack 栈: organizes the program's procedure calls
the interaction between user process and kernel

- invokes a system call :调用系统调用
- enters the kernel :进入内核
- kernel performs the services and returns :内核执行服务并返回结果
the job of the kernel
- uses the hardward protection mechanisms provided by a CPU to ensure that each process executing in user space can access only its own memory. 使用由cpu提供的硬件的保护机制来使得每个进程只能访问他们自己的内存
- the kernel executes with the hardware privileges required to implement these protections. 内核通过硬件访问特权来实现硬件保护机制
Processes and memory
constitution of an process
- user-space memory 用户态空间
- per-process state private to the kernel 仅有内核访问的进程状态
XV-6 time-shares programs
- it transparently switches the available CPUS among the set of processes waiting to execute
- when a process is not executing, xv6 saves its CPU registers, restoring them when it next runs the process
PID : kernel associates a process identifier or PID, with each process.
fork
- a system call that can be invoked by a process to create a new process 是一个可以创建新进程的系统调用
- 子进程只执行fork后面的代码,fork前的代码不执行
- the new process is called child process,with exactly the same memory contents as the calling process, called the parent processl 新进程被称为子进程,与父进程拥有相同的内存内容,但是存储其内容的地址不一样,所以父子进程的变量相互不影响
- Fork returns in both the parent and the child. In the parent, fork returns the child's PID; in the child, fork returns zero. fork同时向父进程和子进程返回返回结果。父进程中fork方法返回子进程的pid,子进程中,fork返回0
int pid = fork();
if(pid > 0){
printf("parent: child=%d\n", pid);
pid = wait((int *) 0);
printf("child %d is done\n", pid);
} else if(pid == 0){
printf("child: exiting\n");
exit(0);
} else {
printf("fork error\n");
}
the relation betweeen parent and child process
- although the child has the same memory contents as the parent initiallym the parent and child are executing with different memory and different rigisters: changing a variable in one does not affect the other. 只是相同的内容,但是位置不一样。
exit
- the exit system call causes the calling process to stop executing and to release resources such as memory and open files. Exit takes an integer status argument, conventionally 0 to indicate success and 1 to indicate failure.
wait
- the wait system call returens the PID od an exited(or killed) child of the current process and copies the exit status of the child to the address passed to wait; if non of the caller's children has exited, wait waits for one to do so.
- if the caller has no childrem, wait immediately returns -1
- if the parent doesn't care about the exit statues of a child, it can pass a 0 address to wait.
exec
- the exec system call replaces the calling process's memory with a new memory image loaded from a file stored in the file system.
- the file must have a particular format, which part is date, at which instruction to start
- when exec succeeds, it does not return to the calling program; instead, the instructions loaded from the file start executing at the entry point declared in the ELF header
- Exec takes two arguments: the name of the file containing the executable and an array of string arguments
I/O and File descriptors
file decriptor
- A file decriptor is a small integer representing a kernel-managed object that a process may read from or write to.
- the file decriptor interface abstracts away the differences between files, pipes, and devicesm making them all look like streams of bytes.
- By convention, a process reads from file descriptor 0(standard input), writes output to file descriptor 1(standard output), and writes error messages to file descriptor 2(standard error)
related system call
- write :The call write(fd, buf, n) writes n bytes from buf to the fifile descriptor fd and returns the number of bytes written.
- read : The call read(fd, buf, n) reads at most n bytes from the fifile descriptor fd, copies them into buf, and returns the number of bytes read.
- dup : the dup system call duplicates an existing file descriptor, returning a new one that refers to the same underlying I/O object. Both file descriptors share an offset, just as the file descriptors dumlicated by fork do.
Pipes
defination and job:
- A pipe is a small kernel buffer exposed to processes as a pair of fifile descriptors, one for reading and one for writing.
- Writing data to one end of the pipe makes that data available for reading from the other end of the pipe.
- Pipes provide a way for processes to communicate.
int p[2];
pipe(p);
- pipe, which creates a new pipe and records the read and write fifile descriptors in the array p ,