420-N23 Operating Systems - Topic 16 - Processes and Threads
420-N23 Operating Systems - Topic 16 - Processes and Threads
Thread
A dispatchable unit of work within a process
Processes Vs.
Threads
Processes are heavyweight, but
threads within processes are
lightweight.
https://www.d.umn.edu/~gshute/os/processes-and-threads.xhtml
Thread
A thread is the unit of execution within a
process. A process can have anywhere from
just one thread to many threads.
When a process starts, it is assigned memory
and resources. Each thread in the process
shares that memory and resources. In single-
threaded processes, the process contains one
thread. The process and the thread are one
and the same, and there is only one thing
happening.
In multithreaded processes, the process
contains more than one thread, and the
process is accomplishing a number of things at
the same time.
Threads
Threads are sometimes called lightweight processes because they have their own stack but can
access shared data. Because threads share the same address space as the process and other
threads within the process, the operational cost of communication between the threads is low,
which is an advantage. The disadvantage is that a problem with one thread in a process will
certainly affect other threads and the viability of the process itself.
A good overview about threads:
https://www.backblaze.com/blog/whats-the-diff-programs-processes-and-threads/
Stack is used for
static memory
allocation and
Heap for dynamic
memory
allocation
Race Condition
A “race condition” arises if two or more To help with these issues, there is a technique
threads access the same variables or objects called synchronization and locking of the
concurrently and at least one does updates. critical sections of a program.
Example: Suppose t1 and t2 simultaneously See:
execute the statement x = x + 1; for some
static global x https://www.cs.cornell.edu/courses/cs2110/2
◦ Internally, this involves loading x, adding 1,
014fa/L25-
storing x ConcurrencyII/cs2110fa14Concurrency2-
6up.pdf
◦ If t1 and t2 do this concurrently, we execute the
statement twice, but x may only be incremented
once
◦ t1 and t2 “race” to do the update
Race Condition Illustrated
Linux Processes &
Process Management
See Running Processes
The “ps” command shows running processes
◦ ps -e : Show all processes as a list
◦ ps -ejH : Show a process in tree format
◦ ps -eLf : Show information about threads
◦ ps -e | grep bash : See the basic info about any process called “bash”
◦ ps 19 : Show info about process 19
◦ ps -Flww 3277 : Get detailed information about process numbered 3277
The “atop” command will show an interactive listing real-time of processes (need to install it)
◦ atop
◦ atop -c
◦ While atop is running, press “y” to show child processes within a process.
◦ Pressing “a” toggles between active and all system processes.
◦ Pressing “u” toggles between user and all system processes.
“top” is a more basic version of atop, with different command sets.
Running a process in the foreground
As we’ve seen, we only need to run the process like:
./processname
It will halt the current bash shell process and run a child process called “processname”.
To make the process run in the background use 2 steps
1. Use the keyboard sequence “CTRL-Z”. Your terminal will be responsive again. Make note of
the jobname it creates.
2. Write “bg” to put the process in the background.
To bring the process back to the foreground, use the command:
fg processname
Run a process in the background
To run in the background, append a “&” character after a space, after the command.
Example:
pwd &
This runs pwd in the background, but since pwd runs so fast, it ends instantly.
$ pwd &
pgrep -a -u bwood
◦ Shows the process numbers for the user bwood.
kill 1234
◦ Stops execution of process number 1234
pkill firefox
◦ Stop process of processes named firefox
Change the Process Priority
You can change the priority a process receives.
Remember processes all work together, and some have higher priority and are given more
system time and/or resources.
Be careful with this command, as giving too much priority to things may destabilize the system
and make things run slower.
Nice:
◦ Starts a process with a specified priority.
◦ nice -n priority processname
Renice:
◦ Changes the priority of a running process.
◦ renice priority -p 'PID'
Scheduling
Crontab Table
Crontab runs programs on a schedule.
When a program is run, it’s called a “job”
Each job to be run is a line in this table with details about run times.
Run a backup every 8:30am on the 10th day of June, no matter what day.
30 08 10 06 * /home/bwood/rsync
Run a job every hour between 9am and 6pm, only working days (Mon-Fri)
00 09-18 * * 1-5 /home/ramesh/bin/check-db-status
Edit crontab for the root user (your crontab and root crontab are 2 different tables)
◦ sudo crontab -e
Special Entries
We can use alias names for common entries. For example, monthly, yearly.
@monthly /home/bwood/job1
Keyword Equivalent
@yearly 0011*
@daily 00***
@hourly 0****
Create a batch file which will write “Hello – Time” every minute to a file in your user directory.
The job called “record_date.sh” should look like this:
◦ #!/bin/bash
◦ echo `date +%r` >> /home/bwood/date_file # Write hms date to date_file and
append
The crontab entry should look like this: (note, there is no “@everyminute” equivalent)
◦ * * * * * /home/bwood/record_date.sh