Here is a simple program in C that will do 1 second of CPU work without using the sleep
function.
#include <stdio.h> #include <time.h> int main() { // Get the current time clock_t start = clock(); // Do some CPU work int i = 0; while (clock() < start + CLOCKS_PER_SEC) { i++; } // Print the result printf("Did %d iterations in 1 second\n", i); // Exit the program return 0; } |
This program gets the current time using the clock()
function from the time.h
header. It then enters a loop that continues until 1 second has passed, as measured by the clock()
function. The loop simply increments a counter variable, which is then printed to the screen before the program exits.
Here is a simple program in x86 assembler that will do 1 second of CPU work without using the sleep
function.
section .text global _start _start: ; Get the current time push dword CLOCKS_PER_SEC call clock add esp, 4 mov [start], eax ; Do some CPU work mov ecx, 0 .loop: inc ecx call clock cmp eax, [start] jl .loop ; Print the result push dword 0 push dword ecx push dword msg call printf add esp, 12 ; Exit the program push dword 0 call exit section .data start dd 0 msg db "Did %d iterations in 1 second", 0 |
This program uses the clock()
function from the C standard library to get the current time and measure the passage of 1 second. It then enters a loop that continues until 1 second has passed, as measured by the clock()
function. The loop simply increments a counter variable, which is then printed to the screen using the printf()
function before the program exits using the exit()
function.
Note that this program is written for x86 architecture and will only work on systems that support this architecture. It also assumes that the C standard library is available and linked to the program.
A simple program to open a window on your desktop and print “Hello World.”
#include <X11/Xlib.h> #include <stdio.h> #include <string.h> int main(int argc, char **argv) { // Open a connection to the X server Display *d = XOpenDisplay(NULL); if (d == NULL) { fprintf(stderr, "Cannot open display\n"); return 1; } // Get the default screen for the display int s = DefaultScreen(d); // Create a window Window w = XCreateSimpleWindow(d, RootWindow(d, s), 0, 0, 200, 100, 1, BlackPixel(d, s), WhitePixel(d, s)); // Select the events that the window will receive XSelectInput(d, w, ExposureMask | KeyPressMask); // Map the window to the screen (make it visible) XMapWindow(d, w); // Create a graphics context for drawing in the window GC gc = XCreateGC(d, w, 0, NULL); // Set the foreground color of the graphics context to black XSetForeground(d, gc, BlackPixel(d, s)); // Main event loop while (1) { // Get the next event XEvent e; XNextEvent(d, &e); // Handle the event if (e.type == Expose) { // Draw the "Hello, World!" message in the window XDrawString(d, w, gc, 10, 50, "Hello, World!", strlen("Hello, World!")); } else if (e.type == KeyPress) { // Exit the event loop when a key is pressed break; } } // Clean up XFreeGC(d, gc); XDestroyWindow(d, w); XCloseDisplay(d); return 0; } |
Compile it like this.
(base) jason@jason-Lenovo-H50-55:~/Documents$ gcc xlibwindow.c -L/usr/X11R6/lib -lX11 -o win |