processes - github pages

25
Processes Operating Systems Kartik Gopalan References: Chapter 2 of the Tanenbaum’s book Chapter 4 of OSTEP book man pages in any UNIX/Linux system

Upload: others

Post on 16-Oct-2021

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: processes - GitHub Pages

Processes

Operating Systems Kartik Gopalan

References: • Chapter 2 of the Tanenbaum’s book • Chapter 4 of OSTEP book • man pages in any UNIX/Linux system

Page 2: processes - GitHub Pages

Process

• A process is a program in execution. • A program is a set of instructions somewhere (like the disk). • These instructions are loaded into the process’ memory “in the beginning” by

the OS.

• Von Neumann model of computing : Once created, a process continuously does the following 1. Fetches an instruction from memory. 2. Decodes it.

• i.e., figures out which instruction this is. 3. Executes it.

• i.e., it does the thing that it is supposed to do, like add two numbers together, access memory, check a condition, jump to a function, and so forth.

Page 3: processes - GitHub Pages

Process versus Program

• Program • Program is a passive entity stored in the disk • Static code and static data

• Process • Actively executing code and the associated static and dynamic

data.

• Program is just one component of a process.

• There can be multiple process instances of the same program • Example: many users can run “ls” at the same time

Page 4: processes - GitHub Pages

So what constitutes a process?

•Memory space (static, dynamic)

•Procedure call stack

•Registers and counters : • Program counter, Stack pointer, General purpose registers

•Open files, connections

• And more.

Page 5: processes - GitHub Pages

Memory Layout of a typical process

• Stack and heap grow towards each other

Program Code

Global variables, constants etc

Function Call Arguments, Return Address, Return Values

Dynamically allocated memory (e.g. malloc())

Text

Data

Heap

Gap

Stack

MAX

0

Page 6: processes - GitHub Pages

System calls to control process lifetime• fork()

• Create a process

• exec() • Run a new program • More accurately: Replace the current process with a new program

image

• wait() or waitpid() • wait for a child process to terminate

• exit() • Terminate the calling process

Page 7: processes - GitHub Pages

Process Creation

•Always using the fork() system call.

•When? • User runs a program at command line

• OS creates a process to provide a service • Check the directory /etc/init.d/ on Linux for scripts that start off different

services at boot time.

• One process starts another process • For example in servers

Page 8: processes - GitHub Pages

Example : fork() and waitpid() https://oscourse.github.io/examples/fork_ex.c

