In Unix programming, learning to use fork and signal is quite basic.
Fork () and signal are often used in daemon resident programs.
A4c. tty/yact/chdrv these Chinese terminal programs are also useful, such
Mozilla, Apache, squid, and other large programs are almost always used.
Although programming in UNIX does not require much thread functionality
Almost all jobs already exist. Pthread is the thread function library on Linux. If you
It is necessary to write a multi-threaded sequence in Linux, such as an MP3 player.
Both pthread and signal can be discussed in a major chapter. Here, I will only talk about the simplest and most commonly used technologies.
Coincidentally, when you are familiar with the use of these basic skills, you can find some in-depth discussions on pthread and signal.
Program writing books to study. There are few opportunities to use these advanced writing methods.
The learning speed should be faster.
Program divergence fork ()
Fork () generates a subroutine that is the same as the parent program. The only difference lies in its process.
ID (PID ).
If we want to write a daemon program, or for example, a Web server, we need to provide multiple routes at the same time.
Multiple connections, you can use fork () to generate multiple identical itineraries.
Function Declaration
Pid_t fork (void );
Pid_t vfork (void );
Return Value:
-1: failed.
0: subroutine.
> 0: return the process ID of the subroutine to the parent program.
In Linux, fork () and vfork () are the same.
Example 1: fork. c
In this example, we demonstrate the standard usage of Fork.
# Include <stdio. h> </P> <p> # include <stdlib. h> </P> <p> # include <unistd. h> </P> <p> void main (void) </P> <p >{</P> <p> pid_t PID; </P> <p> printf ("Hello/N"); </P> <p> pid = fork (); </P> <p> switch (PID) {</P> <p> case-1: printf ("failure! /N "); break; </P> <p> case 0: printf (" I am child! /N "); break; </P> <p> default: printf (" My child is % d/N ", pid); break; </P> <p >}</P> <p> (;;) {/* do something here */} </P> <p >}< br/>
Compile:
Gcc-O ex1 fork. c
Execution result:
./Ex1 &
Hello
My child is 8650
I am child!
We can see that using fork () can split a program into two. Code prior to divergence
Run only once.
Inspection itinerary:
PS | grep ex1
8649 P0 R./ex1
8650 P0 R./ex1
8649 is the PID of the parent program, and 8650 is the PID of the subroutine.
You will need to use "killall ex1" to kill two trips.
Example 2: Daemon. c
In UNIX, we generally use fork () to implement the so-called "daemon program", that is, in DOS.
The so-called "resident program ". The general technique is to end the parent program, and the subroutine becomes the "patron saint ".
In this example, the standard daemon statement is used.
# Include <stdio. h> </P> <p> # include <stdlib. h> </P> <p> # include <unistd. h> </P> <p> void main (void) </P> <p >{</P> <p> pid_t PID; </P> <p> pid = fork (); </P> <p> If (pid> 0) {</P> <p> printf ("daemon on duty! /N "); </P> <p> exit (0); </P> <p >}else </P> <p> If (PID <0) {</P> <p> printf ("can't fork! /N "); </P> <p> exit (-1); </P> <p >}</P> <p> (;;) {</P> <p> printf ("I am the daemon! /N "); </P> <p> sleep (3 ); </P> <p>/* Do Something your own here */</P> <p >}</P> <p>
Compile:
Gcc-O ex2 daemon. c
Execution result:
./Ex2
Daemon on duty!
I am the daemon!
In the next three seconds, an "I am the daemon! "Message, which indicates your program
"Long resident" is already in the system.
Inspection itinerary:
PS | grep ex2
8753 P0 S./ex2
Note that in Example 1, the command we run is "./ex1 &", but in example 2 is "./ex2", no
It has the "&" symbol.
Example 3: Lock. c
In many cases, we hope that there is only one "patron saint" in the system, and PID will be used at this time.
Lock skills. If you notice the content in the/var/run directory, you will find a lot of *. PID
The video content is all numbers, which are actually the PID of the trip.
# Include <stdio. h> </P> <p> # include <stdlib. h> </P> <p> # include <unistd. h> </P> <p> void main (void) </P> <p >{</P> <p> file * FP; </P> <p> pid_t PID; </P> <p> exit (-1); </P> <p >}</P> <p> act. sa_handler = quit; </P> <p> act. sa_flags = 0; </P> <p> sigemptyset (& act. sa_mask); </P> <p> sigaction (sigterm, & act, null); </P> <p> sigaction (sighup, & act, null ); </P> <p> sigaction (SIGINT, & act, null); </P> <p> sigaction (sigquit, & act, null ); </P> <p> sigaction (SIGUSR1, & act, null); </P> <p> sigaction (sigusr2, & act, null ); </P> <p> for (;) {</P> <p> sleep (3 ); </P> <p >}< br/>
Compile:
Gcc-O ex1 lock. c
Run
./Ex1
Daemon on duty!
Send signal
First, we need to find the PID of the daemon program.
PID = 'cat/var/run/lock. PID'
Next, use kill to send signals.
Kill $ PID
Receive signal 15
The program will end and/var/run/lock. PID will be deleted for the next daemon restart
. Note that if exit () is not put in the quit function, the program will never be killed.
Next, let's try some other signals.
./Ex1
PID = 'cat/var/run/lock. PID'
Kill-HUP $ PID
Receive signal 1
You can try it on your own
Kill-int $ PID
Kill-Quit $ PID
Kill-ill $ PID
.
.
.
Wait for these signals to see what their results are.
Signal Definition
Various signal definitions are available in/usr/include/SIGNUM. h.
# Define sighup 1/* hangup (POSIX ).*/
# Define SIGINT 2/* interrupt (ANSI ).*/
# Define sigquit 3/* Quit (POSIX ).*/
# Define sigill 4/* illegal instruction (ANSI ).*/
# Define sigtrap 5/* trace trap (POSIX ).*/
# Define SIGABRT 6/* abort (ANSI ).*/
# Define sigiot 6/* Iot trap (4.2 BSD ).*/
# Define sigbus 7/* Bus Error (4.2 BSD ).*/
# Define sigfpe 8/* floating-point exception (ANSI ).
*/
# Define sigkill 9/* Kill, unblockable (POSIX ).*/
# Define SIGUSR1 10/* User-Defined signal 1 (POSIX ).*/
# Define SIGSEGV 11/* segmentation violation (ANSI ).*/
# Define sigusr2 12/* User-Defined Signal 2 (POSIX ).*/
# Define sigpipe 13/* broken pipe (POSIX ).*/
# Define sigalrm 14/* Alarm Clock (POSIX ).*/
# Define sigterm 15/* termination (ANSI ).*/
# Define sigstkflt 16 /*??? */
# Define sigcld sigchld/* same as sigchld (System V ).*/
# Define sigchld 17/* child status has changed (POSIX ).
*/
# Define sigcont 18/* continue (POSIX ).*/
# Define sigstop 19/* Stop, unblockable (POSIX ).*/
# Define sigtstp 20/* keyboard stop (POSIX ).*/
# Define sigttin 21/* Background read from tty (POSIX ).
*/
# Define sigttou 22/* Background write to TTY (POSIX ).
*/
# Define sigurg 23/* urgent condition on socket (4.2
BSD ).*/
# Define sigxcpu 24/* CPU limit exceeded (4.2 BSD ).*/
# Define sigxfsz 25/* file size limit exceeded (4.2
BSD ).*/
# Define sigvtalrm 26/* virtual alarm clock (4.2 BSD ).*/
# Define sigprof 27/* profiling alarm clock (4.2 BSD ).
*/
# Define sigwinch 28/* window size change (4.3 BSD, Sun ).
*/
# Define sigpoll sigio/* pollable event occurred (System
V ).*/
# Define sigio 29/* I/O now possible (4.2 BSD ).*/
# Define sigpwr 30/* power failure restart (System V ).
*/
# Define sigunused 31
Function declaration:
Signal Operators
Int sigemptyset (sigset_t * Set );
Int sigfillset (sigset_t * Set );
Int sigaddset (sigset_t * Set, int SIGNUM );
Int sigdelset (sigset_t * Set, int SIGNUM );
Int sigismember (const sigset_t * Set, int SIGNUM );
Signal Handling Functions
Int sigaction (int signum, const struct sigaction * Act, struct
Sigaction * oldact );
Int sigprocmask (INT how, const sigset_t * Set, sigset_t
* Oldset );
Int sigpending (sigset_t * Set );
Int sigsuspend (const sigset_t * mask );
Structure signal action
Struct sigaction {
Void (* sa_handler) (INT );
Sigset_t sa_mask;
Int sa_flags;
Void (* sa_restorer) (void );
}
OK station, webmaster, Brian Lin
From: http://docs.huihoo.com/gnu/linux1/tutorial4.html