0% found this document useful (0 votes)
13 views27 pages

420-N23 Operating Systems - Topic 16 - Processes and Threads

The document provides an overview of processes and threads in operating systems, highlighting the differences between them, such as processes being heavyweight and threads being lightweight. It explains the lifecycle of processes, the concept of threads within processes, and issues like race conditions. Additionally, it covers process management commands in Linux, including how to run processes in the foreground or background, change process priority, and schedule jobs using crontab.

Uploaded by

Chris Peterson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views27 pages

420-N23 Operating Systems - Topic 16 - Processes and Threads

The document provides an overview of processes and threads in operating systems, highlighting the differences between them, such as processes being heavyweight and threads being lightweight. It explains the lifecycle of processes, the concept of threads within processes, and issues like race conditions. Additionally, it covers process management commands in Linux, including how to run processes in the foreground or background, change process priority, and schedule jobs using crontab.

Uploaded by

Chris Peterson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

420-N23-LA

Operating Systems &


Scripting Using Linux
PROCESSES AND THREADS

©2020 Brendan Wood


What are Processes and Threads?
Process
An instance of a program running on a computer.

Thread
A dispatchable unit of work within a process
Processes Vs.
Threads
Processes are heavyweight, but
threads within processes are
lightweight.

Threads use the memory space of the


parent process they belong to.

Processes cannot see other process


data, they are independent. Threads
can see other thread data within the
same process.

See the comparison for more details.


Process
A process is a name given to a program instance that has been
loaded into memory and managed by the operating system
Every process has its own:
◦ Process number
◦ Process state (counters and other low-level values)
◦ Code
◦ Data
◦ List of open files
◦ Heap and Stack (another form of data storage used by programs)
Process Flow (States)
As a process executes, it changes state from it’s inception to it’s end. Here is a list of the common
states most processes go through.
New
◦ Process is being created
Running
◦ The process instructions (i.e. the code) are being executed by the processor
Waiting/Blocked
◦ Waiting for some event to occur. Maybe waiting on another process.
Ready
◦ Process is waiting to use the processor.
Terminated
◦ Process finished its job. (It executed).
Process Lifecycle

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 &

[1] 1634 Process #


/home/bwood Output of running Process
[1]+ Done pwd
Process ended
Process Control
pgrep -a bash
◦ Shows the process numbers for the “bash” process.
◦ The -a shows the name also

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.

# Example of job definition:


# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# 00 * * * * command.sh
Crontab Fields and Ranges
Field Description Allowed Value
MIN Minute field 0 to 59
HOUR Hour field 0 to 23
DOM Day of Month 1-31
MON Month field 1-12
DOW Day Of Week 0-6, Mon Tue Wed …
CMD Command Any command to be executed.
Crontab Entries
Important, use ABSOLUTE paths to jobnames always.
Putting a “#” in front of the job, disables it (makes it a comment).

Run mycronjob.sh, every Thursday at 3 PM


00 15 * * Thu /usr/local/bin/mycronjob.sh

Run a backup every 8:30am on the 10th day of June, no matter what day.
30 08 10 06 * /home/bwood/rsync

Run something every day, at 11am and 4pm.


00 11,16 * * * /home/bwood/bin/incremental-backup
Crontab Entries
Run a job every hour between 9am and 6pm
00 09-18 * * * /home/ramesh/bin/check-db-status

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

Run a job every minute


* * * * * jobname
View and edit the Cron Table
List jobs you have defined.
◦ crontab -l

List jobs root has defined.


◦ sudo crontab -l

Edit the crontab


◦ crontab -e

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****

@reboot Run at startup.


An example
Important, make sure you clean up once you create this example, not to waste disk space.

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

Change the mode of the file to be executable, at least by “u”


Example 1 - Continued
The output will look something like
◦ 02:06:19 PM
◦ 02:07:19 PM

The crontab entry should look like this: (note, there is no “@everyminute” equivalent)
◦ * * * * * /home/bwood/record_date.sh

Make sure it worked (after 2 or 3 minutes): cat /home/bwood/date_file


Don’t forget to go back into the crontab, and DELETE the entry.
End
PROCESSES
AND THREADS

You might also like