Implementation of sleep (system call) in OS
In this article, we are going to learn about sleep (system call) in operating systems. In the computer science field, a system call is a mechanism that provides the interface between a process and the operating system. In simple terms, it is basically a method in which a computer program requests a service from the kernel of the operating system.
What is sleep in the operating system?
Sleep is a computer program and when you call this method, then it sets the process to wait until the specified amount of time proceeds, then goes and finds some other process to run.
The sleep system call is used to take a time value as a parameter, specifying the minimum amount of time that the process is to sleep before resuming execution.
Methods used :
- sleep() –
This method is used to sleep a thread for a specified amount of time.
- start() –
This method is used to create a separate call stack for the thread.
- run() –
This method is used if the thread was constructed using a separate Runnable object. Otherwise this method does nothing and returns.
Example 1 –
Note - In the above example you can notice tha at a time only one thread has been executed and if you sleep a thread for thespecified time,the thread shedular picks up another thread and so on.
Points to remember while using the Tread.sleep() :
- Whenever you use this method, it will pause the current thread execution.
- Interrupted Exception will be thrown when any other thread interrupts when the thread is sleeping.
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.