Data Structures
In this lesson, you will learn how the OS keeps track of the processes with the help of data structures.
We'll cover the following
The OS is a program, and like any program, it has some key data structures that track various relevant pieces of information. To track the state of each process, for example, the OS likely will keep some kind of process list for all processes that are ready and some additional information to track which process is currently running. The OS must also track, in some way, blocked processes; when an I/O event completes, the OS should make sure to wake the correct process and ready it to run again.
Information of a process that the OS tracks
The snippet below shows what type of information an OS needs to track about each process in the
// the registers xv6 will save and restore// to stop and subsequently restart a processstruct context {int eip;int esp;int ebx;int ecx;int edx;int esi;int edi;int ebp;};// the different states a process can be inenum proc_state { UNUSED, EMBRYO, SLEEPING,RUNNABLE, RUNNING, ZOMBIE };// the information xv6 tracks about each process// including its register context and statestruct proc {char *mem; // Start of process memoryuint sz; // Size of process memorychar *kstack; // Bottom of kernel stack// for this processenum proc_state state; // Process stateint pid; // Process IDstruct proc *parent; // Parent processvoid *chan; // If !zero, sleeping on chanint killed; // If !zero, has been killedstruct file *ofile[NOFILE]; // Open filesstruct inode *cwd; // Current directorystruct context context; // Switch here to run processstruct trapframe *tf; // Trap frame for the// current interrupt};
From the snippet above, you can see a couple of important pieces of information the OS tracks about a process. The register context will hold, for a stopped process, the contents of its registers. When a process is stopped, its registers will be saved to this memory location; by restoring these registers (i.e., placing their values back into the actual physical registers), the OS can resume running the process. We’ll learn more about this technique known as a context switch in future chapters.
You can also see from the snippet that there are some other states a process can be in, beyond running, ready, and blocked. Sometimes a system will have an initial state that the process is in when it is being created. Also, a process could be placed in a final state where it has exited but has not yet been cleaned up (in UNIX-based systems, this is called the wait()
) to wait for the completion of the child, and to also indicate to the OS that it can clean up any relevant data structures that referred to the now-extinct process.
ASIDE: DATA STRUCTURE — THE PROCESS LIST
Operating systems are replete with various important data structures that we will discuss in these notes. The process list (also called the task list) is the first such structure. It is one of the simpler ones, but certainly, any OS that has the ability to run multiple programs at once will have something akin to this structure in order to keep track of all the running programs in the system. Sometimes people refer to the individual structure that stores information about a process as a Process Control Block (PCB), a fancy way of talking about a C structure that contains information about each process (also sometimes called a process descriptor).
Get hands-on with 1400+ tech skills courses.