minix3.1.8 Source code Interpretation (a)

Source: Internet
Author: User
Tags valid

In the sophomore study of the "operating system" course, the curriculum is mainly based on the Linux system as a case, At that time, the teacher recommended us to read the source code of minix3.1.8, Minix3 is a micro-core architecture-based UNIX-like computer operating system, sophisticated and fully open source, for the operating system of the novice is easier to understand. The following is I in the course of the source code reading notes, if there are also want to interpret minix3.1.8 source codes of friends, recommend a read code software source Insight, using its tree structure can be very clear tracking function and variable definition.


First, the start of Minix3:

L Hardware Read-in bootloader, bootloader loading boot,boot in memory mount boot image (including kernel, PM, FS, etc.)

l kernel Initialization (/KERNEL/MAIN.C):

1. Initialize the process table and the privileged process table:

Whether the state is set to idle, separate tasks or other processes setting P_NR values, maps priv[i] to ppriv_addr[i], etc.

2. Initialize the program in the boot image, copy the process name, process pointer, etc. to the proc table, and set their privileges (whether to allow traps, whether to run kernel call, etc.), and if the kernel task or root system process can be dispatched immediately, other processes cannot be dispatched;

3. Initialize the stack, set up the memory map, initialize the value of the Register

4, initialize the server stack pointer, will proc_ptr point to the current process, queued, set to a scheduled state

5, System task systems get cpu,system after initialization into blocking state

6, the clock task obtains the CPU, it initializes after attempts to accept the clock interrupt program sends the message and blocks

7. Other drivers and server processes get CPU and initialize

8, the CPU finally handed over to the first ordinary user process init

L INIT executes the/ETC/RC script, launches other drives and servers that are not in the boot image (services, etc.), and the RC script checks for the last time it shuts down properly

L INIT creates sub-process for terminal according to/etc/ttytab file, executes/usr/bin/getty waits for user login, executes shell after successful login, waits for user command

Second, the realization of the message mechanism: (/KERNEL/PROC.C)

L construct Notify (macro definition Buildnotifymessage):

1. Change the message type to notify

2. Call the Get_uptime function to read the clock ticks to let the notification contain timestamps

3. Judge the initiator of the message, if it is hardware, the interrupt bitmap of the target process is set, and if it is system, the signal bitmap of the target process is placed

L Message Delivery (DO_IPC)

1. Tracking process

2. Synchronous message Passing calls DO_SYNC_IPC

Asynchronous Message Delivery Call Mini_senda

L Synchronous Message Delivery (DO_SYNC_IPC)

1, check all kinds of errors: only the system call number is less than 32 to allow the call, only to receive information to set the destination process arbitrary, the specified source and destination process is valid, whether the initiating process is allowed to send messages to the destination process, whether the process has the privilege of making a call

2. Calling functions based on the type of message

Sendrec: Set flag bit off interrupt, continue calling send and receive

Send: Call Mini_send to determine whether to complete the send

RECEIVE: Clears the IPC status bit, calls Mini_receive

NOTIFY: Call Mini_notify

SENDNB: Call Mini_send, not blocked when not ready

Other: Error

L Concrete Implementation function:

1, Mini_send:

Check whether the destination process is blocked by waiting for this message (the waiting object is this send process or any), copy the message, the receiving process cancels the block by resetting the receiving bit, and the receiving process is queued to allow it to run, if the receiving process is not waiting for this message to check for deadlocks, Block the sending process from the end of the waiting send queue to the receiving process

2, Mini_receive:

Check if there is a process waiting in the queue waiting to send a message, if there is a destination process to look for and receive messages, will have completed the purpose of receiving the message process out of the queue, otherwise, if the queues are empty check the bitmap to determine whether there is a pending notification, if it is found that the process is waiting for the notification, it is marked as , return OK, if not found, check the mf_asynmsg bit to see if there is a pending senda, if there is an attempt to receive asynchronously, if no appropriate message is found, then check the deadlock again, plug the process

3, Mini_notify:

Similar to Mini_send, if the recipient of the notification is blocked and waiting to receive information, the message is constructed and the receiver leaves the waiting queue, and if the receiver does not wait for the message, the bitmap is set to suspend the signal and return

4, Mini_senda:

Clear the table, reset the header address and the number of parameters in the table, check the number to be sent, the entry is not empty and the kernel has not processed the bits to read, check the various errors may be, check whether the receiver is waiting for this message, if it is sent, if not, notify the recipient. If the recipient does not receive the message immediately, the header address and the number of parameters are logged

Third, process scheduling

l header file defined in (/servers/sched/schedproc.h)

The struct includes the endpoint of the process, the flag bit, the highest allowable privilege level, the current privilege level, and the time slice length

L Initialize and receive messages (/SERVERS/SCHED/MAIN.C)

1. Start and initialize locally

2, waiting for the next message, if it is a notification to determine whether the message sender is a clock, is to check if the timestamp has expired, do not reply; if it is not a clock, the reply to the sender "function is not implemented"

3. Send a message request operation to the SCHED server

l specific scheduling (/SERVERS/SCHED/SCHEDULE.C)

1, Do_noquantum:

When the time slice is exhausted, if the priority of this process is higher than the lowest priority of the normal process, the process is degraded by the internal

The kernel hangs the process at the end of the queue

2, Do_stop_scheduling:

Receive all messages from PM and RS, set the flag bit to indicate that the slot of the schedule is stopped using

3, Do_start_scheduling:

Receive all messages from PM and RS, when the port is valid, set the SCHEDPROC endpoint of the process according to the message, check the type of Schedproc, if it is just generated, set the priority to the highest allowable priority, allocate the full time slice, if it is inherited by parents, The set priority and the time slice length are consistent with the parent process. The SET flag indicates that the Schedproc slot is in use. Takes over the scheduling process, populating the process's current priority and time slices with a kernel call reply message. Allocate time slices with system calls. Mark yourself as a new dispatch.

4, Do_nice:

Keep the old information of the requesting process (including the old priority, the old highest priority), set the new priority to the highest priority, put it back in the queue by the kernel, and restore the old value if an error occurs during the hang back

L Timing Upgrade (balance_queues)

Set the timing to increase the priority level of the process that runs the time slice every 100ticks

Four, clock service

L Start Timer interrupt processing (Bsp_timer_int_handler):

1. Obtain the current tick number (Lost_ticks is the number of ticks for the external record when the clock_task is not executed, to add it to the present time), update time

2. Call Interrupt handler function

3, if the next timeout time has arrived, to prove that the timer is obsolete, to notify the clock task

L GET the number of Beats (get_uptime):

Returns the number of ticks that have elapsed since the system started

L new Design-time device (Set_timer):

1, insert the new timer structure into the clock_timers queue, note that this queue is based on the overflow time from small to large arrangement

2. Set a new time for next timeout with Clock_timers->tmr_exp_time

L Delete Timer (Reset_timer):

1. Remove the timers that are no longer needed from the active and expired timer queue, and update the time of the next timeout

2, if the timer queue is empty, then set the next timeout time is never

l Update Load (load_update):

1. Queue up the prepared process and calculate the average load

2, update the structure of the time

L Interrupt handler function (Ap_timer_int_handler):

1, if the process can be billed to update its system time and the number of remaining Beats

2, decrements the virtual timer, if applicable, decrements the current process of virtual and real timers

3. If the timer expires, send a signal

4, update the average load


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.