Processes

Ps (Processes)

Processes are the programs that are running on your machine. The are managed by the kernel.

PID

ID associated with each process. Assigned in the order that the processes are created.

$ ps - shows a list of running processes.

PID: Process ID

TTY: Controlling terminal associated with the process

STAT: Process status code

TIME: Total CPU usage time

CMD: Name of executable/ command

$ ps aux

a - displays all processes running - even by other users

u - shows more details

x - lists all processes that don’t have a TTY associated with it.

shows a ? in the TTY field

common in daemon processes that launch as part of system startup.

USER: The effective user.

PID: Process ID

%CPU: CPU time used divided by the time the process has been running.

%MEM: Ratio of the process’s resident set size to the physical memory on the machine.

VSZ: Virtual memory usage of the entire process

RSS: Resident set size, the non-swapped physical memory that a task has used.

TTY: Controlling terminal associated with the process.

STAT: Process status code

START: Start time of the process

TIME: Total CPU usage time

COMMAND: Name of executable/command

$ top

shows real time process info. Refreshes every 10 seconds

Controlling terminal

TTY is the terminal that executed the command

There are two types of terminals

terminal devices

native terminal device that you can type into and send output to your system.

TTY1

enter Ctrl-Alt-F1 to enter

Ctrl-Alt-F7 to exit

pseudoterminal devices

The shell you normally use denoted by PTS under the TTY field

? under the TTY field means that the process is not linked to a controlling terminal.

Process Details

The kernel knows:

The status of the process

The resources the process is using and receives

The process owner

Signal handling

And basically everything else

It’s the kernels job to allocate resources to different processes depending on the process demands.

Process Creation

When a process is created, the process that is created gets a PPID, the PPID is the parent process’s (the process that started this process) PID

This is called the Fork system.

init: the very first process that starts when the computer turns on. It has a process ID of 1

Can’t be terminated unless the system shuts down. Also runs with root privileges.

runs many processes that keep the system running.

Process Termination

A process can exit using the _exit system call.

When a process wants to terminate, it lets the system know why it’s terminating with a termination status.

Status of 0 means that the process succeeded.

the parent process must acknowledge the termination of the child process by using the wait system call.

The wait system call checks the termination status of the child process.

Orphan processes get put under care of init.

Zombie processes

When a child process terminates and a parent hasn’t called wait yet. The zombie stays in the process until the parent wait calls it. When a parent calls the zombie it is known as “reaping” and the zombie will disappear.

if the parent doesn’t perform the wait call on the zombie then init will adopt the zombie and wait call it.

Too many zombie processes take up space in the process table. If the process table is filled up it will prevent other processes from running.

Signals

A notification to a process that something has happened.

Why we have signals.

Software interrupts that have a lot of uses.

A user can type one of the special terminal characters (Ctrl-C) or (Ctrl-Z) to kill, interrupt, or suspend processes.

Hardware issues can occur and the kernel wants to notify the process.

Software issues can occur and the kernel wants to notify the process.

They are basically ways processes can communicate.

Signal process.

Signal is generated by some event

Delivered to a process

In a pending state until it is delivered.

When the process is ran, the signal will be delivered.

Processes have signal masks and they can block signals if specified.

After a signal is delivered a process could do a multitude of things:

Ignore the signal

“Catch” the signal and perform a specific handler routine.

Process can be terminated, as opposed to the normal exit system call.

Block the signal, depending on the signal mask.

Common Signals

- Defined by integers with symbolic names that are in the form of SIGxxx.

- SIGHUP or HUP or 1: Hangup

- SIGINT or INT or 2: Interrupt

- SIGKILL or KILL or 9: kill

- SIGSEGV or SEGV or 11: Segmentation fault

- SIGTERM or TERM or 15: Software termination

- SIGSTOP or STOP: Stop

*numbers can vary with signals so they are usually referred by their names.

* some signals are unblockable, one example is the SIGKILL signal. The KILL signal destroys the process.

Kill (Terminate)

$ kill 12445 - signal to kill a process. 12445 is the PID of the process to be killed.

This send a TERM signal. The SIGTERM signal is sent to a process to request it’s termination by allowing it to cleanly release its resources and saving it’s state.

$ kill -9 12445 - specifying signal -9 (SIGKILL) to kill the process.

- SIGHUP - Hangup, sent to a process when the controlling terminal is closed.

- SIGINT - Is an interrupt signal (Ctrl-C)

- SIGTERM - Kill the process, but allow it to do some cleanup first.

- SIGKILL - Kill the process, kill it with fire, no cleanup

- SIGSTOP - Stop/ suspend a process

niceness

Process take turns sharing the CPU. A nice value can influence the process scheduling algorithm.

Processes have a number to determine the priority for a CPU. High numbers are lower in priority and low numbers or negative are higher in priority. Nice is high not nice is low.

$ top - shows processes with Niceness level under NI column.

$ nice -n 5 apt upgrade - set priority for new process.

$ renice 10 -p 3245 - set priority for an existing process.

Process states

$ ps aux

STAT column show values for different states.

- R - running or runnable, waiting for the CPU to process it.

- S - Interruptible sleep, waiting for an even to comeplete, such as input from the terminal.

-D - Uninterruptable sleep - can’t be killed or interrupted, only a reboot can make them go away.

- Z - Zombie

- T - Stopped or suspended

/proc filesystem

Processes are stored here

$ ls /proc - there are subdirectories for every PID. Shows all processes in file form. Shows more infor here than in ps

Job Control

Sending a job to the background

sending an ampersand (&) to the command will run it in the background so you can still use your shell.

Example

$ sleep 1000 &

$ sleep 1001 &

$ sleep 1002 &

View all background jobs

$ jobs - to view the jobs you just sent to the background.

This will show job id in the first column, then status , then the command that was ran. + and - next to the job is the most recent and second most recent program respectively.

Sending a job to the background on existing job

Ctrl+Z > $ bg

Moving a job from the background to the foreground

$ fg %1

Kill background jobs

kill %1