Git Product home page Git Product logo

os's Introduction

Operating System

Important Concepts

  1. OS? -> OS is a software which manages system resources such as CPU, Storage, I/O devices.

    OS acts as a library which provide (Interface) some set of system calls, services to manage hardware and software resources.

    These system calls are called API's

    • For Windows - win32 API's
    • For Linux - POSIX API's

    Why OS?

    To make source code portable

  2. System Calls -> The only way for the user level program to interact with te kernal to perform previlaged operation like accessing files, devices, memory.

  3. OS Goals ->

    • Resource management
    • Security
    • Maximum CPU utilization
    • Efficiency
  4. Process -> Program under execution is called "process". Only single process can run at a time in one CPU (core).

  5. Threads -> Lightweight process which runs within a programs's process and shares the same memory space.

  6. Registers -> Very small amount of storage located within the CPU.
    Used to store data that is frequently accessed or manipulated by the CPU.
    Registers are faster to access memory.

    Some important registers are:

    • Program Counter (PC): Keep track of the next instruction to be executed.
    • Stack pointer (SP): Holds memory address of the top of the stack.
    • Base Pointer (BP):
    • Status Register (FLAGS): Store the outcome of the most recent arthematic or logical operation. e.g carry, zero, overflow flags(Allocated memory overflow if value is 1) .

  7. PC Architecture(x86) ->

    • Registers: High speed memory storing unit.

    • ALU: (Arthematic Logic Unit) Performs all arthematic and logic operations.

    • MMU: (Memory Management Unit) Converts virtual addresses into physical addresses.

    • Cache: Smaller, faster memory. It stores copies of data from frequently used memory locations.

  8. Physical Address Space (Actual memory locations available in computer's RAM)

    • Max Size of RAM depends on architecture of the CPU.

      32-bit Architecture => 232 => 4GB RAM

      64-bit Architecture => 264 => 16EB RAM

    • Memory Units

      1 Bit 0, 1
      8 Bits 1 Byte
      1024 Bytes 1 KB 210 Bytes
      1024 KB 1 MB 220 Bytes
      1024 MB 1 GB 230 Bytes
      1024 GB 1 TB 240 Bytes
      1024 TB 1 PB 250 Bytes
      1024 PB 1 EB 260 Bytes
      1024 EB 1 ZB 270 Bytes
    • Memory Maped I/O :

      • Peripheral devices are assigned a range memory addresses, that corrosponds to the device register's and control structure.
      • The CPU can read and write to the device's register by reading and writing to a corrosponding register.
    • VGA Memory

      • Memory used by GPU to store images and data for display on monitor.
    • Low Addresses

      • Memory reserved for OS, BIOS, low-level components.

  9. Memory Virtualization ->


    • Virtualization: Creation of virtual resource from physical resource. It allows multiple programs to share single physical resource.

    • When program compiles it assumes that it has entire memory(0 to some specific address (say 16kb)). These memory addresses are called Virtual Addresses.

    • So, at compilation virtual addresses are assigned by the compiler.

    • When program runs virtual addresses are converted to physical addresses.

    • This conversion is done by MMU (Memory Management Unit), hardware present in CPU.

    • Physical Address (PA) $=$ Base Address (BA) $+$ Virtual Address (VA)

      Assert (BA $+$ VA $<$ BA $+$ Limit) ---> Illegal memory exception(OS will kill this process immediately)

  10. Concurrency -> Ability to access or execute multiple task or process simultaneously.

  11. Persistance Ability of program or data to remain on a storge device even after the device has been turnd off or terminated.



Header Files

  • unistd.h (Unix-Standard)

    This Header file provides access to the POSIX Operating System API.

    #include<unistd.h>
  • fcntl.h (File-Control)

    This Header file is used to perform various file control operations on a file descriptor, such as changing the file access mode, open, close or setting file status flags.

    #include<fcntl.h>


System Calls

  • Fork

    • The fork() system call is used to creates duplicate copy of the parent process (called child process).

    • After the new child Process is created both the process (parent, child) will execute the next instruction.

    • Child process uses same pc(program counter) register, CPU registers, open file descriptors.

    • fork() doesn't take any argument and returns an integer value.

      int p = fork(); 
      • p<0

        Failed to create new child processs.

      • p=0

        Child process is created successfully. In child process value of p is 0.

      • p>0

        In parent process value of p is the PID(process ID) of the child process.

  • Exec

    • When program invokes exec system call, the operating system replaces the current process's code and data with the code and data of the new process specified by the user.

    • Exec system call replaces the current process image with a new process image.

      int exec (const char *path, const *char argv[]);
      • path -> The path of executable file. This can be absolute or relative.
      • argv -> Array of argumants to be passed to executable file. The last element must be null.
    • If the return value of exec is -1 it means that exec failed and control return to caller.

  • Input Output System Calls

    • Terminology

      • File Descriptor -> An integer that uniquely identifies an open file.

      • File Descriptor Table -> Collection of indices of file descriptor. Each process has its file descriptor table.

      • Standard File Descriptor -> By default first three entries of FDT are automaticaly filled.

        Entries (FD)

        • 0 -> Read from stdin (Keyboard)
        • 1 -> Write to stdout (Display)
        • 2 -> Write to stderr (Error)
    • Input/Output calls

      • Create

        int create ("file name", mode);
      • Open

        int open ("path", modes);
      • Close

        int close (int fd);
      • Read

        size_t read (int fd, buff, sizeOfBuff);
      • Write

        size_t write (int fd, buff, sizeOfBuff);
  • Wait

    When a process calls the "wait" system call, the operating system blocks the parent process until one of the child process terminates, and then returns information about the child process.

    int wait(int *status);
    int wait(NULL);
    • The "status" variable will be used to store the exit status of the child process that has terminated.
    • The exit status is stored in the "status" variable using a bit field structure. The lower 8 bits of the "status" variable store the exit code of the child process, while the upper 8 bits store additional information such as whether the child process terminated due to a signal, and if so, which signal it was.
    • The "wait" system call returns the process ID of the terminated child process to the parent process.
  • Waitpid

    Similar to "wait", the only difference is that "wait" waits for any child process to terminate and collect its exit status, while "waitpid" waits for a specific child process to terminate and collect its exit status. The specific child's pid is passed to the "waitpid" as argument.

    pid_t waitpid (pid_t pid, int *status, int options);

    Options to specify additional behaviour.

  • Pipe (Inter-Process Communication)

    • used to move data between the two processes.

      Command1 --> Command2
      
    • used for inter-process communication

      int pipe (int pipefd[2]);

      pipe store two file descriptors in the array passed to the pipe.

      int fd[2];
      pipe(fd);
      fd[0]; // file descriptor for using read end (recieve data)
      fd[1]; // file descriptor for using write end (send data)
    • pipe return -1 when fails.

  • Inter-Process Communication using Shared Memory

    • System calls used:

      int shmget(key_t key, size_t size, int shmflg); // It is used to create the shared memory senment
      void *shmat(int shmid, void *shmaddr, int shmflag); // It is used to attach the shared segment to with the process
    • In this method of communication we write two programs:

      • program 1: Sender

        • Create the shared segment
        • Attach to it
        • Write some content into it.
      • Program 2: Receiver

        • Attach itself to the shared segment
        • Read the data written by Program 1.
  • Signal

    • Signal is software intrupt delivered to process or thread by the Operating System.

    • It intrupts the normal flow of execution and transfers control to a signal handler.

      signal(int sigNum, void handler);
      // e.g
      signal(SIGINT, handler);

      Here are some commonly used signal numbers

      SIGINT (2) -> Ctrl+C

      SIGTSTP (20) -> Ctrl+Z

      SIGTERM (15) -> Termination signal

      SIGKILL (9) -> Forced termination signal

      SIGSEGV (11) -> Segmentation fault signal


Installing XV6 Operating System using QEMU(Emulator) On Ubuntu

XV6 is Operating System developed by MIT. Used as a teaching tool to learn about Operating System.

Steps involved in Installation:

  • Update Ubuntu Operating System

    sudo apt-get update

    1

  • Install QEMU (Virtualization tool which allows to run one or more virtual machines)

    sudo apt-get install qemu

    2

    sudo apt-get install qemu-kvm 
    # QEMU KVM allows to access physical resources on Virtual Machine.

    4

  • Clone XV6 from Github

    3

    git clone https://github.com/mit-pdos/xv6-public.git

    5

  • Run XV6

    cd xv6-public
    make qemu
    # or you can run qemu in same window(terminal) as
    make qemu-nox

    6


Adding system call in XV6

  • Assign syscall number in syscall.h file.

    #define SYS_getticks 22
  • Add pointer to system call in syscall.c file.

    [SYS_getticks] sys_getticks,

    The above instruction means when system call number 22 occur, function pointed by sys_getticks is called.

  • Add function prototype in syscall.c file.

    extern int sys_getticks(void);

    extern keyword is used to tell the compiler that sys_getticks is defined in some other file. So that linker can find function definition at link time.

  • Now define the system call function(handler) in sysproc.c file.

    int 
    sys_getticks(void) {
        return ticks;
    } 
  • In the next two steps add system call to user interface, so that user program can access the system call.

    • In file usys.S add below line at the end.

      SYSCALL(getticks);
    • In file user.h add the following line.

      int getticks(void);

Till this step we have successfully added a system call which returns ticks(clock cycle of CPU)

  • Now add a user program to XV6 (A simple C program in xv6 directory e.g: myprogram.c).

    include "types.h"
    include "stat.h"
    include "user.h"
    // include order of the above header file sholud be same as shown, otherwise it may create error.
    
    int main(int argc, char *argv[])
    {
      printf(1, "Ticks: %d\n", getticks());
      exit();
    }
  • Edit Makefile to make our program available for compilation.

    ...
    ...
    ...
    
    _usertests\
    _wc\
    _zombie\
    _myprogram\
    EXTRA=\
    
    ...
    ...

    Run below commands

    make clean
    make
  • Now start XV6 on QEMU

    make qemu-nox

    Run ls to check whether myprogram is available.

os's People

Contributors

sf404 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.