WikiGalaxy

Personalize

System Calls and Interrupts

System Calls

Definition:

System calls provide a programmatic way for user-level processes to request services from the operating system's kernel. They act as an interface between a process and the operating system.

Types of System Calls:

  • Process Control
  • File Management
  • Device Management
  • Information Maintenance
  • Communication

    // Example of a System Call in C (Linux)
    #include <stdio.h>
    #include <unistd.h>
    int main() {
        printf("Hello, World!\n");
        return 0;
    }
    

Explanation:

This C program uses the printf function, which internally makes a write system call to output "Hello, World!" to the console.

Console Output:

Hello, World!

Interrupts

Definition:

Interrupts are signals emitted by hardware or software when a task needs immediate attention. They allow the CPU to respond to events in real-time.

Types of Interrupts:

  • Hardware Interrupts
  • Software Interrupts

    // Example of a Software Interrupt in Assembly (x86)
    section .text
    global _start
    _start:
        mov eax, 1          ; system call number (sys_exit)
        int 0x80            ; call kernel
    

Explanation:

This assembly program demonstrates a software interrupt using the int 0x80 instruction to invoke the sys_exit system call.

Console Output:

Program exits without output.

System Call Example: File Management

Creating a File:

System calls like open, read, write, and close are used for file management. Below is an example of creating a file using system calls.


    // C Program to create a file using system call
    #include <fcntl.h>
    #include <unistd.h>
    int main() {
        int fd = open("newfile.txt", O_CREAT | O_WRONLY, 0644);
        if (fd == -1) {
            return 1; // Error handling
        }
        write(fd, "Hello, File!", 12);
        close(fd);
        return 0;
    }
    

Explanation:

This C program creates a file named "newfile.txt" and writes "Hello, File!" to it using system calls open, write, and close.

Console Output:

File created with content: Hello, File!

Interrupt Example: Hardware Interrupt

Keyboard Interrupt:

A common hardware interrupt is the keyboard interrupt, which occurs when a key is pressed on the keyboard.


    // Pseudo-code for handling keyboard interrupt
    void keyboard_interrupt_handler() {
        char key = get_pressed_key();
        // Process the key
    }
    

Explanation:

This pseudo-code represents a basic structure of a keyboard interrupt handler, which processes the key pressed by the user.

Console Output:

Key processed successfully.

System Call Example: Process Control

Fork System Call:

The fork system call is used to create a new process, which is a copy of the calling process.


    // C Program demonstrating fork system call
    #include <stdio.h>
    #include <unistd.h>
    int main() {
        pid_t pid = fork();
        if (pid == 0) {
            printf("Child process\n");
        } else {
            printf("Parent process\n");
        }
        return 0;
    }
    

Explanation:

This C program uses the fork system call to create a child process, resulting in separate outputs for parent and child processes.

Console Output:

Child process\nParent process

Interrupt Example: Timer Interrupt

Periodic Timer Interrupt:

Timer interrupts occur at regular intervals and are used by the operating system to perform tasks like scheduling.


    // Pseudo-code for timer interrupt handler
    void timer_interrupt_handler() {
        update_system_time();
        schedule_next_task();
    }
    

Explanation:

This pseudo-code represents a basic structure of a timer interrupt handler, which updates system time and schedules tasks.

Console Output:

System time updated, next task scheduled.

System Call Example: Communication

Pipe System Call:

Pipes are used for inter-process communication. The pipe system call creates a unidirectional data channel.


    // C Program demonstrating pipe system call
    #include <stdio.h>
    #include <unistd.h>
    int main() {
        int fd[2];
        pipe(fd);
        if (fork() == 0) {
            // Child process
            close(fd[0]);
            write(fd[1], "Hello", 5);
            close(fd[1]);
        } else {
            // Parent process
            char buffer[5];
            close(fd[1]);
            read(fd[0], buffer, 5);
            printf("Received: %s\n", buffer);
            close(fd[0]);
        }
        return 0;
    }
    

Explanation:

This C program demonstrates inter-process communication using pipes, where the child process sends a message to the parent process.

Console Output:

Received: Hello

Interrupt Example: Software Interrupt

Software Interrupts:

Software interrupts are generated within a program. They are used to handle exceptional conditions like overflow.


    // Pseudo-code for software interrupt handling
    void software_interrupt_handler() {
        handle_overflow_exception();
    }
    

Explanation:

This pseudo-code demonstrates handling a software interrupt triggered by an overflow condition within a program.

Console Output:

Overflow exception handled.

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025