Lec2 Proc

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 58

Processes

OS: A Reminder
 OS
 Manages resources
 Involves asynchronous and sometimes parallel
activities

 But how does it do this?


 Through several abstractions
 Let’s look at one such abstraction: the Process

Operating Systems 2
Users, Programs, Processes
 Users have accounts on the system
 Users write programs, then launch
programs
 Different users may launch same program
 One user may launch many instances of the
same program
 Process:
= an executing program
= a program in action
= a running program
Operating Systems 3
Analogy
 Program: steps for attending the lecture
1. walk to Siebel Center Building
2. enter 1404 Lecture Room
3. find a seat
4. listen and take notes
 Process: instance of above program in action
= attending the lecture
 Action
 Right now, you are in the middle of this process!
 You have a “condition” that changes every second!
 Different processes from the same program may behave
differently

Operating Systems 4
An OS runs many processes
simultaneously
 Listing all running processes
 Windows: Task Manager
 Unix/Linux> “ps –all”
PID TTY TIME CMD
1625 pts/6 0:02 tcsh
17246 pts/1 0:35 pine
1272 pts/7 0:00 ps
8865 pts/3 0:01 emacs

 Examples of running processes:


 Command Shell (e.g., tcsh)
 Editors: pine (also emacs, vim, etc.)
 Others: Firefox windows (each firefox window is a separate process)
 And “ps” itself appears as a process!

Operating Systems 5
Concretely! Can we define the
term “process”?
 OK, a process consists of:
1. The program code (does not change, ideally)
2. The Data
3. The Program Counter
4. The Stack
5. The Register values

And anything else that defines the process’ current


condition
Operating Systems 6
A Process, Pictorially

R1 R2 R3 R4

PC (hex): 00d0

A process in a machine with 4 registers

Operating Systems 7
Hmm… how does one “play”
around with a process?
 A process is an abstraction for sequence of
operations that implement a computation.
 There are 2 types of processes:
 OS processes executing system code
 User processes executing user code
 A user process may be given kernel privilege sometimes
(e.g., once it executes a TRAP)
 Regardless of type, a process may be manipulated,
suspended, scheduled and terminated
 By whom? By the OS, as well as by other processes!

Operating Systems 8
Process Management

1. One CPU can execute only one process at a time.


 If there are 4 CPU’s in a machine, how many processes
could be executed simultaneously by the machine?
2. The OS manages the CPU’s selection of which
process is the lucky one in (1). The OS may
change its mind as time goes along!
 That’s called scheduling – we’ll skip it!
3. OS may be told by other processes (through
“subtle” system calls) how to make decision in (2).
 Is it a good idea to allow a system call that explicitly
selects which process CPU will run next? Consider two
“colluding” processes that can hog the CPU.
Operating Systems 9
Process Creation
Processes are created and deleted dynamically (by the
OS or by other processes)
 System initialization, e.g., boot or reboot
 User request to create a new process
 By command line process (type in or double-click)
 Initiation of a batch job, e.g., through cron
 Execution of a process creation system call, e.g., fork()
system call in Unix, CreateProcess() in Windows
 OS assigns each process a unique id called pid
(process id). No two processes on same system can
have identical pid’s.

Operating Systems 10
Forks
PID: 837
int parentpid;
int childpid;

if ((childpid = fork()) == -1) { PC


perror(can’t create a new process);
exit(1);
} Suppose the OS is running this,
else if (childpid == 0) {/* child process and it executes the fork() instruction
executes */
printf(“child: childpid = %d, parentpid =
%d \n”, getpid(), getppid());
exit(0);
}
else { /*parent process executes */
printf(“parent: childpid = %d, parentpid =
%d \n”, childpid, getpid());
exit(0);
}

Operating Systems 11
Forks: create a child!
PID: 837 PID: 839 : New Process!
int parentpid; int parentpid;
int childpid; int childpid;

if ((childpid = fork()) == -1) { PC if ((childpid = fork()) == -1) { PC


perror(can’t create a new process); perror(can’t create a new process);
exit(1); exit(1);
} }
else if (childpid == 0) {/* child process else if (childpid == 0) {/* child process
executes */ executes */
printf(“child: childpid = %d, parentpid = printf(“child: childpid = %d, parentpid =
%d \n”, getpid(), getppid()); %d \n”, getpid(), getppid());
exit(0); exit(0);
} }
else { /*parent process executes */ else { /*parent process executes */
printf(“parent: childpid = %d, parentpid = printf(“parent: childpid = %d, parentpid =
%d \n”, childpid, getpid()); %d \n”, childpid, getpid());
exit(0); exit(0);
} }

