The operating system records all processes and pushes them forward in a reasonable order (resource allocation, process scheduling), which is the multi-process image.

Multi-process image exists in the whole life cycle of operating system and is the most core image of operating system.

The end of the main function in main.c if(! fork()) {init(); }, creates a child process in which the shell is opened.

int main(int argc, char* argv[]) {
	while(1) {
    	scanf("%s", cmd);
        if(! fork()) { exec(cmd); wait(); }}}Copy the code

How are multiple processes organized?

Operating system awareness processes and organizational processes all depend on PCB. At any time, the operating system has only one running process, that is, a running process, whose PCB information exists in the CPU.

The operating system maintains one ready queue and n wait queues (such as disk wait queues and printer wait queues). The elements of the ready queue are the PCB structures of the processes in the ready state, and the elements of the wait queue are the PCB structures of the processes in the blocked state.

How do multiple processes alternate?

/ / pseudo code

cur_pcb.state = 'W';				// Start disk read and write and set its state to blocked
disk_wait_queue.append(cur_pcb);	// Put cur_PCB into disk wait queue
schedule();				

schedule() {
	new_pcb = get_next(ready_queue);	// Get a new_PCB from the ready queue
    switch_to(cur_pcb, new_pcb);		// Switch to new_PCB and save the scene} switch_to(cur_pcb, new_pcb) { cur_pcb.ax = cpu.ax; cur_pcb.bx = cpu.bx; . cur_pcb.cs = cpu.cs; cpu.ax = new_pcb.ax; cpu.bx = new_pcb.bx; . cpu.cs = new_pcb.cs; }Copy the code

How does multiple processes affect it?

Consider the problem posed by the following code.

; process1; process2
mov [100], ax		 100:...Copy the code

Will process 1 change to the memory space where process 2 code resides? The answer is no, each process has its own address space, and 100 is a logical address that maps to a physical address. Process management drives memory management.

How do multiple processes collaborate?

Consider two processes adding files to the queue to be printed. If synchronization is not in place, files in the print queue will be scrambled when the process switches. Process management also raises the issue of process synchronization.

How to form multi-process images?

① Read and write PCB, the most important structure in OS, throughout; (2) Register switch is completed; Write process scheduler; (4) process synchronization and cooperation; ⑤ There should be a mapping from logical address to physical address.