首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

Linux 系統程式開發備忘錄 (五):信號處理、定時器、進程控制、線程同步完整 API

     編輯:關於C語言

Linux 系統程式開發備忘錄 (五):信號處理、定時器、進程控制、線程同步完整 API

本筆記整理 Linux 進程/線程核心控制 API:信號捕獲與阻塞、定時器與休眠、進程建立/終止/等待/執行新程式、線程建立/同步/取消/清理,按功能分類並標註關鍵參數與返回值,適合系統程式開發者高頻查閱。

信號處理

#include <signal.h>
int sigaction(int sig, const struct sigaction *act, struct sigaction *oldact); // 推薦,可靠
int kill(pid_t pid, int sig);          // 發送信號 (pid>0 指定進程, 0 同組, -1 所有)
int raise(int sig);                    // 向自身發送
int sigemptyset/fillset/addset/delset(sigset_t *set, int sig); // 信號集操作
int sigismember(const sigset_t *set, int sig); // 測試成員
int sigprocmask(int how, const sigset_t *set, sigset_t *oldset); // 阻塞/解除阻塞
int pause(void);                       // 等待信號 (永遠返回 -1, errno=EINTR)
void abort(void);                      // 異常終止 (產生 core dump)

定時器與休眠

#include <sys/time.h>
int setitimer(int which, const struct itimerval *new, struct itimerval *old);
// which: ITIMER_REAL(牆鐘,SIGALRM), ITIMER_VIRTUAL(用戶態,SIGVTALRM), ITIMER_PROF(剖析,SIGPROF)
int getitimer(int which, struct itimerval *curr);
#include <unistd.h>
unsigned alarm(unsigned seconds);      // 秒級鬧鐘 (SIGALRM),返回舊剩餘秒數
unsigned sleep(unsigned seconds);      // 休眠秒數,被信號中斷返回剩餘秒數
// POSIX 高精度定時器
#define _POSIX_C_SOURCE 199309
#include <time.h> #include <signal.h>
int timer_create(clockid_t clock, struct sigevent *evp, timer_t *tid);
int timer_settime(timer_t tid, int flags, const struct itimerspec *val, struct itimerspec *old);
int timer_gettime(timer_t tid, struct itimerspec *val);
int timer_delete(timer_t tid);

進程控制

#include <unistd.h>
pid_t fork(void);                      // 建立子進程,父返子 PID,子返 0,錯誤 -1
void _exit(int status);                // 直接終止 (不刷緩衝、不執行 atexit)
#include <stdlib.h>
void exit(int status);                 // 標準終止 (執行 atexit、刷緩衝、呼叫 _exit)
int atexit(void (*func)(void));        // 登記退出處理函式
#include <sys/wait.h>
pid_t wait(int *status);               // 等待任一子進程
pid_t waitpid(pid_t pid, int *status, int options); // 等待特定子進程
// WIFEXITED, WEXITSTATUS, WIFSIGNALED, WTERMSIG 宏解析 status
// exec 族:替換進程映像 (成功不返回)
int execl/execv/execlp/execvp/execle/execve(const char *path, ...);

線程同步

#include <pthread.h>
int pthread_create(pthread_t *tid, const pthread_attr_t *attr, void*(*start)(void*), void *arg);
void pthread_exit(void *retval);       // 線程終止
pthread_t pthread_self(void);          // 取得自身 TID
int pthread_equal(pthread_t t1, pthread_t t2);
int pthread_join(pthread_t tid, void **retval); // 等待線程結束 (回收資源)
int pthread_detach(pthread_t tid);     // 分離線程 (自動回收)
// 互斥鎖
int pthread_mutex_lock/unlock(pthread_mutex_t *mutex);
int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr);
int pthread_mutex_destroy(pthread_mutex_t *mutex);
// 條件變量
int pthread_cond_signal/broadcast(pthread_cond_t *cond);
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex); // 原子解鎖+等待
int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime);
int pthread_cond_init/destroy(pthread_cond_t *cond, const pthread_condattr_t *attr);
// 一次性初始化
int pthread_once(pthread_once_t *once, void (*init)(void));
// 線程取消
int pthread_cancel(pthread_t tid);
int pthread_setcancelstate/type(int state, int *old);
void pthread_testcancel(void);         // 取消點
// 線程清理
void pthread_cleanup_push(void(*routine)(void*), void *arg);
void pthread_cleanup_pop(int execute);
// 線程信號屏蔽
int pthread_sigmask(int how, const sigset_t *set, sigset_t *oldset);
int pthread_kill(pthread_t tid, int sig); // 向線程發送信號 (常用 0 測試存活)

關鍵最佳實踐

  • fork 後子進程只能呼叫 async-signal-safe 函式,或立即 exec
  • waitpid 必須處理 EINTR 重試
  • 互斥鎖保護的臨界區盡量短,避免死結 (固定鎖順序、trylock、超時鎖)
  • 條件變量 wait 必須在循環中檢查條件 (虛假喚醒)
  • 線程取消點:pthread_testcancel、pthread_cond_wait、I/O 等
  • 清理處理器 push/pop 必須成對,pop(1) 執行清理

精通這些 API,是編寫高併發、高可靠 Linux 系統服務的核心能力。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved