程序必须提前设计, 没有人可以凭空编码出复杂的系统

Overview: Mental Map

Let’s Start from Computer, What is Computer?

a complex system consist with different components.

most Important component called CPU (Central process Unit)

image.png

SO… What’s job of CPU? The Job of CPU is to executing programs.

Program, we should consider a Program as list of instruction, we ask machine to acting like provided. the list is called Program.

However, Those actions need to be unambiguous, and a computer is built to carry out those instructions. the unambiguous is called machine language

image.png

ok, machine language looks terrible, we don’t need to understand every digits means for now.

A computer executes machine language programs mechanically, without thinking or understand it’s meaning. How computer execute instruction without understand?

A computer is a machine built of millions of tiny switches called transistors, which have the property that they can be wired together in such a way that an output from one switch can turn another switch on or off.

image.png

and instructions can guide switch on and off

Those instructions store in main Storage (RAM), Memory or main storage is a consecutive space, each position have 8 bit of space which called as 1 byte when loading. and the location of instruction called address.

CPU will loaded form memory stack mechanically.

inside CPU have some storage as well which called registry, and CPU use Program Counter(PC) to indicate future instruction. pc store next address of instruction

image.png

get instruction, execute it and get next again. That’s CPU ever does.

The CPU spends almost all of its time fetching instructions from memory and executing them.

However Computer contained more than CPU and Memory, keyboard and other device can make input, How does CPU communicate with them?

image.png

Different input/output device called IO instead, define a IO controller to communicate with CPU, Uses wired to trans outside data.

image.png

OK, IO device can input now, but how CPU know data is there?

CPU can keep checking for incoming data over and over, This method is called polling

very inefficient. The CPU can waste an awful lot of time just waiting for input. To avoid this inefficiency, interrupts are generally used instead of polling.

An interrupt is a signal sent by another device to the CPU. The CPU responds to an interrupt signal by putting aside whatever it is doing in order to respond to the interrupt.

CPU will store current state and change focus to interrupt method

Interrupts allow the CPU to deal with asynchronous events. In the regular fetch-and execute cycle, things happen in a predetermined order, but interrupted can happen any time, That’s called asynchronous events, some things you can’s predict

Back to IO, When the CPU needs data from the disk, it sends a signal to the disk drive telling it to locate the data and get it ready.

Then, instead of just waiting the long and unpredictable amount of time that the disk drive will take to do this, the CPU goes on with some other task.

When the disk drive has the data ready, it sends an interrupt signal to the CPU. The interrupt handler can then read the requested data.

          +----------------------+
          |        CPU           |
          | (Fetch & Execute     |
          |   Cycle Continues)   |
          +----------+-----------+
                     |
       Sends I/O Request (e.g., read from disk)
                     v
          +----------------------+
          |     Disk Drive       |
          | (Locate & Read Data) |
          +----------+-----------+
                     |
              Data is Ready
                     |
        (Interrupt Signal Sent)
                     v
          +----------------------+
          |   CPU Interrupt      |
          |     Handler          |
          | (Reads the Data)     |
          +----------------------+

Now Let’s change point of view, in Java Thread has same meaning as process,

in certain time only one Thread can execute and use up its allotted slice of time and be suspended to allow other threads to run.

when IO is needed , the thread will suspended