Inter-thread synchronization
Sync: Work with one another to accomplish something
Mutual exclusion: Guaranteed access to shared resources integrity (you don't have me)
Synchronization in POSIX threads: using semaphore implementations
Semaphore: Represents a class of resources whose value represents the number of resources
Access to resources:
P Action (Request Resource) [value of the resource-1]
....
V Action (frees Resource) [value of resource + 1]
1. Define the semaphore
sem_t sem;
2. Initialize the semaphore
int Sem_init (sem_t *sem, int pshared, unsigned int value);
Parameters:
@sem Signal Volume
@pshared 0: Use between threads
@value the value of the semaphore initialized
return value:
Successful return 0, failure return-1
3.P operation
int sem_wait (sem_t *sem);
4.V operation
int Sem_post (sem_t *sem);
Two-process communication (inter-process data interaction)
(1) Traditional inter-process communication mode
[1] Nameless pipe
[2] Famous pipes
[3] Signal
(2) System 5 IPC Object interprocess communication mode
[1] Message Queuing
[2] Shared memory
[3] Beacon set
(3) Socket communication
(4) Increased binder inter-process communication among Android systems
Linux supports all of the above interprocess communication methods
Inter-process communication between three pipelines
(1) Nameless pipes
Characteristics:
Can only be used with affinity interprocess communication (a process with affinity for data copy action (copy parent process creating child process))
int pipe (int pipefd[2]);
Function: Create a nameless pipe
Parameters:
@pipefd get the file descriptor for an unnamed pipe pipefd[0]: Read the nameless pipe pipefd[1]: Write a nameless pipe
return value:
Successful return 0, failure return-1
(2) Pipe reading and writing rules
Read-only, write pipeline----> can write data to the pipeline as long as the pipe is not full
Read end does not exist, write pipeline----> at this time the write pipeline is meaningless, the operating system will send sigpipe kill the process of writing pipeline
Write end exists, read pipeline----> read data in pipeline, no data in pipeline, read block
The write end does not exist, the read pipeline----> The pipeline reads data, there is no data in the pipeline, this time does not block, return immediately, return the value 0
(3) Famous pipes
Features: can be used for any inter-process communication, it is a special kind of file, in the file system exists name,
And the data stored in the file is in kernel space, not on disk
1. Create a well-known pipeline file
int Mkfifo (const char *pathname, mode_t mode);
@pathname path to a well-known pipeline
Permission to @mode a named pipe
return value:
Successful return 0, failure return-1
2. Open the famous pipe file
Open
If one end of a well-known pipe is opened as read-only, it blocks until the other end is opened in a write (write-only or read-write) way
If one end of a famous pipe is opened in a write-only manner, it will block until the other end is opened in a read (read-only or read-write) way
3. Read and write operations
Read/write
4. Close the piping file
Close (FD);
Quad Signal
Signals are asynchronous interprocess communication modes
How the process responds to the signal:
<1> Ignore
SIGKILL and SIGSTOP cannot be ignored
<2> capture
When the process receives a signal, the signal processing function executed at this time
<3> Default
Most signals are the default action for processes that kill processes
When the child process state changes, the operating system sends SIGCHLD to the parent process, which is ignored by default
typedef void (*sighandler_t) (int);
sighandler_t signal (int signum, sighandler_t handler);
Function: Set process to signal processing mode
Parameters:
Number of @signum signal
@handler
Sig_ign: Ignore Signal
SIG_DFL: Using the default processing method
Function Name: Capture mode processing
return value:
Successfully returned handler, failed to return Sig_err
Practice:
How to do non-blocking, non-rotation ways to reclaim zombie child processes
2. Set a timer in the process
unsigned int alarm (unsigned int seconds);
Parameters:
@seconds timed time, in seconds
Attention:
Once the timed time is complete, the operating system sends a SIGALRM signal to the process
A process:
Read a file, write a pipeline
A process end condition: The file has no data to read
B Process:
Read pipelines, write files
B Process End Condition: At write end, read end does not block, if there is no data in the pipeline, the read pipeline will return 0
Linux system Programming (3)