Unix / Linux - Processes Management

Hello, aspiring programmers! Today, we're going to embark on an exciting journey into the world of Unix/Linux processes. As your friendly neighborhood computer science teacher, I'm thrilled to guide you through this fundamental concept. Don't worry if you're new to programming – we'll start from the basics and work our way up. So, grab a cup of coffee (or tea, if that's your thing), and let's dive in!

Unix / Linux - Processes

What is a Process?

Before we start, let's understand what a process actually is. Imagine you're in a bustling kitchen. Each cook working on a specific dish can be thought of as a process. They have their own ingredients (resources), recipe (program), and goal (output). In the computer world, a process is simply a program in execution.

Starting a Process

Starting a process in Unix/Linux is as easy as pie! You can do it in two main ways:

  1. Foreground Process: Type the command and hit Enter.
  2. Background Process: Add an ampersand (&) at the end of the command.

Let's look at some examples:

# Foreground process
$ ls -l

# Background process
$ long_running_command &

In the first example, ls -l runs in the foreground, showing you the directory contents. In the second, the command runs in the background, allowing you to continue using the terminal.

Listing Running Processes

Now, what if you want to see all the cooks (processes) working in your kitchen (system)? That's where the ps command comes in handy!

$ ps aux

This command will show you all running processes. It's like peeking into every corner of your kitchen!

Here's a breakdown of what you'll see:

Column Description
USER Owner of the process
PID Process ID
%CPU CPU usage
%MEM Memory usage
VSZ Virtual memory size
RSS Resident Set Size
TTY Terminal type
STAT Process state
START Start time
TIME CPU time
COMMAND Command name

Stopping Processes

Sometimes, a process might go rogue (like a cook who's burning everything). In such cases, you need to stop it. Here's how:

$ kill PID

Replace PID with the Process ID you want to terminate. If you're feeling particularly ruthless, you can use:

$ kill -9 PID

This is like firing the cook on the spot – it's a forceful termination!

Parent and Child Processes

In Unix/Linux, processes have a family tree. When a process creates another process, it becomes a parent, and the new process is its child. It's like a chef (parent process) assigning tasks to sous chefs (child processes).

You can see this relationship using:

$ pstree

This command shows you the process tree, illustrating the parent-child relationships.

Zombie and Orphan Processes

Now, let's talk about some special types of processes:

  1. Zombie Process: A child process that has completed execution but still has an entry in the process table. It's like a cook who's finished their dish but hasn't clocked out yet.

  2. Orphan Process: A process whose parent has finished or terminated, but it is still running. Imagine a sous chef still working after the head chef has gone home.

To see if you have any zombie processes, you can use:

$ ps aux | grep Z

Daemon Processes

Daemon processes are like the kitchen staff that work 24/7. They run in the background and provide various services. For example, the sshd daemon listens for incoming SSH connections.

To list all daemon processes, you can use:

$ ps -eo 'tty,pid,comm' | grep ^?

The top Command

The top command is your all-in-one kitchen monitor. It provides a dynamic real-time view of the running system. Here's how to use it:

$ top

You'll see a table similar to ps, but it updates in real-time. It's like having a live feed of your kitchen!

Job ID Versus Process ID

Finally, let's clarify the difference between Job ID and Process ID:

  • Process ID (PID): A unique identifier for each process in the system.
  • Job ID: A number assigned by the shell to a process started in the background.

You can see job IDs using:

$ jobs

And you can bring a background job to the foreground using:

$ fg %job_id

Remember, Job IDs are specific to your current shell session, while PIDs are system-wide.

And there you have it, folks! We've covered the basics of Unix/Linux process management. Remember, practice makes perfect, so don't hesitate to try these commands on your system. Just be careful with the kill command – we don't want any accidental terminations in our kitchen!

As we wrap up, I'm reminded of a funny incident from my early days of teaching. I once accidentally killed my text editor process while demonstrating the kill command, losing an hour's worth of unsaved work. Let that be a lesson – always save your work before playing around with processes!

Keep cooking up those processes, and happy computing!

Credits: Image by storyset