Linux programming-memoir 5: A memoir of linux Programming
==== Signal Processing ====
# Include <signal. h>
Int sigaction (int sig, const struct sigaction * act,
Struct sigaction * oldact );
Set the signal processing function, which is safer and more reliable than the signal function
# Include <signal. h>
Int kill (pid_t pid, int sig );
The process that sends a signal to the pid. Generally, the test process exists and can send 0 signals.
0 is returned, and-1 is returned.
# Include <signal. h>
Int raise (int sig );
Send a signal to the process itself, and return 0 success, non-0 Error
# Include <signal. h>
Int sigemptyset (sigset_t * set );
Int sigfillset (sigset_t * set );
Int sigaddset (sigset_t * set, int sig );
Int sigdelset (sigset_t * set, int sig );
Clear, fill, add, delete Signal Set, return 0 success,-1 failed
# Include <signal. h>
Int sigismember (const sigset_t * set, int sig );
Test whether the sig signal is in the set signal set. If 1 is returned, it indicates that the signal exists and 0 indicates that the signal does not exist.
# Include <signal. h>
Int sigprocmask (int how, const sigset_t * set, sigset_t * oldset );
If the signal mask is set, 0 is returned, and-1 fails.
# Include <unistd. h>
Int pause (void );
Pause the program, know that the program receives any signal to interrupt it, always return-1
Set errno to EINTR.
# Include <stdlib. h>
Void abort (void );
Exit the program and generate coredump
=== Timer and sleep ===
# Include <sys/time. h>
Int setitimer (int which, const struct itimerval * new_value,
Struct itimerval * old_value );
Set a timeout and send a specific signal to the process itself
The value of which is as follows:
The time on the ITIMER_REAL wall prevails, and the SIGALRM signal is sent to the user upon timeout.
The user time of the ITIMER_VIRTUAL program prevails, and the SIGVTALRM signal is sent to the user upon timeout.
ITIMER_PROF running time (CPU & USER), send SIGPROF to yourself upon timeout
# Include <sys/time. h>
Int getitimer (int which, struct itimerval * curr_value );
Obtain the status of the which timer set in the above function. If 0 is returned, it indicates that the timer is normal. If-1 indicates that the timer has timed out.
# Include <unistd. h>
Unsigned int alarm (unsigned int seconds );
After seconds, the SIGALARM signal is sent, and the remaining timeout seconds are set for the previous time.
If the return value is 0, no settings are set for the previous time, and the operation is successful forever.
# Include <unistd. h>
Unsigned int sleep (unsigned int seconds );
Sleep seconds. If the returned value is 0, the task is completed, or the number of seconds that have been sleep (interrupted)
# Define _ POSIX_C_SOURCE 199309
# Include <signal. h>
# Include <time. h>
Int timer_create (clockid_t clockid, struct sigevent * evp, timer_t * timerid );
Create a timer, return 0 success,-1 failed
Define _ POSIX_C_SOURCE 199309
# Include <time. h>
Int timer_settime (timer_t timerid, int flags,
Const struct itimerspec * value, struct itimerspec * old_value );
Initialization timer, return 0 success,-1 failed
# Define _ POSIX_C_SOURCE 199309
# Include <time. h>
Int timer_gettime (timer_t timerid, struct itimerspec * curr_value );
Returns 0 if the timer information is obtained.-1 fails.
# Define _ POSIX_C_SOURCE 199309
# Include <time. h>
Int timer_delete (timer_t timerid );
If the timer is deleted successfully, 0 is returned, and the value-1 is returned.
==== Process ====
# Include <unistd. h>
Pid_t fork (void );
Create a process and Return Error-1. If the parent process is successful, return the sub-process ID and sub-process ID.
Returns 0.
Include <unistd. h>
Void _ exit (int status );
Exit Process
# Include <stdlib. h>
Void exit (int status)
Exit the program and execute the following cleanup:
1. If exit handlers is registered, call them first
2. Clear IO-related buffer memory
3. Call the _ exit function and use the status value as the return status.
It is best to use the value 0-for the status value, because of historical reasons, the exit value
It is composed of 16 bits (2 bytes). The 8 bits in height indicate the exit value of the program, and the 8 bits in height indicate the exit signal of the program.
# Include <stdlib. h>
Int atexit (void (* func) (void ));
Register the exit handler function and return 0,-1 error.
# Include <sys/wait. h>
Pid_t wait (int * status );
1. Blocking until the sub-process ends
2. subprocess Return Value write status (when not NULL)
3. Count the resource usage of all sub-Processes
4. Return the ID of the sub-process
-1 indicates an error.
# Include <sys/wait. h>
Pid_t waitpid (pid_t pid, int * status, int options );
1. pid> 0. Wait for the pid subprocess
2. pid = 0. Wait for the process with the same process group ID equal to the parent process
3. pid <-1, waiting for the process whose ID is equal to the absolute value of pid in the same process group
4. pid =-1, wait for all sub-Processes
# Include <unistd. h>
Int execve (const char * pathname, char * const argv [], char * const envp []);
Int execle (const char * pathname, const char * arg ,...
/*, (Char *) NULL, char * const envp [] */);
Int execlp (const char * filename, const char * arg,.../*, (char *) NULL */);
Int execvp (const char * filename, char * const argv []);
Int execv (const char * pathname, char * const argv []);
Int execl (const char * pathname, const char * arg ,...
/*, (Char *) NULL */);
Success will never be returned.-1 indicates an error.
# Include <stdlib. h>
Int system (const char * command );
Generate a sub-process, call shell to execute the command, and return the value
1. If command is NULL but shell exists, a non-zero value is returned; otherwise, 0 is returned.
2. If a sub-process cannot be created,-1 is returned.
3. If the execution is successful, return the status value of the command execution.
4. If the shell cannot be executed, 127 is returned.
==== Thread ====
# Include <pthread. h>
Int pthread_create (pthread_t * thread, const pthread_attr_t * attr,
Void * (* start) (void *), void * arg );
If a thread is created successfully, 0 is returned.-1 indicates an error.
# Include <pthread. h>
Void pthread_exit (void * retval );
End thread
# Include <pthread. h>
Pthread_t pthread_self (void );
Obtains the ID of a thread.
# Include <pthread. h>
Int pthread_equal (pthread_t t1, pthread_t t2 );
Determines whether a thread is the same. The return value is non-zero and the value 0 indicates that the return value is different.
# Include <pthread. h>
Int pthread_join (pthread_t thread, void ** retval );
Reclaim the thread. If 0 is returned, it indicates that the thread is successful and a non-zero error occurs.
# Include <pthread. h>
Int pthread_detach (pthread_t thread );
Separates threads, returns 0 success, non-zero error
# Include <pthread. h>
Int pthread_mutex_lock (pthread_mutex_t * mutex );
Int pthread_mutex_unlock (pthread_mutex_t * mutex );
Locks or locks, returns 0 success, non-zero error
# Include <pthread. h>
Int pthread_mutex_init (pthread_mutex_t * mutex,
Const pthread_mutexattr_t * attr );
A mutex lock is dynamically initialized, and 0 is returned successfully. A non-zero error occurs.
# Include <pthread. h>
Int pthread_mutex_destroy (pthread_mutex_t * mutex );
Destroys the dynamically assigned mutex lock. the return value is 0. A non-zero error occurs.
# Include <pthread. h>
Int pthread_cond_signal (pthread_cond_t * cond );
Int pthread_cond_broadcast (pthread_cond_t * cond );
Int pthread_cond_wait (pthread_cond_t * cond, pthread_mutex_t * mutex );
Send/broadcast/Wait for a condition variable, return 0 success, non-zero error
# Include <pthread. h>
Int pthread_cond_timedwait (pthread_cond_t * cond, pthread_mutex_t * mutex,
Const struct timespec * abstime );
Wait for a condition variable, return timeout, return 0 success, non-zero error
# Include <pthread. h>
Int pthread_cond_init (pthread_cond_t * cond, const pthread_condattr_t * attr );
A condition variable is dynamically initialized, and 0 is returned. A non-zero error occurs.
# Include <pthread. h>
Int pthread_cond_destroy (pthread_cond_t * cond );
Destroys a condition variable and returns 0 success. A non-zero error occurs.
# Include <pthread. h>
Int pthread_once (pthread_once_t * once_control, void (* init) (void ));
Make sure that the atom executes the init function only once, and the return value is 0. A non-zero error occurs.
# Include <pthread. h>
Int pthread_cancel (pthread_t thread );
Thread canceled. 0 is returned. Non-zero is returned.
# Include <pthread. h>
Int pthread_setcancelstate (int state, int * oldstate );
Int pthread_setcanceltype (int type, int * oldtype );
Set whether the response thread cancels the signal/set the synchronous or asynchronous response thread to cancel the signal
0 success, non-zero error returned
# Include <pthread. h>
Void pthread_testcancel (void );
Test the cancellation signal and create a thread cancellation point.
# Include <pthread. h>
Void pthread_cleanup_push (void (* routine) (void *), void * arg );
Void pthread_cleanup_pop (int execute );
Press in/a thread cleanup function pops up. The execution points are as follows:
1. The thread is canceled.
2. The thread exits using the pthread_exit function.
3. Specify a non-zero value for the parameter in the pop-up window.
# Include <signal. h>
Int pthread_sigmask (int how, const sigset_t * set, sigset_t * oldset );
Sets the thread signal processing, returns 0 success, non-zero error
# Include <signal. h>
Int pthread_kill (pthread_t thread, int sig );
Sends a signal to a specified thread. Generally, 0 is sent to test whether the thread is alive.
0 success, non-zero error