Linux and linux commands
Reference: www.cnblogs.com/forstudy/archive/2012/04/05/2433663.html
1. Process and thread
Process: The basic unit of program execution and resource allocation in the system. Each process has its own data segment, code segment, and stack segment. During switchover, a complicated context switching thread is required: the thread has an independent stack segment/errno/thread ID/signal mask. The thread shares the current working directory/code segment/data segment/descriptive word/signal processing/user ID/group ID context switching overhead, this is much smaller than the creation process. It is also known as the function header file related to the lightweight process two threads: # include <pthread. h> 1. pthread_t pthread_self (void); 2. int pthread_equal (pthread_t tid1, pthread_t tid2); 3. create a thread • call the entry point of the thread function • Use the function pthread_create () to run related thread functions after the thread is created. 4. exit the thread • after the thread function is run, the thread exits • or uses the function pthread_exit (), which is the active action of the thread • exit () cannot be used. waiting thread
• Because multiple threads in a process share data segments, the resources occupied by the exited thread will not be released with the termination of the thread.
• Pthread_join () function
Similar to the process's wait ()/waitpid () function, which is used to hook the current thread and wait for the end of the thread to be a thread-blocking function, when the thread that calls it waits until the wait thread ends and the function returns, the resources of the waiting thread are reclaimed.
6. cancel thread • terminate another thread in another thread • pthread_cancel () function • The canceled thread can set its own cancellation status-whether to accept or ignore this request after the canceled thread receives the cancellation request from another thread-If yes, whether to terminate the operation immediately or wait for the call of a function.
7. mutex lock initialization: pthread_mutex_init () mutex lock: pthread_mutex_lock () mutex lock judgment lock: pthread_mutex_trylock () mutex lock unlock: pthread_mutex_unlock () Remove mutex lock: Unlock ()
8. semaphores sem_init () create a semaphores and initialize the sem_wait () and sem_trywait (): P operations. When the semaphores are greater than zero, the semaphores are subtracted by one. • difference: If the semaphores are less than zero, sem_wait () will block the thread, and sem_trywait () will immediately return the sem_post (): V operation, and add the semaphore value and send a signal to wake up the waiting thread sem_getvalue (): obtain the sem_destroy () value of the semaphore: Delete the semaphore.