Skip to content
Related Articles

Related Articles

Improve Article

Implementation of sleep (system call) in OS

  • Last Updated : 15 Sep, 2021
Geek Week

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 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.

 

My Personal Notes arrow_drop_up
Recommended Articles
Page :