Timer in C++ using system calls Last Updated : 05 Jul, 2021 Comments Improve Suggest changes Like Article Like Report The task is to create timer without using any graphics or animation. The timer will be made using system calls wherever necessary. Timer in this context means a stopwatch with up-counting of time.The timer is created in Linux. Following system calls of Linux are used: sleep() : It will make the program sleep for number of seconds provided as arguments to the function. system() : It is used to execute a system command by passing the command as argument to this function. Below is the implementation for creating timer using System Calls: CPP // CPP program to create a timer #include <iomanip> #include <iostream> #include <stdlib.h> #include <unistd.h> using namespace std; // hours, minutes, seconds of timer int hours = 0; int minutes = 0; int seconds = 0; // function to display the timer void displayClock() { // system call to clear the screen system("clear"); cout << setfill(' ') << setw(55) << " TIMER \n"; cout << setfill(' ') << setw(55) << " --------------------------\n"; cout << setfill(' ') << setw(29); cout << "| " << setfill('0') << setw(2) << hours << " hrs | "; cout << setfill('0') << setw(2) << minutes << " min | "; cout << setfill('0') << setw(2) << seconds << " sec |" << endl; cout << setfill(' ') << setw(55) << " --------------------------\n"; } void timer() { // infinite loop because timer will keep // counting. To kill the process press // Ctrl+D. If it does not work ask // ubuntu for other ways. while (true) { // display the timer displayClock(); // sleep system call to sleep // for 1 second sleep(1); // increment seconds seconds++; // if seconds reaches 60 if (seconds == 60) { // increment minutes minutes++; // if minutes reaches 60 if (minutes == 60) { // increment hours hours++; minutes = 0; } seconds = 0; } } } // Driver Code int main() { // start timer from 00:00:00 timer(); return 0; } Output: Note: This can be made to run on Windows with a bit of modifications. Modifications required: 1. Use "cls" in place of "clear" in system() call. 2. Use 'S' in sleep() function in place of lower case 's' in sleep() function. 3. Include windows.h header file.Making these changes code should run perfectly fine on Windows. Comment More infoAdvertise with us Next Article Timer in C++ using system calls S shubham_rana_77 Follow Improve Article Tags : Technical Scripter Linux-Unix C++ Practice Tags : CPP Similar Reads system() in C/C++ The system() function is used to invoke an operating system command from a C/C++ program. For example, we can call system("dir") on Windows and system("ls") in a Unix-like environment to list the contents of a directory.It is a standard library function defined in <stdlib.h> header in C and 6 min read Print system time in C++ (3 different ways) First Method Printing current date and time using time() Second Method CPP // CPP program to print current date and time // using time and ctime. #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { // declaring argument of time() time_t my_time = time(NULL); // ct 2 min read Calling Conventions in C/C++ In C/C++ programming, a calling convention is a set of rules that specify how a function will be called. You might have seen keywords like __cdecl or __stdcall when you get linking errors. For example: error LNK2019: unresolved external symbol "void __cdecl A(void)" (?A@@YAXXZ) referenced in functio 11 min read mktime() function in C++ STL The mktime() is an inbuilt C++ function which converts the local calendar time to the time since epoch and returns the value as an object of type time_t. Syntax : time_t mktime( struct tm *time_ptr ) Parameters: The function accepts a mandatory parameter pointer time_ptr that points to a tm object s 2 min read How to Measure Elapsed Time in C++? Measuring elapsed time is a common requirement for most software development packages. It is used to determine the efficiency of the program and gives an idea of which parts of the program takes which much time. This helps in optimizing the code, such that improves its execution time. In this articl 3 min read Like