paint-brush
The ABCs of Operating Systems by@demolitionthor
226 reads

The ABCs of Operating Systems

by Ankit jaiswal November 16th, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

This article explains the role of an operating system, process management, memory management, and system calls. It uses the xv6 operating system as a practical example to illustrate these concepts. The text uses the operating system that closely follows the design principles of the Unix operating system.
featured image - The ABCs of Operating Systems
Ankit jaiswal  HackerNoon profile picture

This is a foundational introduction to the core concepts of operating systems, including the role of an operating system, process management, memory management, and system calls. It uses the xv6 operating system as a practical example to illustrate these concepts.



A kernel and two user processes.





  1. Role of an Operating System: Manage and share a computer's resources among multiple programs while providing a higher level of services and abstractions compared to what the hardware alone can offer.


  2. Abstraction of Hardware: Abstract low-level hardware details, ensuring that applications (like a word processor) don't need to be concerned with the specifics of the underlying hardware. For example, a word processor doesn't need to know the intricacies of various types of disk hardware.


  3. Multiprogramming: Enables multiple programs to run seemingly concurrently. It efficiently allocates CPU time to these programs so that they appear to run simultaneously.


  4. Controlled Interaction: Operating systems provide controlled mechanisms for programs to interact with one another, such as sharing data or collaborating. This ensures that programs work together in an orderly fashion.


  5. Challenges of Interface Design: Designing an operating system interface is a delicate balance. On one hand, it should be simple and narrow to simplify implementation. On the other hand, it may need to offer sophisticated features for applications. Striking the right balance is essential.


  6. Unix as an Example: The text uses xv6, an operating system that closely follows the design principles of the Unix operating system, as a concrete example. Unix is known for its simple yet powerful interface. This design has influenced many modern operating systems like BSD, Linux, Mac OS X, and to some extent, Microsoft Windows.


  7. Kernel and User Space: An operating system typically follows a kernel-based architecture, where a special program (the kernel) provides services to running programs (processes). Each process has its own memory space containing instructions, data, and a stack. The kernel uses hardware protection mechanisms to isolate processes from each other and ensure that each process can only access its own memory.


  8. System Calls: Processes invoke kernel services using system calls. These system calls provide the interface that user programs use to interact with the operating system. For example, when a process needs to open a file or allocate memory, it invokes the corresponding system call.


  9. Interface Examples: The xv6 operating system offers a subset of system calls similar to those in Unix. These system calls cover various aspects, including process management (e.g., fork, exec), file operations (e.g., open, read, write), and other functionalities (e.g., sleep, getpid).


  10. Processes and Memory Management: xv6 manages processes, which include user-space memory (instructions, data, stack) and per-process state private to the kernel. Processes are time-shared by xv6, meaning the operating system switches CPUs among processes that are ready to execute. Each process has a unique Process Identifier (PID).


  11. Process Creation and Termination: Processes can create new processes using the fork system call. This creates a child process with the same memory contents as the parent. Processes can also terminate using the exit system call, providing an exit status (0 for success, non-zero for failure). The wait system call allows a parent to wait for its child to exit.


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int main()
{
    int pid = fork();
    if(pid > 0 ){
      printf("parent: child=%d\n", pid);
      pid = wait((int *) 0);
      printf("child %d is done\n", pid);
    } 
    else if(pid == 0){
      printf("child=%d\n", pid);
      printf("child: exiting\n");
      exit(0);
    } 
    else {
      printf("fork error\n");
    }

}

Output:
parent: child=403
child: exiting
child 403 is done



  1. Program Execution: The exec system call replaces a process's memory with a new image loaded from a file, effectively starting a new program. It is essential for launching different applications or utilities.


    
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/wait.h>
    
    int main()
    {
        char *argv[3];
        argv[0] = "echo";
        argv[1] = "hello";
        argv[2] = 0;
        exec("/bin/echo", argv);
        printf("exec error\n");
    }