Newlisp provides a simple method for me to create multiple processes. The following program creates 10 processes, each of which sends several 'A' and finally sends a 'q '.
(define (quit-for-error) ((println (net-error)) (exit)))(define (send-test) (set 'socket (net-connect "localhost" 8888)) (if (net-send socket "a") (println "send a successfully") (quit-for-error)) (if (net-send socket "a") (println "send a successfully") (quit-for-error)) (if (net-send socket "a") (println "send a successfully") (quit-for-error)) (if (net-send socket "a") (println "send a successfully") (quit-for-error)) (if socket (println (net-send socket "q")) (quit-for-error)) (exit))(spawn 'r1 (send-test))(spawn 'r2 (send-test))(spawn 'r3 (send-test))(spawn 'r4 (send-test))(spawn 'r5 (send-test))(spawn 'r6 (send-test))(spawn 'r7 (send-test))(spawn 'r8 (send-test))(spawn 'r9 (send-test))(spawn 'r10 (send-test))(until (sync 1000))(exit)
Spawn creates a sub-process to execute the send-test function.
A total of 10 processes have been created.
Until is a wait until all sub-processes exit.
Run the test program now:
newlisp ./parallel_text.lspsend a successfullysend a successfullysend a successfullysend a successfullysend a successfullysend a successfullysend a successfully1send a successfullysend a successfully1send a successfullysend a successfullysend a successfully1send a successfullysend a successfullysend a successfullysend a successfully1send a successfullysend a successfullysend a successfullysend a successfully1send a successfullysend a successfullysend a successfullysend a successfully1send a successfullysend a successfullysend a successfullysend a successfully1send a successfullysend a successfullysend a successfullysend a successfully1send a successfullysend a successfullysend a successfullysend a successfully1send a successfullysend a successfullysend a successfullysend a successfully1
The service program is shown as follows:
./cppapplication_4 the number of connections is: 1the new connection object is starting now.correct data receivedthe number of connections is: 2the new connection object is starting now.correct data receivedthe number of connections is: 3the new connection object is starting now.correct data receivedcorrect data receivedcorrect data receivedcorrect data receivedcorrect data receivedcorrect data receivedwrong data received, char is:113closing the socket~Connectioncorrect data receivedwrong data received, char is:113closing the socket~Connectioncorrect data receivedcorrect data receivedcorrect data receivedwrong data received, char is:113closing the socket~Connectionthe number of connections is: 1the new connection object is starting now.correct data receivedcorrect data receivedcorrect data receivedcorrect data receivedwrong data received, char is:113closing the socket~Connectionthe number of connections is: 1the new connection object is starting now.correct data receivedcorrect data receivedcorrect data receivedcorrect data receivedwrong data received, char is:113closing the socket~Connectionthe number of connections is: 1the new connection object is starting now.correct data receivedcorrect data receivedcorrect data receivedcorrect data receivedwrong data received, char is:113closing the socket~Connectionthe number of connections is: 1the new connection object is starting now.correct data receivedcorrect data receivedcorrect data receivedcorrect data receivedwrong data received, char is:113closing the socket~Connectionthe number of connections is: 1the new connection object is starting now.the number of connections is: 2the new connection object is starting now.correct data receivedcorrect data receivedcorrect data receivedcorrect data receivedwrong data received, char is:113closing the socket~Connectioncorrect data receivedcorrect data receivedthe number of connections is: 2the new connection object is starting now.correct data receivedcorrect data receivedcorrect data receivedcorrect data receivedwrong data received, char is:113closing the socket~Connectioncorrect data receivedcorrect data receivedwrong data received, char is:113closing the socket~Connection
After careful check, 10 connections are created and 10 connections are closed. The program is normal. Okay. Now we want to change the server from a single thread to a multi-threaded model.