pid = fork(); if (pid < 0) { perror("fork failed:”); exit(1); }

if (pid == 0) { // Child executes this block printf(“This is the child\n"); exit(0); }

if (pid > 0) { //Parent executes this block

printf(“This is parent. The child is %d\n", pid);

ret = waitpid(pid, &status, 0); if (ret < 0) {

perror(“waitpid failed:”) exit(2);

}

printf(“Child exited with status %d\n”, status); exit(0);

}

Page 9: processes - GitHub Pages

The strange behavior of fork()•fork() is called once …

•But it returns twice!! • Once in the parent and • Once in the child

• The parent and the child are two different processes.

• Child is an exact “copy” of the parent.

•So how to make the child process do something different? • Return value of fork in child = 0 • Return value of fork in parent = [process ID of the child] • By examining fork’s return value, the parent and the child can take

different code paths.

Page 10: processes - GitHub Pages

Running a new program

• Consider how a shell executes a command

$ pwd /home/user

• How does that work? •Shell forked a child process •The child process executed /bin/pwd using the exec() system call

•Exec replaces the process’ memory with a new program image.

Page 11: processes - GitHub Pages

exec() system call

Parent ChildFork

Exec

New program image

in execution

Page 12: processes - GitHub Pages

exec() — Example code exec_ex.c https://oscourse.github.io/examples/exec_ex.c

if ((pid = fork()) < 0) { fprintf(stderr, "fork failed\n"); exit(1);

}

if (pid == 0) { if( execlp("echo",

"echo", "Hello from the child", (char *) NULL) == -1)

fprintf(stderr, "execl failed\n"); exit(2);

}

printf("parent carries on\n");

Page 13: processes - GitHub Pages

The strange behavior of exec()

• Replaces current process image with new program image. • In the last example, parents’ image was replaced by

the “echo” program image.

• All I/O descriptors open before exec stay open after exec. • I/O descriptors = file descriptors, socket descriptors,

pipe descriptors etc. • This property is very useful for implementing filters.

Page 14: processes - GitHub Pages

Different Types of exec()•int execl(char * pathname, char * arg0, … , (char *)0);

• Full pathname + long listing of arguments

•int execv(char * pathname, char * argv[]); • Full pathname + arguments in an array

•int execle(char * pathname, char * arg0, … , (char *)0, char envp[]); • Full pathname + long listing of arguments + environment variables

•int execve(char * pathname, char * argv[], char envp[]); • Full pathname + arguments in an array + environment variables

•int execlp(char * filename, char * arg0, … , (char *)0); • Short pathname + long listing of arguments

•int execvp(char * filename, char * argv[]); • Short pathname + arguments in an array

•More info: check “man 3 exec”

Page 15: processes - GitHub Pages

Terminating a process

• Return from the first function • Usually main()

• exit(status) • Exit the program. • Status is retrieved by the parent using wait(). • 0 for normal status, non-zero for error

Page 16: processes - GitHub Pages

Process Hierarchy Tree

• A created two child processes, B and C

• B created three child processes, D, E, and F

Parent of B and C

LeafChild of A

Parent of D, E, F

Page 17: processes - GitHub Pages

CPU scheduler

• Time-shares many processes on one CPU

CPUWho’s Next?

CPU SchedulerQueue of Ready Process

Page 18: processes - GitHub Pages

Process Lifecycle

• Ready • Process is ready to execute, but not yet executing • Its waiting in the scheduling queue for the CPU scheduler to pick it up.

• Running • Process is executing on the CPU

• Blocked • Process is waiting (sleeping) for some event to occur. • Once the event occurs, process will be woken up, and placed on the scheduling

queue.

Page 19: processes - GitHub Pages

How do multiple processes share CPU?

6 THE ABSTRACTION: THE PROCESS

Running Ready

Blocked

Descheduled

Scheduled

I/O: initiate I/O: done

Figure 4.2: Process: State Transitions

If we were to map these states to a graph, we would arrive at the di-agram in Figure 4.2. As you can see in the diagram, a process can bemoved between the ready and running states at the discretion of the OS.Being moved from ready to running means the process has been sched-uled; being moved from running to ready means the process has beendescheduled. Once a process has become blocked (e.g., by initiating anI/O operation), the OS will keep it as such until some event occurs (e.g.,I/O completion); at that point, the process moves to the ready state again(and potentially immediately to running again, if the OS so decides).

Let’s look at an example of how two processes might transition throughsome of these states. First, imagine two processes running, each of whichonly use the CPU (they do no I/O). In this case, a trace of the state of eachprocess might look like this (Figure 4.3).

Time Process0 Process1 Notes1 Running Ready2 Running Ready3 Running Ready4 Running Ready Process0 now done5 – Running6 – Running7 – Running8 – Running Process1 now done

Figure 4.3: Tracing Process State: CPU Only

In this next example, the first process issues an I/O after running forsome time. At that point, the process is blocked, giving the other processa chance to run. Figure 4.4 shows a trace of this scenario.

More specifically, Process0 initiates an I/O and becomes blocked wait-ing for it to complete; processes become blocked, for example, when read-

OPERATING

SYSTEMS

[VERSION 0.91] WWW.OSTEP.ORG

Page 20: processes - GitHub Pages

How do multiple processes share CPU?

THE ABSTRACTION: THE PROCESS 7

Time Process0 Process1 Notes1 Running Ready2 Running Ready3 Running Ready Process0 initiates I/O4 Blocked Running Process0 is blocked,5 Blocked Running so Process1 runs6 Blocked Running7 Ready Running I/O done8 Ready Running Process1 now done9 Running –10 Running – Process0 now done

Figure 4.4: Tracing Process State: CPU and I/O

ing from a disk or waiting for a packet from a network. The OS recog-nizes Process0 is not using the CPU and starts running Process1. WhileProcess1 is running, the I/O completes, moving Process0 back to ready.Finally, Process1 finishes, and Process0 runs and then is done.

Note that there are many decisions the OS must make, even in thissimple example. First, the system had to decide to run Process1 whileProcess0 issued an I/O; doing so improves resource utilization by keep-ing the CPU busy. Second, the system decided not to switch back toProcess0 when its I/O completed; it is not clear if this is a good deci-sion or not. What do you think? These types of decisions are made by theOS scheduler, a topic we will discuss a few chapters in the future.

4.5 Data Structures

The OS is a program, and like any program, it has some key data struc-tures that track various relevant pieces of information. To track the stateof each process, for example, the OS likely will keep some kind of processlist for all processes that are ready, as well as some additional informa-tion to track which process is currently running. The OS must also track,in some way, blocked processes; when an I/O event completes, the OSshould make sure to wake the correct process and ready it to run again.

Figure 4.5 shows what type of information an OS needs to track abouteach process in the xv6 kernel [CK+08]. Similar process structures existin “real” operating systems such as Linux, Mac OS X, or Windows; lookthem up and see how much more complex they are.

From the figure, you can see a couple of important pieces of informa-tion the OS tracks about a process. The register context will hold, for astopped process, the contents of its registers. When a process is stopped,its registers will be saved to this memory location; by restoring these reg-isters (i.e., placing their values back into the actual physical registers), theOS can resume running the process. We’ll learn more about this techniqueknown as a context switch in future chapters.

c© 2014, ARPACI-DUSSEAU

THREE

EASY

PIECES

Page 21: processes - GitHub Pages

Typical Kernel-level data structure for each process

• See task_struct in Linux source code

Page 22: processes - GitHub Pages

Examining Processes in Unix/Linux

• ps command • Standard process attributes

• /proc directory • More interesting information if you are the root.

• top command • Examining CPU and memory usage statistics.

Page 23: processes - GitHub Pages

Orphan process

• When a parent process dies, child process becomes an orphan process.

• The init process (pid = 1) becomes the parent of the orphan processes.

• Here’s an example: • https://oscourse.github.io/examples/orphan.c

• Do a ‘ps –l’ after running the above program and check parent’s PID of the orphan process.

• After you are done remember to kill the orphan process ‘kill –9 <pid>’

Page 24: processes - GitHub Pages

Zombie Process

• When a child dies, a SIGCHLD signal is sent to the parent.

• If parent doesn’t wait()on the child, and child exit()s, it becomes a zombie (status “Z” seen with ps).

• Zombies hang around till parent calls wait() or waitpid().

• But they don’t take up any system resources. • Just an integer status is kept in the OS. • All other resources are freed up.

Page 25: processes - GitHub Pages

References

• Chapter 2 of the Tanenbaum’s book • Chapter 4 of OSTEP book

• Man pages for different system calls • Try “man 2 <syscall_name>”

• E.g. man 2 exec • Syscalls are normally listed in section 2 of the man page