Linux Basic Topics:
- They are API, given by OS.
- They provide the interface to access the services (services are given by OS).
- Like: fork(), read(), write() and many more.
It is an instance of a running program.
There are three ways to start the process:
system() : wait for other process to finish.
- using system() library function like system(“ps ax”).
- The process who has started this call will wait for process that has been started by system call to finish.
exec(): replace the current process.
- exec() is also library function.
- It replace the original program with new program (started using exec)
- Original program will not start again.
fork(): duplicate the process.
- It is system call.
- It creates the duplicate copy of process.
- It creates the new entry in process table.
- It executes the same code.
- It has own variables and own PID and it executes independently.
- It has own data space, environment, file description.
- int res = fork(); (if res is zero then its newly child process otherwise its parent process).
- wait(), it is wait API and it is used to wait for one of its child process.
Process is no longer active and its execution is also completed BUT its still present in process table (as its name and same we see in movies that person has been dead but its existence is still there).
If parent of any process is terminated then that child process will become orphan process, init() process will become new parent.
Long term scheduler:
- It selects the process from job queue.
- Secondly, It brings the selected process into ready queue.
- It is also named as “job scheduler”.
Short term scheduler:
- It selects the process from ready queue (only select).
- It is also named as “CPU scheduler”.
- It carries out the swap-in and swap-out of the process.
- Its job is to the pick and drop of the process (selected by short term scheduler).
- It allocates the CPU to process that has been selected by “shot term scheduler”
- In other word, it gives the CPU to process selected by “short term scheduler”.
It is the situation where 2 or more process are holding a resource and while waiting for a resource that has been held by other process so no one is able to move.
There are four conditions that are required for deadlock:
- Mutual exclusion: Only one process can access the resource at a time (Resource is NON shareable).
- Hold and wait: Process is holding a resource and waiting for other resource.
- Non-preemption: Resource will be release by process voluntary.
- Circular wait: There all are in circular wait (each process is holding a resource and it is required by next process and so on).
- Deadlock prevention: If one of above condition is false then we can prevent the deadlock.
- Deadlock avoidance: Banker algorithm.
Starvation/Indefinite blocking :
- It is the state when process is in indefinitely wait (waiting state)
- Example: low priority thread that could be in starvation if it does not get CPU.
- Solution: Increase the priority with time.
- It is the state when process is NOT in waiting state.
- It is doing NON progressing work.
- It is NOT doing any useful work.
- It run in background.
- No user interaction is required.
- It provides the service that can be done in background without user interaction.
- It started when system boot strapped (init is parent).
- It is boot strap program.
- It is initial program.
- It initialize the CPU register.
- It initialize the memory.
- It knows How to load OS.
- It knows How to start the system.
- It is the segment of code where common variable is present.
- It has be executed as atomic.
- 2 or more threads are trying the access the same resource at same time.
- They are trying to change it at the same time.
- It is locking mechanism.
- When there is race condition between threads then use it.
- When some operation should be atomic then use it.
- It is signalling mechanism (Thread that is using semaphore when it will release then it will signal to thread that is going to use it).
- When there is situation that thread should be wake up on other thread response then use it.
- binary semaphore has 2 value that 0(wait) and 1(available).
- When we need to control the access to many resources.
- It checks the condition in loop.
- it repeatedly checking that lock/other common resource is available or not.
- thread remain active (does not go for sleep)
- its also called as busy waiting/polling.
- we all wonder that why don’t we use mutex or semaphore because we all know that polling is NOT good option.
- The trick is that our thread DOES NOT go to sleep then we need to use spin lock.