Linux master program

  

6.1, the overall idea of ​​the Linux program

The init process is the first process initiated by the Linux kernel, it is the initiator of all subsequent processes. The init process of this system is a link to the rc process, so the rc process is the main process, which is the program that runs into the kernel first. Initially, other child processes are started in the main process program. Suspend the wait process signal.

The child process does something to process or start or shut down other child processes by signaling the main process, the main process receives the signal. In addition, some processes communicate through socket sockets.

For some processes, the program runs once and exits. Some processes are programs that run continuously.

6.1, rc program

The main sequence is named rc, init, ip-up, ip-down, dtu-dns, start_watchdog, start-lan, stop-lan, start_timer, etc. Processes are connections to rc. When running these processes, the rc process determines which handlers to execute based on the process parameters.

If you run the init indicator, you will enter the RC main loop.

6.3, initialization runtime environment

The main loop includes the following parts of the initialization:

1, system initialization

mount system related directory

Generation of all device nodes

Mounting flash partition related directories

Creating links to parameter files

Creating related directories

Device parameter initialization

Console Initialization

Kernel Module Load

Time Zone Settings, System Time and Hardware Synchronization

Built-in Hardware Watchdog Startup

External hardware watchdog startup

2, GPIO initialization

Turn off the relevant indicator, turn on the module power, turn off the power of the sub-board

Turn off the unused GPIO

Turning on the power of the daughter board

Initializing each module

3. Turning on the LAN

4. Initializing the signal

Initializing the signal

Registration Signal

5, Configuration File Initialization

PPP Dialing Related Configuration File

DTU Parameters Configuration File

Network Controller Parameter Configuration File

Phone or SMS Trigger Configuration File

ICMP Detection Configuration File

DNS Detection Configuration File

DDNS parameter configuration file

Maintenance channel parameter configuration file

6.4, running various processes

1. Open TCP local server

Figure:

Figure 6.2 Local server process flow chart

2, open various services

Httpd server open

DTU client connection open < Br>

Dhcpd service is turned on

Telnet service is turned on

Monitoring the number of duplicate processes is turned on

Traffic statistics are turned on

6.5, ensuring that the PPPD process is correct Run

1. Before running pppd, first determine whether the module interface can be opened normally. If the exception is turned on, restart the system.

2. After running pppd, judge whether to generate /tmp/ppp/link. file every 1 second, and re-dial if it is not generated within the timeout period.

3, before re-run pppd to reset the module, disconnect the pppd connection to avoid the last run pppd did not exit.

4, pppd dial-up successfully open the network test function, to avoid pppd itself can not detect the drop.

6.6, run to loop processing

After the main program is initialized, the related service starts to enter the main loop processing, the main loop starts to dial the line and after the online processing, and finally idle Wait for an event signal. The main loop mainly monitors whether there is an abnormality in the PPPD and the WAN. If an abnormality is found, the corresponding processing is performed. The processing flow is as shown in the following figure:

Figure 6.3 Main Process Main Loop Flowchart

As above As you can see from the flowchart, the main loop is a state machine. When a process sends a signal, the signal processing function changes the state value, the main loop jumps out of the IDLE state, to the RESTART, or STOP state, loops again, and finally stabilizes to the idle state. It is common to run the ip-down process when the PPPD goes offline. The process sends a signal to the main process, and the main process goes to the RESTART state. The main program re-runs the dialing process.

6.7. Use of Signals

Signals are events that are generated by UNIX and Linux systems in response to certain conditions. The process that receives the signal takes some action accordingly. Usually the signal is generated by an error. But they can also be explicitly sent from one process to another as a way to communicate or modify behavior between processes. The generation of a signal is called generation, and a signal is received called capture.

Need to include the header file: #include <signal.h>

The use of the signal includes the following steps:

1. Register a signal.

For example: signal(SIGHUP, rc_signal);

2. Signal processing function.

static void rc_signal(int sig)

{

……

if (sig == SIGHUP) {

Syslog(6,"signalling RESTART\ ");

signalled = RESTART;

}

……

}

3. Send signal

Signal the main process: kill(1, SIGHUP);

Copyright © Windows knowledge All Rights Reserved