Operating Systems 12
Forks: create a child!
PID: 837 PID: 839 : New Process!
int parentpid; int parentpid;
int childpid; int childpid;

if ((childpid = fork()) == -1) { if ((childpid = fork()) == -1) {


perror(can’t create a new process); perror(can’t create a new process);
exit(1); exit(1);
} }
else if (childpid == 0) {/* child process else if (childpid == 0) {/* child process PC
executes */ executes */
printf(“child: childpid = %d, parentpid
= %d \n”, getpid(), getppid());
printf(“child: childpid = %d, parentpid
= %d \n”, getpid(), getppid());
Will be executed
exit(0); exit(0);
} }
else { /*parent process executes */ PC else { /*parent process executes */
printf(“parent: childpid = %d,
parentpid = %d \n”, childpid, Will be printf(“parent: childpid = %d,
parentpid = %d \n”, childpid,
getpid()); getpid());
exit(0); executed exit(0);
} }

Processes 837 (parent) and 839 (child) are clones! Except…


•They each have their own and separate code, pc, registers, stack
•They have the same data except that
•childpid==839 in parent (837). So parent knows child’s pid.
•childpid==0 in child (839)
•They are free to operate on their own from now onwards
Operating Systems 13
Forking
int main()
{
pid_t pid;
/* fork another process */
pid = fork();
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
exit(-1);
}
else if (pid == 0) { /* child process */
execlp("/bin/ls", "ls", NULL);
}
else { /* parent process */
/* parent will wait for the child to complete */
wait (NULL);
printf ("Child Complete");
exit(0);
}
}
Operating Systems 14
Process Hierarchies

PID 837

PID 839

 Created via process creation operations (incl.


fork())
 Tree is called “Process Group” in Unix
Operating Systems 15
Process Termination
1. Normal exit (voluntary): end of main()
2. Error exit (voluntary): exit(2)
3. Fatal error (involuntary): divide by 0, core dump
4. Killed by another process (involuntary): kill procID,
end task

 1-2 above achieved through system call exit()


 3 above achieved process itself executing a TRAP
 4 achieved through“signal” received by some other
process
 Signal=software interrupt
 Kill uses signal type SIGKILL

Operating Systems 16
Process Creationism
 When creating a process, it needs resources such as CPU,
memory files, I/O devices
 Process can get resources from the OS or from the parent
process (e.g., CPU, disk)
 When getting resources from a parent, the child process is
restricted to a subset of parent resources
 Prevents many processes from overloading system
 When creating a new process, execution possibilities are
 Parent waits until child has terminated. $emacs
 Parent continues concurrently with child. $ emacs&
 When creating a new process, address space possibilities:
 Child process is duplicate of parent process
 Child process has a new, different program loaded into it, e.g.,
execve() system call

Operating Systems 17
Vs. The Terminator
 When a process finishes last statement, it asks OS
to delete it
 Child process may return output to parent process,
and then all child’s resources are de-allocated.
 Other termination possibilities: Abort invoked by
parent process
 Child has exceeded its usage of some resources
 Task assigned to child is no longer required
 Parent is exiting, and OS does not allow child to continue
without parent
 Windows also kills parent when child is killed (not in Unix/Linux)

Operating Systems 18
Process: A Manipulatable
Object

 OS manipulates a process as if it is an
object with a condition

 The operations that OS performs on the


process change the condition of the
process.
 An important component of a process’
condition: its state
Operating Systems 19
Process State

 Possible process states


 Running (occupy CPU)
 Blocked
 Ready (does not occupy CPU)
 Other states: suspended, terminated
 Notice the transitions between states
 Question: in a single processor machine, how many
processes can be in running state?

Operating Systems 20
Process State
 As a process executes, it changes state
 new: The process is being created
 running: Instructions are being executed
 blocked: The process is waiting for some event to occur
 ready: The process is waiting to be assigned to a processor
 terminated: The process has finished execution

Operating Systems 21
Diagram of Process State

Operating Systems 22
The Process Model
(as seen in memory)
(as seen by CPU)

(as seen by OS)

 4 independent, sequential processes in a 1 CPU machine


 Only one program active at any instant
 Real life analogy?
 You doing homeworks for 4 different courses

Operating Systems 23
A process’ condition consists of
 Process State
 new, ready, running, waiting, halted;
 Program Counter
 the address of the next instruction to be executed for this process;
 CPU Registers
 index registers, stack pointers, general purpose registers;
 CPU Scheduling Information
 process priority and pointer;
 Memory Management Information All present in
 base/limit information; OS-maintained data
 Accounting Information structure called
PCB=process control
 time limits, process number; owner
