Open In App

Implementation of sleep (system call) in OS

Last Updated : 26 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

The sleep system call is used to pause the execution of a program for a specified amount of time. The implementation of the sleep system call in an operating system involves modifying several components of the kernel, including the system call interface, system call dispatching mechanism, and the kernel code that implements the sleep system call.

Here are the general steps to implement the sleep system call in an operating system:

  1. Define the system call interface: The first step is to define the interface for the sleep system call, including its name, arguments, and return value. This information should be added to the system call header file.
  2. Assign a system call number: Next, assign a unique number to the sleep system call. This number should be added to the system call header file and the system call dispatching mechanism.
  3. Modify the system call dispatcher: The system call dispatcher is responsible for determining which system call to execute based on the system call number passed by the user program. This mechanism should be updated to handle the sleep system call number.
  4. Implement the sleep system call: The final step is to implement the sleep system call in the kernel code. This involves modifying the appropriate kernel function to handle the sleep system call and adding any necessary data structures or helper functions.
  5. The implementation of the sleep system call may involve several details such as handling interrupts and scheduling the process to sleep for the specified amount of time.
  6. Once the sleep system call is implemented, it can be used by user programs to pause execution for a specified amount of time, providing a useful mechanism for managing system resources and coordinating program execution.

It is worth noting that the implementation of a system call in an operating system can be a complex task that requires a deep understanding of operating system design and implementation. It is recommended to consult the operating system documentation and other relevant resources before attempting to implement a new system call.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads