The first assignment: in-depth source code analysis of the xv6 process model

1. Process

1.1 The concept of process

1) Narrow definition: A process is an instance of a running program.

2) Broad definition: A process is a running activity of a program with a certain independent function about a data set. It is the basic unit of the dynamic execution of the operating system. In the traditional operating system, the process is not only the basic allocation unit, but also the basic execution unit.

1.2 The composition of the process

1.3 Process Control Block


Quote from: https://blog.csdn.net/hgnuxc_1993/article/details/54847732

2. How the operating system organizes processes 

  • The organizational process is understood here as the management and control process
  • The operating system manages and controls the process through the PCB. When each process is created, the operating system generates a PCB for it. At the same time, a unique process is marked with a unique PID, and the status of the process can be known through the PCB; when the process ends, the operation The system destroys the PCB corresponding to the process.  
  • xv6PCB structure, see appendix

 

 

 

3. How the process state transitions

 

4. How processes are scheduled

The current process performs process switching by calling the yield function. The yield function calls the sched function, and the sched function starts the swtch function to complete the process switch. The whole process is like this:

yield => sched => swtch 

sched is an infinite loop that continuously scans the process table and selects a RUNNABLE process scheduling, that is, switching from the scheduler switch to the newly selected process

swatch function tasks: 1. Save the context of the current (old) process. 2. Load the context of the new process (new) into the machine registers.

Reference URL: https://blog.csdn.net/Swartz2015/article/details/61615603

See appendix for functions

5. Talk about your views on the operating system process model

When I was studying the process part, I didn't pay attention to the requirements of this job. I paid more attention to the part from power-on to process creation, real mode and protected mode. So I added this knowledge in a hurry, and I don't have a deep understanding of scheduling.

Attached are some reading notes, see appendix.

6. References

  • https://legacy.gitbook.com/book/th0ar/xv6-chinese/details
  • https://pdos.csail.mit.edu/6.828/2012/xv6/xv6-rev7.pdf
  • https://pdos.csail.mit.edu/6.828/2012/xv6/book-rev7.pdf

7.附录

  • PCB结构

struct proc {

uint sz; // 进程的内存大小(以byte计)
pde_t* pgdir; // 进程页路径的线性地址。
char *kstack; // 进程的内核栈底
enum procstate state; // 进程状态
volatile int pid; // 进程ID
struct proc *parent; // 父进程
struct trapframe *tf; // 当前系统调用的中断帧
struct context *context; // 进程运行的入口
int killed; // 当非0时,表示已结束
struct file *ofile[NOFILE]; // 打开的文件列表
struct inode *cwd; // 进程当前路径
char name[16]; // 进程名称
};

  •  void scheduler(void)

{

struct proc *p;

for(;;)

{

// Enable interrupts on this processor.

sti();

// Loop over process table looking for process to run.

acquire(&ptable.lock);

for(p = ptable.proc; p < &ptable.proc[NPROC]; p++)

{

if(p->state != RUNNABLE)

continue;

// Switch to chosen process. It is the process's job

// to release ptable.lock and then reacquire it

// before jumping back to us. proc = p;

switchuvm(p); p->state = RUNNING;

swtch(&cpu->scheduler, proc->context);

switchkvm(); // Process is done running for now.

// It should have changed its p->state before coming back.

proc = 0;

}

release(&ptable.lock);

}

}

  • swtch的函数代码如下

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325164127&siteId=291194637