[Software Designer] Chapter 4 Operating System Knowledge


1. Overview of the operating system

Can effectively organize and manage various software and hardware resources in the system, reasonably organize the computer system workflow, control the execution of the program, and provide users with a good working environment and friendly interface

1.1 The role of the operating system

  • Management system hardware, software (API interface), data resources
  • Control program running
  • interface between man and machine
  • Direct interface between application software and hardware

1.2 Features and functions of the operating system

  • process management
    • Process state, predecessor graph, PV operation, deadlock problem
  • storage management
    • Segment page storage, page replacement algorithm
  • file management
  • index file, bitmap
  • job management
  • device management
  • microkernel operating system
    • Virtual Device and SPOOLING Technology

2. Process management

insert image description here

insert image description here

2.1 Precursor graph

Precursor diagram is a tool commonly used in process modeling and analysis. It graphically displays information such as the preconditions, process, and subsequent impact of an event or task, helping people better understand and master complex processes. process relationship. The use of forward trend diagrams can help us better understand the relationship between tasks, optimize process efficiency, reduce risks, and improve work quality. At the same time, the forward trend map can also be used in project management, production planning, resource allocation, risk assessment, etc., and is a very practical tool.

A predecessor graph usually consists of nodes and arrows, where a node represents a task or event, and an arrow represents the relationship between them. In predecessor diagrams, arrows are generally divided into three types:

  • Solid arrows indicate task dependencies
  • Dashed arrows indicate alternative paths to tasks
  • Double-headed arrows indicate interdependencies between tasks.

When drawing a predecessor map, we need to first determine the order and dependencies of the tasks, and label the name and duration of each task. Then, according to the dependencies and optional paths between tasks, draw nodes and arrows, and add corresponding text descriptions, so that readers can better understand and use.

2.2 Process synchronization and mutual exclusion

  • Process synchronization
    Process synchronization refers to the execution of multiple processes in a certain order to avoid unpredictable results. The implementation methods of process synchronization include mutual exclusion, semaphore, monitor and so on. Among them, mutual exclusion is the most common and simple implementation.
  • Mutual exclusion of processes Mutual
    exclusion means that when multiple processes access shared resources at the same time, the resources need to be locked and unlocked to ensure that only one process can access shared resources at the same time, thereby avoiding competition and conflicts. Commonly used mutual exclusion methods include critical sections, mutexes, read-write locks, etc.
  • The implementation of process mutual exclusion needs to meet the following conditions:
    • The mutex is binary, that is, there are only two states of "locked" and "unlocked".
    • Once a process acquires the mutex, other processes must wait for it to release the lock before acquiring the lock and entering the critical section.
    • Only one process at a time can enter a critical section and perform access to a shared resource.

2.3 PV operation (semaphore operation)

three concepts

  • Critical resources: resources that need to be shared between processes in a mutually exclusive manner, such as printers, tape drives, etc.
  • Critical section: The section of code that accesses critical resources in each process is called a critical section
  • Semaphore: is a special variable

insert image description here
[Example 1] Suppose there are two processes P1 and P2, they need to share a resource, but the resource can only be accessed by one process at the same time. To avoid race conditions and deadlocks, PV operations can be used. In the following code snippet, two processes P1 and P2 are implemented and shared resources are used. Among them, semaphore is a global variable with an initial value of 1. Please supplement the PV operation function in the code snippet to ensure that resources are shared correctly.

#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>

sem_t semaphore;

void *process_one(void *arg)
{
    
    
    printf("P1 is waiting to access the resource.\n");
    // PV操作
    printf("P1 is accessing the resource.\n");
    sleep(2);
    printf("P1 is releasing the resource.\n");
    // PV操作
    pthread_exit(NULL);
}

void *process_two(void *arg)
{
    
    
    printf("P2 is waiting to access the resource.\n");
    // PV操作
    printf("P2 is accessing the resource.\n");
    sleep(1);
    printf("P2 is releasing the resource.\n");
    // PV操作
    pthread_exit(NULL);
}

int main()
{
    
    
    pthread_t t1, t2;
    sem_init(&semaphore, 0, 1);

    pthread_create(&t1, NULL, process_one, NULL);
    pthread_create(&t2, NULL, process_two, NULL);

    pthread_join(t1, NULL);
    pthread_join(t2, NULL);

    sem_destroy(&semaphore);

    return 0;
}

【Answer】

void P(sem_t *sem)
{
    
    
    sem_wait(sem);
}

void V(sem_t *sem)
{
    
    
    sem_post(sem);
}

[Analysis]
The P function in the PV operation function is used to obtain the (sem_wait) semaphore, and the V function is used to release the (sem_post) semaphore, thereby realizing mutually exclusive access to shared resources. In the main function, use sem_init to initialize the semaphore, and use the P and V functions in each process to acquire and release the semaphore. Finally, by using the pthread_join function to wait for the end of the two threads, it is ensured that the shared resources are shared correctly.

【Example 2】
insert image description here

[Solution] (1) A (2) C

insert image description here

[Example 3]
insert image description here
[Answer] (1) C (2) A (3) A

insert image description here

2.3 Deadlock problem (p138)

Definition of deadlock: A group of processes is deadlocked if each process in the group is waiting for an event to occur that can only be raised by other processes in the group

Process management is the core of the operating system, but if it is not designed properly, there will be deadlock problems. If a process is waiting for an impossible event, the process is deadlocked. And if one or more processes generate deadlock, it will cause system deadlock

