Does Each Process Have Its Own Hardware Registers
Elements of a process
Figure five.1. The Elements of a Procedure
Process ID
The process ID (or the PID) is assigned by the operating system and is unique to each running process.
Memory
We will learn exactly how a process gets it's retention in the following weeks -- information technology is 1 of the most fundamental parts of how the operating organization works. Withal, for now it is sufficient to know that each process gets it's own section of memory.
In this memory all the program code is stored, along with variables and whatever other allocated storage.
Parts of the memory tin be shared between process (called, non surprisingly shared memory ). You will ofttimes see this called System Five Shared Retentiveness (or SysV SHM) after the original implementation in an older operating system.
Another important concept a procedure may use is that of mmap ing a file on disk to memory. This ways that instead of having to open the file and use commands such as read()
and write()
the file looks equally if it were any other type of RAM. mmaped
areas have permissions such as read, write and execute which need to be kept track of. As we know, it is the job of the operating arrangement to maintain security and stability, and then it needs to check if a process tries to write to a read merely expanse and render an error.
Lawmaking and Data
A process can be further divided into lawmaking and data
sections. Program code and data should be kept separately since they crave different permissions from the operating system and separation facilitates sharing of code (as you lot encounter later on). The operating system needs to give program code permission to exist read and executed, merely generally not written to. On the other hand data (variables) crave read and write permissions merely should not exist executable[12].
The Stack
I other very important part of a process is an area of retentiveness called the stack . This can be considered function of the data section of a procedure, and is intimately involved in the execution of any program.
A stack is generic data structure that works exactly like a stack of plates; you tin can button an item (put a plate on top of a stack of plates), which and so becomes the top item, or yous can pop an detail (have a plate off, exposing the previous plate).
Stacks are fundamental to function calls. Each time a function is called information technology gets a new stack frame
. This is an area of memory which usually contains, at a minimum, the accost to return to when complete, the input arguments to the office and space for local variables.
By convention, stacks usually abound down [13] . This means that the stack starts at a high address in memory and progressively gets lower.
Effigy five.two. The Stack
We tin can run across how having a stack brings about many of the features of functions.
-
Each function has its own re-create of its input arguments. This is because each role is allocated a new stack frame with its arguments in a fresh surface area of retentivity.
-
This is the reason why a variable defined within a function tin non be seen by other functions. Global variables (which can be seen by any function) are kept in a split up area of data memory.
-
This facilitates recursive calls. This ways a function is gratis to call its self over again, considering a new stack frame volition be created for all its local variables.
-
Each frame contains the accost to return to. C only allows a single value to exist returned from a function, and so past convention this value is returned to the calling role in a specified register, rather than on the stack.
-
Considering each frame has a reference to the one before it, a debugger tin can "walk" backwards, post-obit the pointers up the stack. From this it can produce a stack trace which shows you lot all functions that were called leading into this function. This is extremely useful for debugging.
Y'all tin meet how the fashion functions works fits exactly into the nature of a stack. Any function tin can phone call whatsoever other role, which and then becomes the up about function (put on top of the stack). Eventually that office volition return to the role that called it (takes itself off the stack).
-
Stacks do make calling functions slower, considering values must be moved out of registers and into memory. Some architectures allow arguments to exist passed in registers directly; still to proceed the semantics that each function gets a unique re-create of each statement the registers must rotate .
-
You may accept heard of the term a stack overflow . This is a common way of hacking a system by passing bogus values. If yous equally a programmer accept capricious input into a stack variable (say, reading from the keyboard or over the network) you demand to explicitly say how big that data is going to exist.
Allowing any corporeality of data unchecked will simply overwrite memory. By and large this leads to a crash, but some people realised that if they overwrote just enough retention to place a specific value in the return accost part of the stack frame, when the function completed rather than returning to the correct place (where information technology was called from) they could make it return into the data they but sent. If that information contains binary executable code that hacks the organisation (due east.thousand. starts a terminal for the user with root privileges) and so your calculator has been compromised.
This happens considering the stack grows down, just data is read in "upward" (i.east. from lower address to college addresses).
At that place are several ways effectually this; firstly as a programmer yous must ensure that y'all always bank check the corporeality of information you are receiving into a variable. The operating organization can aid to avoid this on behalf of the programmer by ensuring that the stack is marked as not executable ; that is that the processor will non run any lawmaking, even if a malicious user tries to pass some into your plan. Modern architectures and operating systems support this functionality.
-
Stacks are ultimately managed by the compiler, as information technology is responsible for generating the program lawmaking. To the operating system the stack just looks like any other area of memory for the procedure.
To keep track of the current growth of the stack, the hardware defines a annals as the stack pointer . The compiler (or the developer, when writing in assembler) uses this register to go on rail of the current top of the stack.
Case 5.1. Stack pointer example
1 $ cat sp.c void function(void) { 5 int i = 100; int j = 200; int k = 300; } 10 $ gcc -fomit-frame-arrow -Due south sp.c $ true cat sp.s .file "sp.c" .text 15 .globl function .blazon function, @office role: subl $xvi, %esp movl $100, four(%esp) 20 movl $200, viii(%esp) movl $300, 12(%esp) addl $16, %esp ret .size office, .-function 25 .ident "GCC: (GNU) 4.0.ii 20050806 (prerelease) (Debian 4.0.1-4)" .section .notation.GNU-stack,"",@progbits
To a higher place nosotros prove a uncomplicated office allocating three variables on the stack. The disassembly illustrates the utilize of the stack pointer on the x86 architecture[14]. Firstly we allocate some space on the stack for our local variables. Since the stack grows down, we subtract from the value held in the stack pointer. The value 16 is a value large plenty to hold our local variables, but may non be exactly the size required (for case with 3 iv byte int
values we really but demand 12 bytes, not xvi) to keep alignment of the stack in memory on certain boundaries every bit the compiler requires.
Then we move the values into the stack memory (and in a real office, use them). Finally, before returning to our parent function we "pop" the values off the stack by moving the stack pointer dorsum to where it was before we started.
The Heap
The heap is an expanse of memory that is managed by the process for on the wing retentiveness allotment. This is for variables whose memory requirements are non known at compile time.
The bottom of the heap is known as the brk , then chosen for the arrangement phone call which modifies it. Past using the brk
call to abound the expanse downwards the process can request the kernel allocate more retentiveness for it to use.
The heap is most normally managed by the malloc
library telephone call. This makes managing the heap easy for the programmer past allowing them to simply classify and free (via the free
call) heap memory. malloc
tin use schemes like a buddy allocator to manage the heap memory for the user. malloc
can also be smarter well-nigh allocation and potentially use anonymous mmaps for actress process memory. This is where instead of mmaping a file into the process retentivity it directly maps an area of system RAM. This tin be more than efficient. Due to the complexity of managing memory correctly, information technology is very uncommon for whatsoever modern programme to have a reason to call brk
directly.
Memory Layout
Figure 5.iii. Procedure memory layout
As we have seen a process has smaller areas of memory allocated to it, each with a specific purpose.
An example of how the process is laid out in memory by the kernel is given to a higher place. Starting from the top, the kernel reserves its self some retentiveness at the top of the process (we meet with virtual memory how this memory is actually shared betwixt all processes).
Underneath that is room for mmaped
files and libraries. Underneath that is the stack, and below that the heap.
At the bottom is the programme image, as loaded from the executable file on disk. We have a closer look at the procedure of loading this data in later capacity.
File Descriptors
In the first calendar week we learnt about stdin
, stdout
and stderr
; the default files given to each process. You volition remember that these files always have the same file descriptor number (0,1,2 respectively).
Thus, file descriptors are kept by the kernel individually for each process.
File descriptors also have permissions. For instance, you may be able to read from a file only non write to information technology. When the file is opened, the operating system keeps a record of the processes permissions to that file in the file descriptor and doesn't let the procedure to do anything information technology shouldn't.
Registers
We know from the previous chapter that the processor essentially performs mostly uncomplicated operations on values in registers. These values are read (and written) to retention -- nosotros mentioned higher up that each process is allocated retention which the kernel keeps rail of.
So the other side of the equation is keeping track of the registers. When it comes time for the currently running process to give up the processor so another process can run, information technology needs to save it'south electric current state. Every bit, we need to exist able to restore this state when the process is given more time to run on the CPU. To do this the operating system needs to store a re-create of the CPU registers to memory. When it is time for the process to run over again, the operating system volition copy the register values back from memory to the CPU registers and the process will be right back where information technology left off.
Kernel Country
Internally, the kernel needs to proceed runway of a number of elements for each process.
Process State
Another important element for the operating organisation to go along track of is the process land. If the process is currently running it makes sense to have it in a running land.
However, if the procedure has requested to read a file from deejay we know from our memory hierarchy that this may take a significant amount of time. The process should give upwardly information technology's electric current execution to allow another process to run, but the kernel need non permit the procedure run once more until the data from the disk is available in memory. Thus it can mark the process as disk wait (or like) until the information is ready.
Priority
Some processes are more important than others, and become a higher priority. See the discussion on the scheduler below.
Statistics
The kernel can proceed statistics on each processes behaviour which can aid it brand decisions near how the procedure behaves; for example does it mostly read from disk or does it mostly exercise CPU intensive operations?
Does Each Process Have Its Own Hardware Registers,
Source: https://www.cs.swarthmore.edu/~kwebb/cs31/s15/bucs/elements_of_a_process.html
Posted by: campbellwhation.blogspot.com
0 Response to "Does Each Process Have Its Own Hardware Registers"
Post a Comment