Processes and Threads

eternaloptimist

Well-Known Member
Joined
Jul 10, 2013
Messages
175
hey everyone!
Sorry for the noob questions. Am I right in thinking that a process runs inside a thread? A process is a program in execution?
In a multi core system, is it still one process that can be run at a time or can multiple processes be in execution at the same time?

I appreciate all the help can get.
Thanks
 

[)roi(]

Executive Member
Joined
Apr 15, 2005
Messages
6,282
hey everyone!
Sorry for the noob questions. Am I right in thinking that a process runs inside a thread? A process is a program in execution?
In a multi core system, is it still one process that can be run at a time or can multiple processes be in execution at the same time?

I appreciate all the help can get.
Thanks
Google is your friend:
 

gkm

Expert Member
Joined
May 10, 2005
Messages
1,519
But in summary, it is the other way round. Threads run in a process. Operating system then runs multiple processes. And on a multi-core system, multiple threads can run concurrently, from the same or different processes. Even on a single core system, multiple threads can run kind of at the same time, but then they get time slices. Time slices apply to multi-core systems as well, since pretty much always there are more threads than cores.
 

Solarion

Honorary Master
Joined
Nov 14, 2012
Messages
21,886
Doesn't a Process start a Thread?

A Thread being a Process in action.
 

cguy

Executive Member
Joined
Jan 2, 2013
Messages
8,527
Doesn't a Process start a Thread?

A Thread being a Process in action.

There's a lot of variation between the terms depending on OS. Generally though, when a process is created, at least one thread is created with it that can execute code, however, as gkm said, multiple threads can be created in one process the one initial thread can create one or more threads, which each could conceivably also create one or more threads - these are all part of the same process though. A process is generally about resource control for a task - OS resources (file handles, page tables/address spaces, inherited core affinities, etc.) are assigned at the process level and shared by the threads within that process. Processes can create other processes too, and may be killed when the parent process is killed (there's generally a way to control this life-scope dependency on process creation).

Windows tends to be more explicit about what it calls a process and a thread (threads are part of processes and each are explicitly different data structures in the kernel) - in many ways, at the kernel level Linux treats threads and processes the same - the difference being how they are forked (with a new address space, resource table, etc. vs cloning that of the creating thread). OS apps like "top" will present a view of either threads or processes (optional), where threads sharing the same address space and resource table are lumped together into a "process".

As gkm said, threads generally get time sliced access to the CPU cores - this also happens on multi-core systems.

Tl;dr - only threads create anything, since they're what execute. They can create threads, and they can create threads that have their own OS resources (page tables, file handles, process struct, etc.).
 
Last edited:

[)roi(]

Executive Member
Joined
Apr 15, 2005
Messages
6,282
For anyone who is still confused or too lazy to read; this Youtube series covers pretty much everything you'd need to know about this type of stuff.

UC Berkeley's Computer Science 162: Operating Systems and System Programming
Note: I suggest you start with second video to understand everything related to Processes & threads, and more...

Here's a link I posted about a while ago, but is useful for this type of theory and a lot more.
 

Solarion

Honorary Master
Joined
Nov 14, 2012
Messages
21,886
Thanks cguy and gkm and [)roi(] great links there. Lots of info there. I've only recently had to dive into threading when programming fingerprint scanners, it's still a work in progress. I have a a Process running as a Windows service which pings 50 biometric scanners (every 5 seconds), tracks their status, and pulls logging data off of them at regular intervals. The real headache with this is there are multiple threads that need to run when opening up the main User Console, used for reporting, configuration of Fingerprint scanners etc. I've got until December to figure it out or else the company I'm programming it for will go with an out of box solution which isn't ideal.

Need a lot more coffee and reading on this matter.
 

[)roi(]

Executive Member
Joined
Apr 15, 2005
Messages
6,282
Yes, to add to what cguy said, that is often called the main thread.
Careful to not confuse with the main thread of an application process.
- To clarify: Main application thread of an application usually comprises of a run loop which loops through a predefined set of activities e.g. checking for screen updates, etc...

But you are correct in saying all processes start on some main thread -- even the OS has one.
 
Last edited:

[)roi(]

Executive Member
Joined
Apr 15, 2005
Messages
6,282
Thanks cguy and gkm and [)roi(] great links there. Lots of info there. I've only recently had to dive into threading when programming fingerprint scanners, it's still a work in progress. I have a a Process running as a Windows service which pings 50 biometric scanners (every 5 seconds), tracks their status, and pulls logging data off of them at regular intervals. The real headache with this is there are multiple threads that need to run when opening up the main User Console, used for reporting, configuration of Fingerprint scanners etc. I've got until December to figure it out or else the company I'm programming it for will go with an out of box solution which isn't ideal.

Need a lot more coffee and reading on this matter.
Sounds like solution == separate processes? device communications, reporting, ...
 

[)roi(]

Executive Member
Joined
Apr 15, 2005
Messages
6,282
Sorry don't know this one.

Also don't forget that there a quite a few free books availlable; here's a list that is maintained on github:
Not sure what language you're using; here for example is one catering to C#:
/Edit:
Here's links to the UC Berkeley reference materials:

Btw if you follow a more functional style: contain or remove state then threading becomes far easier because you won't have you worry about thread safety (incl. race conditions)
 
Last edited:

gkm

Expert Member
Joined
May 10, 2005
Messages
1,519

cguy

Executive Member
Joined
Jan 2, 2013
Messages
8,527
I have a book from Tanenbaum on my shelf that is probably the precursor to this book and it is very good and complete. So, I think definitely worth reading.

Yeah - I looked through the contents page and it looks very complete. A few years ago I read "Inside the Linux Kernel", which is more obviously more focused on just Linux, and has less info about different types of OSes, scheduling algorithms and deadlock prevention - it misses out a lot, but the advantage is that it's much shorter, and a lighter read.
 
Top