[Example]
The system has 3 processes: A, B, and C. All 3 processes require 5 system resources. Deadlock is impossible if the system has at least that many resources.
【answer】

K*(n-1)+1

It is concluded that at least 3*(5-1)+1=13 resources are required

Four conditions of deadlock

  • mutually exclusive condition
  • Request and Hold Conditions
  • non-preemptive condition
  • loop wait condition

How to deal with deadlock

  • deadlock prevention
  • avoid deadlock
  • Detect deadlock
  • remove deadlock

2.4 Banker's Algorithm

The Banker's Algorithm: Principles for Allocating Resources

  • A process can be admitted when its maximum demand for resources does not exceed the number of resources in the system
  • Processes can request resources in installments, but the total number of requests cannot exceed the maximum demand
  • When the existing resources of the system cannot meet the number of resources still required by the process, the request for the process can be postponed, but the process can always get the resources within a limited time
    [example]

    Assume that there are three types of mutually exclusive resources R1, R2, and R3 in the system, and the available resources are 9, 8, and 5 respectively. At time T0, there are five processes in the system: P1, P3, P4, and P5. The maximum demand for resources by these processes and the number of allocated resources are shown below. If the processes are executed in _____ sequence, the state of the devil system is safe.

insert image description here
Alternative answers:
A. P1->P2->P4->P5->P3
B. P2->P4->P5->P1->P3
C. P2->P1->P4->P5->P3
D. P4->P2->P4->P1->P3

3. Storage management

3.1 Partition storage management (P204)

insert image description here

3.2 Page Storage Organization

Divide the user program into equally sized pages
insert image description here

Advantages and disadvantages of page storage organization

  • High memory utilization, small fragmentation, easy allocation and management
  • Increased system overhead; may cause jitter
    insert image description here
    【Solution】: D, B
    insert image description here

3.3 Segment storage organization

  • Advantages: Multiple programs share memory, and the modification of each program does not affect each other
  • Disadvantages: low memory utilization, large waste of memory fragmentation
    insert image description here

3.4 Segment page storage organization

  • Advantages: small waste of space, easy storage sharing, easy storage protection, and dynamic linking
  • Disadvantages: Due to the increase of management software, the complexity and overhead also increase, the required hardware and the occupied content also increase, which greatly reduces the execution speed

3.5 Fast watch

The fast table is a small-capacity associative memory (Associative Memory), which is composed of high-speed caches. It is fast and can guarantee parallel search by content from the hardware. It is generally used to store the page numbers of the few active pages that are currently most frequently accessed. .

3.6 Page Replacement Algorithm

  • First in first out algorithm (FIFO)
  • Will produce jitter
    insert image description here
    insert image description here
    【Example】
    insert image description here
    【Solution】
    insert image description here

4 File Management

4.1 Index file structure

Index file structure example
[Example]
insert image description here
[Solution] 5-6 are all first-level indirect address references, and the byte occupied by the address is divided by the disk block size to get 1kb/4
Index File Structure Sample Questions and Answers

4.2 File and tree directory structure

insert image description here

Absolute path: the path starting from the drive letter
Relative path: the path starting from the current path
If the current path is: D1, and F2 path is required, then: absolute path: /D1/W2/F2, relative path: W2/F2

  • file attributes
    • R read-only file attribute
    • A archive attribute
    • S system file
    • Hhidden file
  • The composition of the file name
    • drive letter
    • path
    • main file name
    • extension name

4.3 Management of free storage space

  • Free area table method
  • free list method
  • bitmap
  • group chaining

【Example of Bitmap】
insert image description here
【Solution】
insert image description here

5. Equipment management

5.1 Data transmission control method

Detailed summary of data transmission control methods
insert image description here

5.2 Virtual device and SPOOLING technology

insert image description here
SPOOLING (Simultaneous Peripheral Operations On-line) technology is a technology that implements input and output buffering in computer systems. It can manage input and output tasks in a queue, thereby improving the concurrency and efficiency of the system.

The main principle of SPOOLING technology is to put input and output tasks into a queue for buffering, and a dedicated process is responsible for management and scheduling. When the input and output device is ready, the process will get the task from the queue and process it, so as to realize the input and output operation.

The advantage of SPOOLING technology is that the input and output tasks can be separated from the host processing tasks, so as to realize concurrent processing and task scheduling. It can make full use of the resources of the computer system and improve the efficiency and reliability of the system. In addition, SPOOLING technology can also implement functions such as sorting and pagination of printouts, thereby improving the quality and readability of output.

SPOOLING technology has a wide range of applications, such as printouts, disk storage, and network communications. In practical applications, SPOOLING technology is usually used in combination with virtual devices, such as using virtual printers to implement buffering and scheduling of printouts, thereby improving the efficiency and quality of printouts.

6. Microkernel Operating System

insert image description here

substance advantage shortcoming
monolithic core All functions such as graphics, device drivers, and file systems are implemented in the kernel, running in the kernel state and the same address space Reduce the system overhead of inter-process communication and state switching, and obtain higher operating efficiency The kernel is huge, takes up more resources and is not easy to cut. The stability and security of the system is not high
microkernel Only realize the basic functions, put the graphics system, file system, device driver and communication functions outside the kernel The core is refined, easy to cut and transplant. The system server program runs in the user address space, and the system has high reliability, stability and security. Can be used in distributed systems User state and kernel state need to be switched frequently, resulting in a system that is not as efficient as a single kernel

Guess you like

Origin blog.csdn.net/qq_54351538/article/details/129214348