*** Conditions and time issues, the program has not been tested yet.
UNIX concurrent technology learning and application on scanner
I have been learning to write a scanner. My direction is: Multi-process - Multi-Thread - Thread Safety (MT-SAFE). I want to implement on the scanner. Now I have learned a multifunction section.
Part 1 - Basic knowledge 1. Process concept:
The process defines a basic unit that performs a particular program. It has its own address space, performs stack, file descriptor, and so on.
II. Create a process:
1.Fork () function prototype #include
2. Return the value of the value of 0, the parent process is the child process ID, the error is -1.
3.Fork () function usage
Two usage: A parent process passes the different code segments by copying themselves simultaneously. This is a common on the network service process: the parent process waits for the customer's service request. When this request arrives, the parent process calls fork The function, the sub-process handles this request. The parent process continues to wait for the next customer service request. Each process is to perform a different program, which will shell is common, in which case the child process is from fork () The function returns immediately calls the exec () function to perform other programs.
Usage: #include
IF ((pID = fork ())> 0) {/ * parent process * /} else if (pid == 0) {/ * child process * / exit (0);} else {printf ("fork error / n "); EIXT (0);}}
4. The process terminates two possibilities - the parent process is finally terminated. The parent process of all sub-processes changes to the init process called the process by the init process.
- The child process is terminated in the parent process. The parent process calls the wait () function or the waitpid () function can get the process ID, the termination of the process of the child, and the total amount of CPU used in the process.
The process is normal or an abnormal termination, and the system kernel sends a SIGCHLD signal to its parent process.
5. Process Termination Call
1.Wait () function Wait for the child process to return and modify the status #include
2.WaitPID () function Wait to specify the child process returns and modify the status. # Include
Otherwise the behavior of the function is determined by the parameters PID and Options. PID specifies that the parent process requires that the status of those sub-process, where: = - 1 requires the return status of any child process;> 0 requires the status of the sub-process of the process number PID; <- 1 require aware of the process group number The status of the sub-process for the absolute value of the PID .Options parameter is a flag represented by bit mode, which is a bitmap that each flag is "or" operation, each flag is represented in one byte:
Wuntraced reports any sub-process state that is unknown but stopped running the specified process, the status of the process has not been reported from the time of stopping running.
WContinued reports any sub-process status of the specified process that continues to run, the status of the sub-process has not been reported from continuing to run.
When Wnonang is called, the child process status of the process is specified, and it is not immediately valid (ie, it can be read immediately), the calling process is not suspended.
WNOWAIT maintains the process that sets its status on the STAT_LOC () function, the process will not return the status value next time.
The return value is the sub-process number, otherwise it is -1, and the STAT_LOC () function returns the return value of the sub-process:
#include
The exit () function terminates the process and returns the status. #include
Part II - Multi-process 80banner scanner specific implementation
I. Idea:
1. The multi-IP processing module uses the strtok () function to separate IPSTRTOK (IP, "); for loop IP
2.usage prompt module int usage (char * pro) {Printf ("fork 80 banner scanner"); Printf ("Usage:% s [startip] [stopip] / n", pro);}
3. Scanning module Viod Scanip (char SIP [20])
4. Multi-process and processing module fork ()
II. Implementation
/ ************************************************** ******************** Fork () 80 banner scanner ver1.0 ** to complel: * user $ gcc -o 80scaner 80scanner.c ** to use: * User $. / 80scanner start_ip stop_ip (The Best Input C Class IP Otherwise * Waste Too Many System Resource) ** Coded by Nightcat * June 2003
*********************************************************** *************** * * 所 要 文件 * / # include
INT Main (int Argc, char * argv []) {/ * declared variable * / int CHILD_STATUS; PID_T child_id; pid_t pid; char * pro; int count
CHAR * START_IP, * STOP_IP;
Char * chge; char scan_ip [20];
INT IP1, IP2, IP3, IP4; INT END1, END2, END3, END4
/ * Input parameter judgment * / if (argc <3) {usage (argv [0]); exit (1);}
/ * Handling IP * / START_IP = Argv [1]; ChGE = STRTOK (START_IP, "); IP1 = ATOI (CHGE); ChGE = Strtok (NULL,". "); IP2 = ATOI (chGE); chGE = Strtok (NULL, "); IP3 = ATOI (ChGE); ChGE = Strtok (NULL,". "); IP4 = ATOI (ChGE);
STOP_IP = Argv [2]; chGE = STRTOK (STOP_IP, "); END1 = ATOI (ChGE); ChGE = Strtok (NULL,". "); end2 = atoi (chGE); chGE = STRTOK (NULL," "); end3 = atoi (chGE); chGE = STRTOK (null,". "); end4 = ATOI (chGE);
/ * Cycle scan * / for (count = IP4; count <= end4; count ) {spRINTF (scan_ip, "% d.% D.% D.% D", IP1, IP2, IP3, count); / * generation Process * / if ((pID = fork ()) == 0) {scanip (scan_ip); exit (0);}}}
/ * Usage function * / int usage ("fork () 80 banner scanner / n"); Printf ("INPUT C Class IP"); Printf ("Usage:% s [start_ip] [stop_ip_ip] [STOP_IP ] / n ", pro);
/ * Scan function * / void scanip (char SIP [20]) {struct socmedddr_in addr; int S; char buffer [1024]; if ((s = socket (AF_INET, SOCK_STREAM, 0) <0) {PERROR (" Socket error / n "); exit (1);} addr.sin_family = Af_inet; addr.sin_addr.s_addr = inet_addr (SIP); addr.sin_port = HTONS (80); if (Connect (S, Struct SockAddr *) & Addr, SizeOf (AddR)) <0) {Perror ("Connect Error"); exit (1);} Send (S, "HEAD / HTTP / 1.0 / N / N", 17, 0); RECV (S, Buffer, sizeof (buffer), 0); Printf ("% s's http version: / n% s", sip, buffer; close (s); SLEEP (5); exit (0);} II. Summary:
Debug problem:
1.SPrintf (scan_ip, "% d.% D.% D.% D", IP1, IP2, IP3, count); this statement prompt overflow, mainly the memory space allocated by SCAN_IP is not enough, by the original char * scan_IP change Solution to Char Scan_IP [20]
2.send (s, "head / http / 1.0 / n / n", 17, 0); this statement has not received information, as long as it is HTTP / 1.0 less, two carriages / N / N, Plus you can!
Among them, new students are: 1. Process method of IP 2. Multi-process method