block,
 I/O Status Information for each process that
 list of I/O devices allocated to the process;
has not been terminated

Operating Systems 24
Process Control Block (PCB)

Fields of a process table entry


e.g.: Linux: struct task_struct Operating Systems 25
Remember This?

Application

Libraries User space/level

Kernel space/level
Portable OS Layer

Machine-dependent layer

Operating Systems 26
Main
Memory
Main Memory/ 1GB
Set aside
for OS/kernel
 One (common) use only
approach
 Kernel is high memory
 User is low memory

PID 901
 What restrictions (code,data,
apply? stack)
PID 902
(code,data,
stack)
 read(f, buf, nbytes)
0
Operating Systems 27
Process Address Space
 Program segments
PID 901 0xffff….
 Text=static (code,data,
stack) Kernel space
 Data=static data
 Heap=dynamic data Stack
 Stack=dynamic
 Lots of flexibility
 Allows stack growth
 Allows heap growth Heap
Code & Data
 No predetermined division
0x0000…
Why is this needed?
Operating Systems 28
Remember system call libraries?
Process Scheduling
 Objective of multiprogramming – maximal
CPU utilization, i.e., have always a process
running
 Objective of time-sharing – switch CPU
among processes frequently enough so that
different users can use the single CPU as if
they had their own
 Need Context Switching

Operating Systems 29
Context Switch
 Switch CPU from one process to another
 Context of a process represented in the PCB
 Performed by scheduler
 It does the following:
1. saves PCB of the old process;
2. loads PCB of the new process;
3. flushes memory cache;
4. change memory mapping (TLB)
 Context switch is expensive(1-1000 microseconds)
 No useful work is done (pure overhead)
 Can become a bottleneck
 Real life analogy?
 Your switching back and forth between HW’s for different courses
 Needs hardware support

Operating Systems 30
Process Context Switch
OS jobs PCB-1
PCB-0

Interrupt/system call
exec

Save PCB-0
idle

Reload from PCB-1


idle Interrupt/
System call exec

Save into PCB-1

idle

Reload from PCB-0

exec
Operating Systems 31
“Process”? That’s it!
 It’s one executing instance of a “program”
 It’s separate from other instances
 It can started/manipulated/killed by other
processes
 It can be started/manipulated/killed by by
them

 Also covered
 Process model, fork, PCB, context switch
Operating Systems 32
Threads for this lecture
 What is a thread?
 Examples of using threads

 Thread implementations

Operating Systems 33
Hmm, what was that
“Process” again?
 It’s one executing instance of a “program”
 Consists of code, data, heap, stack, PC, regs, and a
little other information in the PCB
 It can start/manipulate/kill other processes
 What’s in a process?
 Process control block
 Pointers to Code (text), data, stack, heap
 Process state, priority, accounting
 Program counter, register variables, stack pointers, etc
 Open files and devices

Operating Systems 34
Threads
 Processes do not share resources very much (or not at
all), therefore context switching cost is very high.
 Inefficient!
 Concept of Thread:
 A process can consist of many “threads”
 Thread is a light-weight process
 A Thread comprises
 Thread ID
 Program counter
 Register set
 Stack space
 Threads within same process share
 Code section
 Data section
 OS resources such as open files, signals belonging to the task

Operating Systems 35
Threads: Lightweight
Processes

execution
Environment (resource)

(a) Three processes, each with one thread


(b) One process with three threads
Operating Systems 36
Thread Model

 Threads in the same process share resources


 Each thread executes separately

Operating Systems 37
Threads have their own stacks

Operating Systems 38
Thread Model: Context Switch
 Context Switching between threads within same
process inexpensive
 Don’t need to read in/out PB parts on code, data, file,
signals, etc.
 Thread context switch still requires
 Register set switch, PC change, stack change, state
 But no memory management related work !
 Efficient (partly at least)!

 Context Switching between threads in different


processes still expensive
Operating Systems 39
Thread Model: State
 Just like processes, a thread could have a state
that is either
 Ready or Blocked or Running or Terminated
 Threads share CPU and on single processor
machine only one thread can run at a time
 A thread can create a child thread which in turn
blocks waiting for a system call to be completed
(parent thread does not wait)
 Creating/manipulating/terminating threads also cheaper
than same ops. on processes
 No protection among threads within same process!

Operating Systems 40
Use pthread library
to create and kill
threads within a process
A example program
#include ``csapp.h''
void *thread(void *vargp);

int main() {
phtread_t tid; // stores the new thread ID

1 pthread_create(&tid, NULL, threadrun, NULL); //create one new thread exec’ing //


threadrun()

3 pthread_join(tid, NULL); //main thread waits for the other thread to terminate
exit(0); /* main thread exits */
}

void *threadrun(void *vargp) /*thread routing*/


2
{
printf(``Hello, world! \n'');
return NULL;
}
Operating Systems 41
Thread Usage: word processor

 Useful because a word processor has different tasks


that are executed in a logically parallel manner
Operating Systems 42
Thread Usage: Web Server

 A web server needs to take care of each client in


separately
Operating Systems 43
Web Server

 Rough outline of code for previous slide


(a) Dispatcher thread
(b) Worker thread

Operating Systems 44
Tradeoffs
Three ways to construct a server

Operating Systems 45
Blocking Vs. non-blocking System
Calls
 Blocking system call
 Usually I/O related: read(), fread(), getc(), write()
 Doesn’t return until the call completes
 The process/thread is switched to blocked state
 When the I/O completes, the process/thread becomes ready
 Simple
 Real life example: attending a lecture
 Using non-blocking system call for I/O
 Asynchronous I/O
 Complicated
 The call returns once the I/O is initiated, and the caller can
immediately continue
 Once the I/O completes, an interrupt is delivered to the OS,
which informs the calling process/thread
 Real life example: applying for job

Operating Systems 46
Benefits of Threads
 Responsiveness
 Multi-threading allows applications to run even if
part of it is blocked
 Resource sharing
 Sharing of memory, files and other resources of
the process to which the threads belong
 Economy
 Much more costly and time consuming to create
and manage processes than threads
 Utilization of multiprocessor architectures
 Each thread can run in parallel on a different
processor
Operating Systems 47
Implementing Threads in User Space

Thread library used


•Thread table maintained
in user space
•All thread management
In user space by library
•Kernel knows nothing
about threads

E.g.: pthread library

User-level Threads
Operating Systems 48
User-level Threads
 Advantages
 Fast Context Switching:
 User level threads are implemented using user level thread
libraries, rather than system calls, hence no call to OS and no
interrupts to kernel
 When a thread is finished running for the moment, it can call
thread_yield. This instruction (a) saves the thread information in
the thread table itself, and (b) calls the thread scheduler to pick
another thread to run.
 The procedure that saves the local thread state and the scheduler
are local procedures, hence no trap to kernel, no context switch,
no memory switch, and this makes the thread scheduling very fast.
 Customized Scheduling possible by user

Operating Systems 49
User level Threads
 Disadvantages
 Blocking
 A user level thread executing a blocking system call
can block the entire process
 No Protection
 There is no protection between threads, since the
threads share memory space

Operating Systems 50
Implementing Threads in the Kernel

OS knows about
individual threads within
each process
•Thread table maintained
by kernel

•E.g.: Windows 2K/XP

Kernel-level Threads
Operating Systems 51
Hybrid Implementations (e.g.,
Solaris)

Multiplexing user-level threads onto kernel- level threads

Operating Systems 52
Pop-Up Threads (e.g, servers)

 Creation of a new thread when message arrives


(a) before message arrives
(b) after message arrives

Operating Systems 53
Multi-threading Models
 Many-to-One Model – many user threads are mapped to one kernel
thread
 Advantage:
 Thread management is done in user space, so it is efficient
 Disadvantage:
 Entire process will block if a thread makes a blocking call to the kernel
 Because only one thread can access kernel at a time, no parallelism on
multiprocessors is possible
 One-to-One Model – each user thread maps to a separate kernel thread
 Advantage:
 More concurrency than in many-to-one model
 Multiple threads can run in parallel on multi-processors
 Disadvantage:
 Creating a user thread requires creating the corresponding kernel thread. There
is an overhead related with creating kernel thread which can be burden on the
performance.

Operating Systems 54
Multi-threading Models
 Many-to-Many Model – many user
threads are multiplexed onto a smaller or
equal set of kernel threads.
 Advantage:
 Application can create as many user threads as
needed
 Kernel threads run in parallel on multiprocessors
 When a thread blocks, another thread can still run

Operating Systems 55
Making Single-Threaded Code Multithreaded:
Pitfalls

Conflicts between threads over the use of a global variable


Operating Systems 56
A solution: Private Global Variables

Read more about this


in Section 2.2.8

•But clumsy: need libraries to create a separate global space for each thread

Operating Systems 57
Summary
 A process may comprise multiple threads
 Threads in a process share a lot, so easier to
context switch
 User level versus kernel level threads
 Former is more efficient to manage, but
problematic if one thread blocks
 Reading: Section 2.1 - 2.2
 Next: Section 2.3

Operating Systems 58

You might also like