5 DERECHA
Operating System 본문
The operating system (OS) is a software program that manages and controls the hardware and software resources of a computer system. It is responsible for providing a platform for running applications, managing system resources such as memory, CPU, and I/O devices, and providing user interfaces for interacting with the system.
1. Process
1) Process
A process in an operating system is a program that is currently executing. It can be a single-threaded or multi-threaded program that is loaded into memory and is being executed by the CPU. The OS manages and schedules processes, provides resources such as memory and I/O devices to processes, and enforces security and protection between processes. Each process has a unique process ID (PID) that identifies it and allows the OS to manage it independently of other processes.
The operating system is responsible for managing processes and their resources, ensuring that they execute correctly and efficiently. When a program is executed, it is loaded into memory and becomes a process. And there is a layout when the proces is loaded into memory. The process is composed of several components, including the stack, heap, data, and code.
The stack is a region of memory used for storing temporary data, such as function arguments, return addresses, and local variables.
The heap is a region of memory used for dynamic memory allocation, where data can be allocated and deallocated at runtime.
The data section is a region of memory that contains static or global variables.
The code section is a region of memory that contains the executable code of the program.
2) Process Context
The process context is a collection of information that represents the state of the process at a particular time. It includes the program counter, which points to the current instruction being executed, and various registers that store the values of variables and flags used by the program. The address space of the process is also part of the context, which defines the virtual memory layout of the process.
Process Context | Content |
Hardware Context (CPU State) | CPU Register, Program Counter |
Operating system Context | PCB(Process Control Block) |
Memory Context | code, data, stack, heap |
In an operating system, the process context refers to the set of information that represents the current state of a process. The process context is essential for the operating system to manage and control the execution of processes.
There are several types of process context in an operating system, including:
- Hardware context: This includes the contents of the CPU registers, such as the program counter, stack pointer, and general-purpose registers. These registers are used by the CPU to execute the instructions of the process.
- Memory context: This includes the memory map of the process, which describes the virtual memory space that the process occupies. It includes the process code, data, heap, and stack.
- Operating system context: This includes the process control block (PCB), which is a data structure used by the operating system to store information about the process, such as its process ID, priority, state, and other related information.
- File system context: This includes information about the files and resources that the process has open, such as file descriptors or handles.
- Network context: This includes information about the network connections that the process has established, such as socket descriptors.
All of these types of process context are essential for the operating system to manage and control the execution of processes, and to ensure that processes do not interfere with each other or the system as a whole. The operating system uses the process context to manage the scheduling and synchronization of processes, to allocate resources to processes, and to enforce security and protection between processes.
3) PCB
The process control block (PCB) is another critical component of the process context. It is a kernel data structure that contains all the necessary information about a process, such as its process ID, parent process ID, priority, state, and resource usage. The PCB is used by the operating system to manage and schedule processes, track their execution, and switch between them when necessary.
1. Information used by the operating system for management
- Process state
- Process ID
- Scheduling information : Scheduling parameter information such as process importance, scheduling queue pointer, etc.
- Priority : Priority of the process
2. Hardware value related to CPU performance
- Program counter : Pointer pointing the address to execute
- Register : The CPU register value referenced by the process to continue the operation correctly after the interrupt.
3. Memory related
- Code, Data, Stack location, base/limit register values
4. File related
- open file descriptions : List of open files
4) Process State
1. New : Process first created state
2. Ready : Waiting for the process to be allocated to the CPU (meets all other conditions such as memory)
3. Running: Process assigned, holding CPU and performing commands
4. Waiting : The state in which the process waits for an event to occur. The state in which the command cannot be performed immediately even when the CPU is assigned.
5. Terminated: When the process has finished running. The process has not yet been completely removed.
6. Suspend ready :A process in the ready state, which is moved to secondary memory from the main memory due to lack of the resources (mainly primary memory) is called in the suspend ready state.
If the main memory is full and a higher priority process comes for the execution then the OS have to make the room for the process in the main memory by throwing the lower priority process out into the secondary memory. The suspend ready processes remain in the secondary memory until the main memory gets available.
5) Context Switching
Context switching is a technique used by modern operating systems to manage multiple processes or tasks. When a process is running on the CPU, it may be interrupted by an event, such as an I/O request or a timer interrupt. When this happens, the operating system needs to save the current state of the process, so it can later resume where it left off.
The operating system saves the state of the process on a data structure called a Process Control Block (PCB), which contains information such as the values of CPU registers, the program counter, and the stack pointer. The operating system then selects the next process to run, loads its PCB, and restores its state. This process is known as a context switch, because the operating system switches from the context of one process to the context of another process.
It is important to note that not all interrupts or system calls result in a context switch. A context switch only occurs when the processor is transferred from one process to another. For example, if a process makes a system call that blocks it until an I/O operation completes, the CPU may continue executing other processes while the blocked process is waiting. When the I/O operation completes, the operating system may unblock the process and resume its execution without performing a context switch.
In summary, a context switch is the process of saving the state of a running process, selecting a new process to run, and restoring its state. The state of a process is stored in a data structure called a PCB, and the operating system uses this information to manage multiple processes efficiently.
2. Thread
1) Thread
In computing, a thread is a lightweight execution unit that exists within a process. In other words, a thread is a subset of a process and it is responsible for executing a part of the process code. A single process can have multiple threads running concurrently, each performing a separate task.
The kernel of an operating system manages the threads that are present in a process. The threads share the resources of the process, such as the memory, file handles, and other system resources. This makes threads an efficient way to achieve parallelism within a single process.
While multiple threads can simultaneously access the code areas of a process without any problems, simultaneous access to writable data areas can cause issues with data integrity. To avoid such issues, threads need to synchronize their access to shared resources such as memory or files. Failure to properly synchronize access to shared resources can result in data inconsistencies, race conditions, and other problems.
However, the stack is a thread-specific resource, meaning each thread has its own stack. This means that threads can use their stacks without worrying about synchronization or interference from other threads.
In summary, a thread is an independent unit of execution that exists within a process. Multiple threads can exist within a single process and they share the resources of the process. While simultaneous access to code areas is not a problem, simultaneous access to writable data areas requires synchronization to ensure data integrity. The stack is a thread-specific resource and can be used by a thread without worrying about synchronization with other threads.
2) Race Condition
A race condition is a type of problem that can occur in computer systems, particularly in operating systems and multi-threaded applications.
A race condition occurs when two or more threads or processes access shared data or resources in an unpredictable order, and the final result depends on the exact timing and ordering of the operations. This means that the output of the program may differ each time it is run, even if the input and code are the same.
For example, consider two threads in a program that both try to update a shared variable x. If both threads try to modify the value of x at the same time, the final value of x will depend on which thread gets to modify it first. If thread A modifies x before thread B, then the final value of x will be different from the case where thread B modifies x before thread A.
Race conditions can lead to unpredictable and incorrect results, crashes, or other errors in a program. They can be particularly difficult to debug, because the behavior of the program may not be consistent and may depend on external factors such as system load or network latency.
To prevent race conditions, programs must use synchronization mechanisms to ensure that only one thread or process can access a shared resource at a time. These mechanisms include locks, semaphores, and other constructs that allow threads to coordinate their access to shared resources.
3) Semaphore
It prevents multiple processes from accessing data from shared resources. Semapore is also a simple counter that represents the state of a resource, which is typically used for resources that have a relatively long time, and is a technology that adjusts or synchronizes behavior in multiple processes that competitively use the operating system's resources. In other words, only one thread can be inserted or multiple threads can be inserted. This is the difference from mutex.
4) Mutex
It is a way to prevent multiple threads from accessing data from shared resources. In other words, it is a technology that allows the running time of threads with a Critical Section (a program code part that accesses shared data in each process) to be executed alone so that they do not overlap each other. Multiple processes use locking and unlocking to coordinate access to shared resources, which means that two threads cannot be used simultaneously by mutual exclusion.
5) TCB
TCB is a data structure that exists for each Thread, with a PC, a Register Set (CPU information), and a pointer pointing to a PCB. Typically, TCB becomes the basic unit of context switching at the kernel level, and only TCB information needs to be stored for switching in the same process.
3. Stack
A call stack is a data structure that stores information about the active subroutines (functions) in a program during its execution. When a function is called, a new stack frame is added to the call stack, which contains information about the function's local variables, parameters, and the return address. When the function completes, its stack frame is removed from the call stack, and control returns to the calling function.
An extended base pointer (EBP) is a register used in some architectures (such as x86) that provides additional information about the current stack frame. The EBP register points to the base of the previous stack frame, and it's used in conjunction with the stack pointer (SP) to access variables and parameters in the current and previous stack frames.
The EBP register is particularly useful when debugging programs because it provides a way to navigate the call stack and examine the variables and parameters of each function. By following the EBP chain from one stack frame to the next, a debugger can reconstruct the call history of a program and diagnose problems that occurred during its execution.
The call stack with an extended base pointer is a data structure that stores information about the active subroutines in a program and uses the EBP register to provide a fixed reference point for accessing the variables and parameters of each function's stack frame.
Here is a exmaple about EBP and ESP.
int foobar(int a, int b, int c)
{
int xx = a + 2;
int yy = b + 3;
int zz = c + 4;
int sum = xx + yy + zz;
return xx * yy * zz + sum;
}
int main()
{
return foobar(77, 88, 99);
}
4. Calling convention
1) Calling Convention
A calling convention is a set of rules that dictate how parameters are passed to a function, how the function's return value is returned, and how the function's local variables are stored and accessed in memory. Different computer architectures and programming languages use different calling conventions.
In general, a calling convention defines:
- How parameters are passed to a function: This can be done using registers or by pushing the parameters onto the stack.
- How the return value is returned: This can be done using a register or by placing the return value in a specific memory location.
- How the function's local variables are stored and accessed: This can be done using registers, a stack frame, or some combination of the two.
Different calling conventions have different advantages and disadvantages. Some calling conventions are designed for speed, while others prioritize ease of use or consistency across different platforms. For example, the cdecl calling convention, which is commonly used in C programming, passes parameters on the stack and expects the caller to clean up the stack after the function call. The fastcall calling convention, which is used in some Microsoft Windows environments, passes the first few arguments in registers for speed, but then uses the stack for additional arguments.
It is important to follow the calling convention when calling functions, as failing to do so can cause unexpected behavior, such as corrupted data or crashes. Additionally, calling conventions can impact performance, so choosing the right calling convention for a specific situation can be important for optimizing code.
2) Name Mangling
Name mangling is a technique used by compilers to give unique names to functions and variables in compiled code. This is done to avoid naming conflicts between different modules or libraries that might use the same names for their functions or variables.
In name mangling, the compiler takes the original name of a function or variable and transforms it into a new, unique name by adding prefixes or suffixes based on a set of rules. These rules can vary depending on the programming language, compiler, and operating system.
For example, in C++, when a class member function is compiled, the compiler will add a prefix to the function name that includes the class name and an underscore character. This makes it possible for the compiler to differentiate between the member functions of different classes that have the same name.
Name mangling is generally transparent to the programmer and happens automatically during the compilation process. However, it can make debugging and linking more difficult, as the names of functions and variables in the compiled code may not match the original names used in the source code. This is why many compilers provide options to disable or customize name mangling.
'Operating System' 카테고리의 다른 글
Interview Questions (0) | 2023.10.22 |
---|---|
Operating System (Memory Manager) (1) | 2023.04.17 |
Operating System (Hardware Initialization) (0) | 2023.04.11 |