All you need to know about processes and threads
When sharing memory and cpu time a lot of problems may arise when accessing the same resource. We would need to find an efficient way to manage resources. The solution to managing those resources is where threads and processes come in.
What is a process ?
A Process is simply a logical container that holds all the information about each of the applications running on a system including its lifecycle. A process can hold the name of application, process id (pid), priority, vm address, state, program counter and more.
What is a Thread ?
A thread is the smallest sequence of programmed instructions that can be managed independently. It is an execution context which is all the information cpu needs to execute a stream of instructions. When a process is created there is a single thread of execution. The Kernel allows each application to have multiple ways of execution through the program.
If two threads access a single resource we need a way of delegating appropriate access to that resource this is where the concept of locking comes in. Processes give the illusion of simultaneous or parallel execution due to threading
When a particular resource or piece of code is important it can be locked. A situation where two computer programs sharing the same resource are effectively preventing each other from accessing the resource where both programs cease to function is known as deadlock. Locking ensures data isn’t modified by multiple threads. Access management to a shared resource can be done using mutex and semaphores
A mutex is a mutual exclusion object that synchronizes access to a resource. It is created with a unique name at the start of a program. It is a locking mechanism that makes sure only one thread can acquire the mutex at a time and enter the critical section which only released when it exits the critical section. Mutex is basically an object that holds a resource which enables multiple program threads to access that single shared resource but one at a time.
A semaphore is a signalling mechanism. A thread that is waiting on a semaphore can be signaled by another thread. This is different than a mutex as the mutex can be signaled only by the thread that calls it. Semaphores use two atomic operations wait and signal for process synchronization. The wait operation decrements the value of its arguments. For example if S is positive it decrements if S is negative or zero then no operation is performed. Semaphores are usually an integer which allows multiple program threads to access a finite instance of shared resources.
Hope this helps in clarifying what processes and threads are. And the two locking mechanisms used to manage access to shared resources. I found it difficult to grasp these concepts at first and the information was all over the place so I decided give a brief summary about the topic here.