Skip to content

LukaPavlovicGit/xv6-interprocess-communication

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Adding process communication support to XV6 Operating System

Xv6 has been modified to support communication between processes. Processes communicate using shared memory, which is limited to only one parent-child relationship. In this example I have created three processes, one primestart which is the parent process, and two others primecom and primecalc which are child processes. The primestart process creates structures that are shared by the other two processes and reports them to the operating system. Primecom receives commands from the user and pushes the command ID into shared memory. Primecal computes prime numbers and writes them to shared memory. It also reads the commands sent by primecom and executes them.

Here is a more detailed explanation of the problem and how I solved it.

The parent process must tell the operating system which memory addresses to share, and all child processes should be given the same physical addresses in their virtual memory space by the operating system.

To enable memory sharing, I created a new structure called shared that contains metadata about the parts of memory to be shared, and added two additional attributes to the proc structure: Pointer to the parent page directory and the array of shared structures.

The system changes I made to support process communication functionality are:

  1. fork() modification:
    When the system call fork is invoked, the new process should inherit all common structures from its parent. At this point, the child has only one copy of the shared memory. This means that the child cannot yet communicate with its parent.

  2. exec() modification:
    I have limited the regular process size to a maximum of 1 GB. From 1 GB up to 2 GB, the physical addresses of the shared memory inherited from the parent are allocated. At this point, communication between child and parent is possible because the real physical addresses of the parent's shared memory are mapped in the child's virtual memory from 1 GB up to 2 GB.

Additional changes to the xv6 OS:

  1. Two system calls :

    • int share_mem(char *name, void *addr, int size) :
      This system call is invoked by the parent process to report memory to be shared.
      @param *name - unique name for the shared memory.
      @param *addr - address of the shared memory.
      @param size - size of the shared memory.
      Returnig value is: 0~9 if share went successfully, -1 - if any of parametars is wrong, -2 - shared structure with a 'name' already exist, -3 - if exist more than 10 shared structures.

    • int get_shared(char *name, void **addr) :
      This system call is invoked by child processes to access a common structure.
      @param *name - name of the shared structure which child process want to access.
      @param **addr - pointer (passed by reference) which needs to point on shared memory after system call is over.
      Returning value is: 0 - if calls went successful, -1 - if any of parametars is wrong, -2 - if there is no shared structure named with 'name'.

  2. Three user programs:

    • primestart :
      This program prepares structures to be passed on to its children..
      That structures are:
      - empty array sized of 400000 bytes (type of int).
      - counter of the position in the array (type of int)
      - command indicator (type of int)

    • primecom :
      This program receives commands from the user.
      Possible commands are:
      - prime <n> - prints n-th prime number. If n-th number is not calculated error is thrown.
      - latest - prints latest calculated prime number.
      - pause - pause calculation of prime numbers.
      - resume - resume calculation of prime numbers.
      - end - notifies primecalc to exit process, and shut down himself.

    • primecalc :
      This program calculates prime numbers. When a prime number is found, it writes the number to the common array and also increments the position counter of the array. Also, this program checks if the command some was sent by the program primecom.