Skip to content
Related Articles

Related Articles

Implementation of sleep (system call) in OS

Improve Article
Save Article
  • Last Updated : 28 Nov, 2022
Improve Article
Save Article

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.

Syntax –

 sleep(time);

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 – 

Java




class TestSleep extends Thread{ 
 public void run(){ 
  for(int i=1;i<5;i++){ 
    try{Thread.sleep(300);}catch(InterruptedException e){System.out.println(e);} 
    System.out.println(i); 
  
 
 public static void main(String args[]){ 
  TestSleep t1=new TestSleep(); 
  TestSleep t2=new TestSleep(); 
    
  t1.start(); 
  t2.start(); 
 
  

Output –

Note - In the above example you can notice that at a time only one thread has been executed and if 
       you sleep a thread for thespecified time,the thread scheduler 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.
My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!