It’s been a while since I last posted. I had been tied to projects and couldn’t escape to update the blog. But now I am back and ready to roll.

Today I’ll define the differences between a Process and a Thread. First things first, let’s see the definitions.

A process:

Simply put a process is a term used to describe the set of resources (such as external code libraries and the primary thread) and the necessary memory allocations used by a running application.

For each .exe loaded into memory, the OS creates a separate and isolated process for use during it lifetime. Using this approach to application isolation, the result is a much more robust and stable runtime environment, given that the failure of one process does not affect the functioning of another.

Every win32 process is assigned a unique process identifier (PID).

A Thread:

A thread can simply be defined as a path of execution within a process. The first thread created by a process’s entry point is called the primary thread.

Every Win32 process has exactly one main thread that functions as the entry point for the application. A Win32 Graphical User Interface (GUI) application defines a method called WinMain() as the application entry point, while a console based application defines the Main() method as the entry point.

process-thread relationship

The .Net Framework provides the System.Diagnostics namespace that offers, among others, some types that help with the interaction and manipulation of processes programmatically.

This code below for example will enumerate and list all the running processes and all the active threads running under each process.

I will not go into the details of explaining all the keywords in the code at this moment, but this is simply to further elaborate the differences between a Process and a Thread.

[sourcecode language=”csharp”]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//for processes
using System.Diagnostics;

namespace ThreadvsProcess
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(“xxxxx Processes and Threads in .Net xxxxxn”);

ShowRunningProcesses();

//press Enter to exit
Console.ReadLine();
}

static void ShowRunningProcesses()
{
// List all the processes currently running on the local machine.
Process[] allProcesses = Process.GetProcesses(“.”);

// Show the Process ID (PID) and name of each process.
foreach (Process p in allProcesses)
{
string details = string.Format(” Process ID: {0}tName: {1}”,
p.Id, p.ProcessName);
Console.WriteLine(details);
//show the Threads running under this process
DisplayThreadsForPID(p.Id);
}
Console.WriteLine(“xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxn”);
}

static void DisplayThreadsForPID(int pID)
{
try
{
Process currProc = null;
currProc = Process.GetProcessById(pID);

// List out the details for each thread in the specified process.
Console.WriteLine(” Threads used by: {0}”,
currProc.ProcessName);
ProcessThreadCollection allThreads = currProc.Threads;
foreach (ProcessThread th in allThreads)
{
string detail =
string.Format(” Thread ID: {0}tTime Started: {1}tPriority: {2}”,
th.Id, th.StartTime.ToShortTimeString(), th.PriorityLevel);
Console.WriteLine(detail);
}
Console.WriteLine(“xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxn”);
}
catch (Exception)
{
//ignore the exception for simplicity
return;
}
}

}
}
[/sourcecode]

If you run this code, you’ll see the output similar to the one shown in the  image below depending on the processes currently running on your machine.

output

There’s a lot more you can do with Processes and Threads apart from just enumerating and dumping them to the console window and this was just a scratch at the top to show the differences between the two.

Till next time, yours truly.

An Overview of Processes and Threads in .Net
Tagged on:         

Leave a Reply

Your email address will not be published. Required fields are marked *