Open In App

Difference Between User-CPU-Time and System-CPU-Time in UNIX

Last Updated : 06 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Unix systems have a time utility that allows a user to see where their application took significant time to process. The syntax of this utility is as follows:            

time <command-to-be-timed>

Its result generally has three categories as follows:

real <time>
user <time>
sys  <time>

User CPU time and system CPU time are both measures of the amount of CPU time used by a process or program.

User-CPU-Time: 

User CPU time is the time taken by the processor to process the code in your application. It is generally termed as running on user space as opposed to being in the kernel space. When you develop your application, you would have written programming constructs such as conditionals, expressions, looping statements, branches, etc., The time spent on the actual code written by the client is measured via user-cpu-time.

User CPU time refers to the amount of CPU time spent by a process or program in user mode, which is the mode in which the application runs.

System-CPU-Time:

System CPU time is the time taken by the process to process kernel code. When an application is programmed, developers need to write to system output, read from system input, and access a local disk resource such as files, databases, etc., These cross-cutting concerns across applications are addressed via system calls that your operating systems provide. The time spent on this execution is generally referred to as system-cpu-time.

Real:

Real-time is the total time spent beginning from the time on which the application has started and the time on which the application has done its task. It may also include time spent waiting for its turn to process or waiting for resources for the successful execution of the program. 

Differences between User-CPU-time and System-CPU-time:

User-CPU-time

System-CPU-time

It is the measure of time taken by the application while executing the code written by the user It is the measure of time taken by an application while executing kernel code
In Unix-based systems, it is generally represented as ‘user’ in response to time utility. In Unix-based systems, it is generally represented as ‘sys’ in response to time utility.
   
The time taken can be analyzed and optimized by the user. Time taken by the system depends on the system calls of the underlying kernel. 
Scope is Limited to the specific process or program Scope can include CPU time used by other processes or the operating system on behalf of the specific process or program
The amount of CPU time spent in user-mode code is called user CPU time. The amount of CPU time spent in kernel-mode code is called system CPU time.
Example:- A program spends 10 seconds executing a loop in its own code. Example:- A program spends 2 seconds waiting for data to be loaded from disk by the operating system.

For Example:

#include<stdio.h>
#include<sys/types.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{

int i=0;
sleep(60);
FILE* fpt;
fpt = fopen("/home/test/file1.txt","w");


for(int i=0;i<10000000;i++)
{
    printf("");
    fprintf(fpt,"%d",i);
}
fclose(fpt);
}

The above code first sleeps for a minute, opens a file resource on the disk, and writes the variable “i” to it till the loop terminates and prints nothing to the console. Finally, it closes the file resource opened from the disk.

We can compile this code with:

gcc test.c

This creates a binary ./a.out

Now if we run,

 time ./a.out

On UNIX systems it may generate output as seen below. The actual time may vary from time to time based on the program logic and CPU utilization. 

Output:

real    1m0.557s
user    0m0.452s
sys     0m0.084s

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

Similar Reads