What is a process? - Fairleigh Dickinson University

Download Report

Transcript What is a process? - Fairleigh Dickinson University

What is a process?
• A program contains code within one or
more files that are interconnected (linked)
for execution. A program when submitted
by a user contains documentation, etc. that
is omitted when translated for execution.
• A job is a program, together with data,
specification of files and devices that are
needed, and possibly job control language.
– Some of the literature calls a process a job or a task
Chapter 3 Processes
1
What is a process?
• A process is an abstraction of a program (unit) in
execution; the unit of a program that is assigned
the processor (sometimes called a task). In order for a
program (or part of a program) to become a process, it
must be assigned a Process IDentifier, a Process Control
Block, plus various other resources.
– An executing program can consist of multiple processes.
– Each process is assigned an ID (PID) so that (among other things)
the parent or system can kill it and reclaim resources.
• A process’s Program Counter is the address of the next
instruction to be executed.
• OS, application, system programs also become processes
• Lightweight processes are called threads.
– A thread is sometimes called the unit of dispatching
Chapter 3 Processes
2
(simplified) Process Control Block
•
•
•
•
•
•
•
•
Identifier
State
Priority
Program Counter
Pointers to memory, file, children
Context (register) data
I/O status information
Accounting information
Chapter 3 Processes
3
Part of Linux PCB
struct task_struct{
long state; /*0 for dispatchable (long data type
chosen for alignment purposes*) /
pid_t pid;
long priority;
unsigned long signal;
unsigned long blocked;
unsigned long flags;
Chapter 3 Processes
4
Linux PCB (continued)
int errno; long debugreg[8];
struct task_struct *next task, *prev_task;/*doubly linked*/
struct task_struct *next run, *prev_run; /*lists*/
unsigned long saved_kernel_stack;
unsigned long kernel_stack_page;
struct files_struct *files;
/* ptr to open files*/
struct mm_struct *mm;
/* memory ptr*/
/* more fields, such as parent, owner ids */
}
Chapter 3 Processes
5
Process Address Space
• Block of logical addresses that are assigned
to the process
– Contiguous
– Beginning at 0
• Process Address Space contains
–
–
–
–
Process Control Block
Process code (executable)
Process data
Stack and probably heap for data manipulation
Chapter 3 Processes
6
(part of) a process address space
storage block
PCB
stack
heap
static data
code
Chapter 3 Processes
7
Making a program into a process
• A program must be assigned resources to be in “executable” form.
– In virtual memory systems, the program is set up into pages and/or
segments, ready for dynamic loading
– In real memory systems, program code is loaded into memory
• In modern systems, code is separated from data to be read only
• Storage is also allocated for static process data, process stack and
heap, file buffers, and other data structures
– Process ID is assigned and placed in various system tables
– Other resources and structures are allocated, such as a PCB
– Program Counter (address of next instruction to be executed)
placed in PCB
• Threads are also processes (with their own PCs and stacks) but they
share files and data with parent.
Chapter 3 Processes
8
Threads
• Lightweight processes that share memory and files
• Single thread of control; each executes sequentially
• Parent process with multiple threads can perform multiple
duties concurrently (may execute interleaved on
uniprocessor or in parallel on multiprocessor)
• Motivation – reduce overhead of context switching and
process creation!
• Motivation – support multiprocessing
– But so does multi-tasking, but working with shared data supports
different multiprocessing applications
• Concurrency issues are highly relevant; more critical with
threads than C child processes, since they share memory
– In C, if parent or child process tries to write to data, child gets its
own copy
Chapter 3 Processes
9
Question
• Which resource must a program be assigned
before it can become a process?
a. CPU
b. Process ID
c. Devices
d. Clock
b. pid
Chapter 3 Processes
10
Process States
begin
Terminate
Ready
Running
Blocked
•
Also resident and non-resident states (VMS), zombie (UNIX)
Chapter 3 Processes
11
Finite State Table
for process states
State
Event causingTransition State
READY
RUN
RUN
BLOCKED
placed on CPU by dispatcher
READY/
BLOCKED
system overload -medium-term
RUN
READY
BLOCKED
READY
time slice interrupt, preemption
program issues system call
system call completed
NONRESIDENT
scheduler decides to swap process out of memory
NONRESIDENT medium-term scheduler  back into memory READY, BL’D
Chapter 3 Processes
12
Question
• Which of the following events will cause a
process to move from the run state to the
ready state?
a. i/o request
b. i/o completion
c. Time-slice interrupt
– c. Time-slice interrupt
Chapter 3 Processes
13
Question
• What event might take a process from run
to blocked
a. i/o request
b. i/o completion
c. Time-slice interrupt
– a. i/o request
Chapter 3 Processes
14
Question
• What event might take a process from
blocked to run?
a. i/o request
b. i/o completion
c. Time-slice interrupt
d. None of the above
» d. None of the above; only processes in ready state are
eligible for dispatching
Chapter 3 Processes
15
Operating Systems Processes
• During boot time, several OS processes are
created
– Foreground processes
• Interact with users
– Background processes
• Mail servers, web server, print server
• Daemons
– Check list of executing processes with task manager in
Windows (show processes from all users) and in
POSIX compliant systems with (ps –ef)
Chapter 3 Processes
16
Some scheduling goals
–
–
–
–
Maximize CPU utilization (ex: early batch systems)
Minimize average user wait time (ex: time-sharing)
Maximize throughput
Fairness – processes should be executed in order of
arrival
– Priority – ready process with higher priority (external,
internal) should be chosen for execution first
– Bounded waits – no process should be indefinitely
denied the CPU
• The above generally conflict with each other
Chapter 3 Processes
17
Some Scheduling Lists
• Ready list of PCBs is generally a prioritized queue
– Chosen by short-term scheduler; switched by dispatcher
– Time slices are typically about .1 second
• Dispatcher must be fast; if it takes .01 seconds, 9% of time is
used for dispatching overhead
• Blocked list is not a queue
– Process is removed from list when event completes
• Input spooling queue is frequently prioritized
– Process chosen by long-term (job) scheduler (if it exists)
• Job scheduler runs less frequently – may carefully choose which job
to run based on “good” process mix (CPU vs I/O use)
– Most systems today are interrupt-driven
• Non-resident list of jobs that were swapped out - medium-term
scheduler schedules them back into memory
Chapter 3 Processes
18
Questions
• What are some data structures that are
needed for process control?
– Process control block
– Dispatch list
– Blocked list
• What is a difference between a dispatch list
and a blocked list?
– The dispatch list is a queue of some type.
Chapter 3 Processes
19
Context Switching
• When a process is removed from the CPU (changed from
run state to blocked or ready state)
–
–
–
–
–
Its register values must be interchanged
Caches must be overwritten
Process state is updated in the PCB
Lists containing it (i.e., dispatch list) are updated
Virtual memory systems have additional overhead
• Generally takes from 1 micro to 1 millisecond
• Hardware support for context switches is being researched
– Multiple register sets in CPU
– Single hardware instruction for multiple context switching actions
• Threads minimize context switching overhead
– Share data sections (minimizes cache flushing, etc.)
Chapter 3 Processes
20
Process Creation
• Process creation (spawning)
– UNIX fork () [a system call]
• Returns child PID to parent;
– Parent can wait for child to complete with wait (pid)
– Gets its own pid with getpid() command
• Returns 0 (success) to child
– child finds its pid with getpid() command
– Gets pid of its parent with getppid() command
• Copy of parent’s address space is given to child
• Both continue execution of code following fork() in
nondeterministic order unless constrained
– Windows API CreateProcess ()
Chapter 3 Processes
21
Sample fork () operation
#include <stdio.h>
/*modified from Silberschatz”*/
int main (int argc, char *argv[])
{ int pid;
pid = fork ();
if (pid < 0)
/* fork failed*/
exit (-1);
else if (pid == 0)
/* child process; */
execlp (“/bin/ls”, “ls”, NULL); /*replaces parent code*/
else if (pid > 0) {
/* parent process*/
wait (pid);
/* parent waits for child’s completion */
printf (“child has completed execution\n”); }
printf (“%d\n”, pid);
/* printed by parent only */
exit (0);
}
Chapter 3 Processes
22
Question
• What will the following program print?
#include <stdio.h>
int main (int argc, char *argv[])
{ int pid;
pid = fork ();
if (pid < 0) exit (-1);
/* fork failed*/
else if (pid == 0)
/* child process; */
printf (“in child \n”);
else if (pid > 0) {
/* parent process*/
wait (pid); /* parent waits on child’s pid; synchronization */
printf (“child has completed execution\n”);
}
printf (“%d\n”, pid);
/*printed by parent and child */
exit (0);
}
Chapter 3 Processes
23
Process termination
• Process terminates either voluntarily (exit (n) or
ExitProcess ) or abnormally (killed or fatal error)
– OS collects terminated process’s allocated resources
• What happens to a parent if its child terminates?
– Child sends signal to parent of termination; parent
continues normally
• What happens to a child if its parent terminates?
– Possibly cascading termination – all children are killed
– Or child (orphan) is assigned a substitute parent
– Or child is sent a signal (sighup)
Chapter 3 Processes
24
Process Status Word (PSW)
• PSW contains status information, such as
condition codes, not visible to users
– virtual memory flags (process is running on real
or virtual machine)
– parity flag, trap flag (for traces during
debugging), interrupt enable flag, overflow
flag, sign flag (for synchronization)
Chapter 3 Processes
25
Concurrency and Parallelism
• Multiprogramming and multiprocessing
systems handle multiple processes that are
in a state of execution and sharing memory
– In multiprocessing, multiple processes may be
executing at the same time.
– If a process requests i/o and is suspended; another
process is dispatched
• Assumptions cannot be made about timing,
order of access to shared data, resources
Chapter 3 Processes
26
Competing processes
• Independent processes can execute in any order
• They only need to synchronize when they compete
for shared resources
– Competition synchronization (the mutual exclusion
problem)
– Code to shared resources is called a “critical region”
– If different process code in critical region for the same
resource are interleaved in execution, a “race condition”
occurs
Chapter 3 Processes
27
Cooperating processes
• Processes synchronize to share information, control timing
– For example, producer and consumer processes that
• communicate when there are items to process
• communicate when there is room in the buffer to place new items for
processing (if the buffer is bounded)
• Synchronization through shared memory or through IPC
(InterProcess Communication) facility
• Use of reference parameters
– Efficient to avoid copying large amounts of data
• (section objects in Windows; handle)
• Additional synchronization needed to prevent race
conditions
Chapter 3 Processes
28
Security introduction
• Protection and Security
– Protection is provided to prevent inadvertent
corruption
•
•
•
•
•
•
Supervisor/user mode
Bounds registers on memory
Access rights
Authentication (passwords, etc.)
Backups
Audits and logs
Chapter 3 Processes
29
Security
• It is very important to guard against
advertent attacks
– Intruders/ attackers/ black hats
– Attacker typically seeks to gain root privileges
• Many security violations are due to the
misuse of granted authority by insiders
Chapter 3 Processes
30
Some security violations
• Eavesdropping (violates confidentiality)
– Chief prevention mechanism is encryption
• Unauthorized access
– Prevention mechanisms include passwords and access
control
• Insertion, change data – violate integrity
– Prevention mechanisms include ICV, checksums
• Malware (malicious software)
– Ex: Parasitic (viruses) or self-contained (worms)
– Use Internet security suites (detect viruses, spywear)
Chapter 3 Processes
31
Other security mechanisms
• Intrusion Detection systems
– Sensors
– Analyzers
• Monitor and signal “events”
• A.I. for anomaly detection
• Intrusion Prevention systems
– Take proactive measures
Chapter 3 Processes
32
Authentication measures
• Proof by knowledge
– Passwords, pins
• Proof by possession
– Keys, smart cards
• Proof by property
– Biometrics
• Proof by location
• Use the above dynamically, possibly in hybrid form,
(perhaps using timestamps)
• Strong authentication (encrypted)
Chapter 3 Processes
33
Access control
• Control user/ program access to system or
any part of system
– Type of access permitted
• Read, write, execute (rwe)
• Windows objects have security descriptors
– System manages access control lists and entities
• Auditing and accounting are important to
determine what users have accessed
– Intruder may alter auditing software
Chapter 3 Processes
34
Firewalls
• Policies and rules are established for
incoming/ outgoing traffic
– Typically default rule is BLOCK
• Firewalls operate on several layers
– May be called proxy servers on application
layer
– Proxy server is specific for an application
• Next generation firewalls are more powerful
Chapter 3 Processes
35