Operating System - Learn Process with C/C++, Python

 Learn Process with c/c++, python

What is Process ?

A process is a execution unit of application(an application is static entity which is stored in storage devices) that is loaded on memory and is being executed. This means that a process is something that is actually doing something on our computer and is a basic unit of execution in an operating system. Process address space is not directly mapped to the physical memory, but they utilize the concept of virtual memory. This is done by the help of MMU(hardware component that translates virtual memory to physical memory). 

Benefits of using virtual memory 

  • Large programs can be written, as available virtual address space size can be larger than actual physical memory size 
  • Less I/O required and this leads to faster and easier way of swapping processes

Anatomy of a Process 

Let's look at the basic structure of a process 
Process structure

Process address space (virtual memory) 

  • Address space of a process consists of all linear addresses that the process is allowed to used. 
  • The address used by one process must have no relation to the address used by another(except for the shared memory during inter-process communication) 
  • Physical memory addresses that are mapped to the virtual memory are not continuous. 
  • Code, Initialized data : stores the data of written code  
  • Stack : keep track of where the process currently is in the function call chain, as well as to allocate local variables and pass parameters and return values to and from routines 
  • Heap : used for dynamically allocating user-managed memory. 
    • ex ) malloc() in C or new in C++ 

Process control block (PCB) 

  • Operating system holds most of active processes in data structures called PCB. 
  • PCB is created when a process is being created and removed when process terminates. 
  • Each process has a single PCB 
  • Information on PCB is updated when process's state changes 

Process states 

Process state

  • New : Process being created 
  • Ready : Process is waiting to be assigned to a processor 
  • Running : Instructions are being executed 
  • Waiting : Process is waiting for some event to occur(I/O completion or reception of signal) 
  • Terminated : Process has finished execution. 

PCB structure 

  • Pointer : It is a stack pointer which is required to be saved when the process is switched from one state to another to retain the current position of the process 
  • Process state : stores the state of the process 
  • Process ID : PID of current process and parent process
  • CPU registers : accumulator, base, registers and general purpose registers  
  • Program counter : holds the address of the next instruction to be executed for that process 
  • CPU scheduling : priority information and pointers to scheduling queues 
  • Memory management information : page tables or segment tables 
  • Accounting information : user and kernel CPU time consumed, account numbers, limits, etc 
  • I/O status information : devices allocated, open file tables, etc

Multitasking and Multiprocessing 

  • Multitasking : switching between multiple processes frequently so that the user can interact with each program while it is running. 
  • Multiprocessing : a system that has 2 or more processors. Because there are more than 2 processors, processes can run in parallel. 

Multiprocessing in C/C++

  • We can use the fork() command (from <unistd.h>) to duplicate the parent process and create a child process. The child process and parent process run in separate memory space. 

Multiprocessing in python 

  • Multiprocessing in python is to spawning up multiple python processes with an API.  
  • There are 2 methods to enable multiprocessing in python 
    • using multiprocessing module 
    • using concurrent.futures module 

Multiprocessing module 

  • Let's see first, how long it will take to accomplish certain tasks
  • We can see that the result it takes approximate 0 + 1 + 2 = 3 seconds without multiprocessing. 
  • Now let's execute this with multiprocessing. 
  • Now the program returned in approximately 2 seconds which is the longest execution of time.sleep(2). By using multiprocessing, we can expect improved performance on either  I/O bound or CPU bound operations. 

Multiprocessing with concurrent.futures module 

  • This module is convenient in that it can also be used to execute multi-threads. 
  • This code illustrates basic usage of concurrent.futures in multiprocessing 
  • Let's look at CPU_bound execution whose performance can be increased with multiprocessing.
  • Without multiprocessing 
  • With multiprocessing


REFERENCE 

Comments

Popular posts from this blog

TCP with Python

Operating System - Learn Thread with C/C++, Python

UDP with Python