Posted: . At: 10:33 AM. This was 1 year ago. Post ID: 17155
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.


A simple program in C. Do 1 second of CPU work and then exit.


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

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.