EEE3535 Operating Systems Notes (PDF)

Document Details

SpiritedWave

Uploaded by SpiritedWave

Yonsei University

Won Woo Ro, Ph.D.

Tags

operating systems process management computer science CPU virtualization

Summary

These notes cover the topic of operating systems and the concept of processes. It details the different states processes can exist in, and provides some details on how processes interact with the system, in terms of loading a program, and system calls. It also includes diagrams and examples to clarify the concepts discussed.

Full Transcript

The Process  The definition of a process: it is a running program.  CPU virtualizing (Time sharing) EEE3535...

The Process  The definition of a process: it is a running program.  CPU virtualizing (Time sharing) EEE3535 The OS can promote the illusion that many virtual CPUs Operating Systems exist. Time sharing: Running one process, then stopping it and running another No. 2: 4. The Abstraction: The Process The potential cost is performance, as each will run more slowly if the CPU(s) must be shared. Won Woo Ro, Ph.D. A Process: Machine State 4.2 Process API  One obvious component of machine state that  Create comprises a process is: Create a new process to run a program (a command into the shell or double‐click)  Memory Instructions lie in memory; the data that the running  Destroy program reads and writes sits in memory as well. Halt a runaway process Thus the memory that the process can address (called its address space) is part of the process.  Wait  Registers Wait for a process to stop running Many instructions explicitly read or update registers and  Miscellaneous Control thus clearly they are important to the execution of the process. Some kind of method to suspend a process and then resume it Special registers: program counter (PC), stack pointer, and  Status frame pointer Get some status info about a process 4.3 Process Creation 4.3 Process Creation  Load a program code and any static data into  The program’s heap is created and used for explicitly memory, into the address space of the process. requested dynamically allocated data Programs initially reside on disk (flash‐based SSD) in Program request such space by calling malloc() and executable format. free it by calling free()  The program’s run‐time stack is allocated.  The OS do some other initialization tasks. Use the stack for local variables, function parameters, and Input/output (I/O) setup return address. Each process by default has three open file descriptors: Initialize the stack with arguments  argc and the argv standard input, output, and error array of main() function  Start the program running at the entry point, namely main() The OS transfers control of the CPU to the newly‐created process. Loading: From Program To Process 4.4 Process States CPU Memory  Running code A process is running on a processor (Executing static data heap Instructions).  Ready A process is ready to run but for some reason the OS has stack chosen not to run it at this given moment. Process  Blocked A process has performed some kind of operation that Loading: makes it not ready to run until some other event takes Takes on-disk program place. code and reads it into the static data address space of process When a process initiates an I/O request to a disk, it Program becomes blocked and thus some other process can use the processor. Disk Process State Transition Descheduled Running Ready Scheduled I/O: initiate I/O: done Blocked 4.5 Data Structures Example) The xv6 kernel Proc Structure  The OS has some key data structures that track // the registers xv6 will save and restore various relevant pieces of information // to stop and subsequently restart a process struct context { Process list: Ready processes, current running process, int eip; // Index pointer register blocked processes, int esp; // Stack pointer register int ebx; // Called the base register Register context int ecx; // Called the counter register int edx; // Called the data register  PCB(Process Control Block) int esi; int edi; // Source index register // Destination index register A C‐structure that contains information about each }; int ebp; // Stack base pointer register process. // the different states a process can be in enum proc_state { UNUSED, EMBRYO, SLEEPING, RUNNABLE, RUNNING, ZOMBIE }; Example) The xv6 kernel Proc Structure (Cont.) // the information xv6 tracks about each process // including its register context and state struct proc { char *mem; // Start of process memory uint sz; // Size of process memory char *kstack; // Bottom of kernel stack // for this process enum proc_state state; // Process state int pid; // Process ID struct proc *parent; // Parent process void *chan; // If non-zero, sleeping on chan int killed; // If non-zero, have been killed struct file *ofile[NOFILE]; // Open files 5. Interlude: Process API struct inode *cwd; // Current directory struct context context; // Switch here to run process struct trapframe *tf; // Trap frame for the // current interrupt }; 5.1 The fork() System Call Calling fork() example (Cont.) Result (Not deterministic)  Create a new process prompt>./p1 The newly‐created process has its own copy of the address hello world (pid:29146) hello, I am parent of 29147 (pid:29146) space, registers, and PC. hello, I am child (pid:29147) prompt> p1.c #include or #include prompt>./p1 #include hello world (pid:29146) hello, I am child (pid:29147) int main(int argc, char *argv[]){ hello, I am parent of 29147 (pid:29146) printf("hello world (pid:%d)\n", (int) getpid()); prompt> int rc = fork(); if (rc < 0) { // fork failed; exit fprintf(stderr, "fork failed\n"); exit(1); } else if (rc == 0) { // child (new process) printf("hello, I am child (pid:%d)\n", (int) getpid()); } else { // parent goes down this path (main) printf("hello, I am parent of %d (pid:%d)\n", rc, (int) getpid()); } return 0; } The wait() System Call The wait() System Call (Cont.)  This system call won’t return until the child has run and exited. Result (Deterministic) prompt>./p2 p2.c hello world (pid:29266) hello, I am child (pid:29267) #include hello, I am parent of 29267 (wc:29267) (pid:29266) #include prompt> #include #include int main(int argc, char *argv[]){ printf("hello world (pid:%d)\n", (int) getpid()); int rc = fork(); if (rc < 0) { // fork failed; exit fprintf(stderr, "fork failed\n"); exit(1); } else if (rc == 0) { // child (new process) printf("hello, I am child (pid:%d)\n", (int) getpid()); } else { // parent goes down this path (main) int wc = wait(NULL); printf("hello, I am parent of %d (wc:%d) (pid:%d)\n", rc, wc, (int) getpid()); } return 0; } The exec() System Call The exec() System Call (Cont.)  Run a program that is different from the calling program p3.c (Cont.) … p3.c execvp(myargs, myargs); // runs word count printf("this shouldn’t print out"); #include } else { // parent goes down this path (main) #include int wc = wait(NULL); #include printf("hello, I am parent of %d (wc:%d) (pid:%d)\n", #include rc, wc, (int) getpid()); #include } return 0; int main(int argc, char *argv[]){ } printf("hello world (pid:%d)\n", (int) getpid()); int rc = fork(); if (rc < 0) { // fork failed; exit fprintf(stderr, "fork failed\n"); Result exit(1); } else if (rc == 0) { // child (new process) prompt>./p3 printf("hello, I am child (pid:%d)\n", (int) getpid()); hello world (pid:29383) char *myargs; hello, I am child (pid:29384) myargs = strdup("wc"); // program: "wc" (word count) 29 107 1030 p3.c myargs = strdup("p3.c"); // argument: file to count hello, I am parent of 29384 (wc:29384) (pid:29383) myargs = NULL; // marks end of array prompt> … All of the above with redirection All of the above with redirection (Cont.) p4.c p4.c #include … #include // now exec "wc"... #include char *myargs; #include myargs = strdup("wc"); // program: "wc" (word count) #include myargs = strdup("p4.c"); // argument: file to count #include myargs = NULL; // marks end of array execvp(myargs, myargs); // runs word count int } else { // parent goes down this path (main) main(int argc, char *argv[]){ int wc = wait(NULL); int rc = fork(); } if (rc < 0) { // fork failed; exit return 0; fprintf(stderr, "fork failed\n"); } exit(1); } else if (rc == 0) { // child: redirect standard output to a file close(STDOUT_FILENO); Result open("./p4.output", O_CREAT|O_WRONLY|O_TRUNC, S_IRWXU); prompt>./p4 … prompt> cat p4.output 32 109 846 p4.c prompt>

Use Quizgecko on...
Browser
Browser