[C / C ++] between C ++ base _3_ process, signals, interprocess communication, / multi-threading, thread synchronization


1. process: ps, getpid (), fork (),>

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
getpid library function-number acquiring process is
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
a multi-process
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
under the first row is the parent process, the process of the second line sub-
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
process applications, the concept of concurrent following
Here Insert Picture Description
Here Insert Picture Description
applications concurrently, the server in the
Here Insert Picture Description
following server
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
following code for the above analysis
Here Insert Picture Description
Here Insert Picture Description

2. signal: signal (, EXIT.)

How to get the program running in the background
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
signal for the process
Here Insert Picture Description
as essentially a macro signal names
Here Insert Picture Description
ending above named child process sends a signal to the parent SIGCHLD, if the parent process does not handle the signal, and that the child process becomes a zombie process.
The signal value of 11 is their illegal use or indiscriminate use of null pointer addresses, and even add the same signal values 9 ignore the code can not be ignored.
Here Insert Picture Description
Here Insert Picture Description
A third recovery signal processing method as default values generally do not, as a custom function EXIT function
Here Insert Picture Description
following the function is custom EXIT function, because the global variable is set TcpServer EXIT function to access it and to close the socket
Here Insert Picture Description
Here Insert Picture Description
as ctrl + c and kill / kill all eXIT command can call the function to shut down the process, but not to kill the process, and called the notice of withdrawal
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
following is the operating system kill command signal, not kill C language functions. kill other users can not kill the process
Here Insert Picture Description
kill function returns a value of 0 on success, -1 Failure. This feature will be used in the monitoring process, the following argv [1] is the first parameter i.e. kill pid number, argv [2] The second parameter is a signal value
Here Insert Picture Description
Here Insert Picture Description

3. The inter-process communication: shmget ()

Process data space is independent of each other, they can not communicate. However, in some cases required inter-process communication with each other to complete the system, or a function to exchange data
following communication scenario is the process
Here Insert Picture Description
message queue similar socket, but only in the machine. General shared memory and semaphores combined with
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
5 sample program: a pointer operation is equivalent to the shared memory operation
Here Insert Picture Description
is written is the string "process of this program ...", redis do not have to write their own shared memory
Here Insert Picture DescriptionHere Insert Picture Description

4. Multithreading: pthread_create ()

Here Insert Picture Description
The first parameter in pthread_t (data type): typedef unsigned long int pthread_t, unsigned long
Here Insert Picture Description
new customers start a thread on the end of the process rather than the company, following the thread to see how to write the main function, pthread_create is a C language library functions
Here Insert Picture Description
will thread_start changed pth_main (thread main function)
Here Insert Picture Description
as it is written in the form above, so
Here Insert Picture Description
below in the server
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
print below the socket
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
as (void*)ii = void*arg
Here Insert Picture Description
a single multi-threaded process: kill only process (kill thread does not exist), the process of killing threads automatically exit
multiple processes: parent was killed, not to reclaim the child process
multiple threads will not complete the assignment thread switching, the value is removed, not the original value of that value

5. Thread Synchronization: pthread_mutex_t

Here Insert Picture Description
Here Insert Picture Description
pthread_mutex_t structure. Data input: landing page. Data show: java, Microsoft .net, c #. Shared resources: 全局变量, 全局对象(database conjunctions, socket, log file objects)
Here Insert Picture Description
to open the way to a +, because the log files continue to the end of the additional content
Here Insert Picture Description
Here Insert Picture Description
as book242 not the contents written on the screen but the log file is written
Here Insert Picture Description
Here Insert Picture Description
after the top kill process killall book242 View .log files can be displayed, the following modifications are optional buffer write buffer to solve the problem of
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
multiple threads share resources with logfile, may be a problem (file content writing disorder), logfile to define a lock, a lock a shared resource
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
above before writing operation and the same log, the surface can not see the effect, the following modifications sleep one second
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
to 20 client 30 sent message is not flashed, and lock with 1 second, so to be finished (20 * 30 )second
Here Insert Picture Description

Published 18 original articles · won praise 113 · views 20000 +

Guess you like

Origin blog.csdn.net/weixin_43435675/article/details/103216651