Transcript process

CS 468: Advanced UNIX
Class 4
Dr. Jesús Borrego
Regis University
1
scis.regis.edu ● [email protected]
Topics
•
•
•
•
•
2
Controlling Processes
Periodic Processes
Process Management
Q&A
Midterm
Process
• A running program
• Used to control resource allocation by the OS
• When a process is created, a table is made in a
Process Table
• Process consists of address space and memory
pages allocated to the process
• Address space has code, variables, stacks, virtual
memory pointers, and other data
4
Processes
A process can be defined as:
A process can be defined as:
a program in execution
an instance of a running program
the entity that can be assigned to, and executed on, a processor
a unit of activity characterized by a single sequential thread of
execution, a current state, and an associated set of system resources
Source: Operating Systems: Internals and Design Principles (7th. Ed.) by William Stallings
5
Process data structures
•
•
•
•
•
•
•
6
Address space map for process
Process state
Execution priority
Resources allocated and in use
File descriptors and network ports used
Process signal mask
Process owner
Processes
7
Threads
• Created by a fork in a process
• Inherits from parent
• Can have multiple threads executing
concurrently
• Multicore and multiprocessor systems benefit
multithreading
8
Attributes
•
•
•
•
PID – unique process ID
PPID – parent PID
UID – user ID; who created the process
EUID – effective UID – reflects resources the
process can have; same as UID in most cases
GID – group ID
• EGID – effective GID
• Niceness – how the process interacts with others
• Control terminal – linkages to standard I/O
9
Process lifecycle
• Created with fork
• Process execution begins with exec
• Process terminates with a call to exit
▫ Provides return code (0 is success)
▫ Parent receives return code to acknowledge child’s
death
• If parent dies first, child is “adopted” by init
10
Signals
•
•
•
•
Interrupt request for processes
Can be used to communicate among processes
Can kill, interrupt, or suspend processes
Can be originated by kernel when unhandled
exceptions occur
• When received, a handler is called (if handler is
defined); otherwise, kernel takes action
11
Kill signal
• Used to terminate a process
• Can be used by normal users to kill their own
processes
• Root access is required for other processes
• Kill receives a signal, a numeric value (see
previous slide)
• In some systems, killall terminates init –
system shutdown
13
Monitoring
processes
• To monitor processes: ps
• Shows PID, UID,
priority, control terminal
• Can include memory
used, CPU utilization,
status
• See example of ps aux
on page 131
16
Dynamic Monitoring
• ps captures a snapshot of the system
▫ At that particular time
• top – top processes using the system every 10
seconds (default)
• topas – AIX version
• prstat – Solaris version
20
/proc File System
• The /proc directory contains information
about the system state
• The kernel creates the directory
• Information is organized by subdirectories
named by the PID
▫ /proc 1 is the init process directory
• Predefined directories are shown in Table 5.7
(next slide)
21
Tracing signals and system calls
• Linux provides visibility into a process with
strace
▫ Shows every system call and every signal received
• Similar: truss (AIX, Solaris), and tusc (HPUX)
• Example of strace is shown on page 137
24
Scheduling Commands
• To schedule scheduled processes: cron daemon
• Starts when system boots up and terminates
when shutdown
• Receives configuration files (crontab) with
commands and times to execute
▫ User crontabs located /var/spool/cron
▫ One for root, up to one per user
• Executes lines with sh
26
crontab File Format
• #comments
• Six field line:
▫ Minute, hour, day of month, month, weekday
(separated by white space)
▫ Command (passed to shell in entirety)
• % represent new lines
▫ Command receives characters up to first %
27
Time Formats
•
•
•
•
* - wild card
Single integers – matches exactly
Two integers separated by dash – range
Ranges followed by slash and step:
▫ 1-10/2  1 to 10, in 2 increments
• Comma separated list of values
35 10 * * 1-3  10:35 am Monday to Wednesday
29
crontab Management
• To create or replace your user’s crontab:
crontab filename
• To check out crontab and edit:
crontab -e
• To list contents of crontab:
crontab -l
• To remove crontab:
crontab –r
• Roots can add username to above
31
Applications of cron
• Preinstalled:
▫ Daily scripts in /etc/cron.daily
▫ Weekly scripts in /etc/cron.weekly
• Reminders: periodic tasks
• Filesystem cleanup
▫ Removing obsolete files
• Network file synchronization
• Log file cleanup
32
Process Management
• Kernel controls process access to RAM and CPU:
▫ Scheduler – allocates CPU time to a processes,
based on scheduling algorithm
▫ Memory Manager – allocates memory space to
each process and protects it from external source
access
33
CPU
Utilization
34
Scheduling
of processes
Source: Operating Systems: Internals and Design Principles (7th. Ed.) by William Stallings
35
Executable file layout
Magic Number and Main Header
Section One Header
Section Two Header
Section One
Section Two
36
First Processes
• PID 0 (sched) – created at boot
• PID 0 executes fork and exec twice to create
processes 1 (init) and 2 (pageout)
Process 1
fork/exec
process 48
fork/exec
process 12
fork/exec
process 34
37
Process States
Suspended
Signal
Initialize
Idle
Runnable
Signal
Allocated
CPU
Event
Occurs
Running
Wait on
Event
Sleeping
38
Exit
Zombied
Process Composition
•
•
•
•
•
39
Code Area – executable portion of the process
Data Area – static data used by process
Stack Area – Temporary data
User Area – Housekeeping process information
Page Tables – Memory management system
User Area
• One per process
• Accessible by the kernel
• Fields included:
▫ Signal handling record
▫ File descriptor record
▫ CPU access record
40
Process Table
• One entry for each process
• Created in kernel’s data region
• Contain:
▫
▫
▫
▫
▫
41
PID and PPID
UID and GID
Process state
Location of code, data, stack, user area
Pending signal set
Midterm
•
•
•
•
42
2 hour
Take home
Due by Friday midnight
Email in a Word document to
[email protected]
Questions?
43