进程与线程管理原理 进程与线程管理原理1. 技术分析1.1 进程概念进程是程序的执行实例进程特性 独立性: 独立的地址空间 并发性: 多个进程同时执行 动态性: 生命周期变化 进程状态: 就绪: 等待CPU 运行: 执行中 阻塞: 等待I/O 终止: 执行完成1.2 线程概念线程是进程内的执行单元线程特点 共享资源: 共享进程地址空间 轻量级: 创建开销小 并发执行: 同一进程内多个线程 线程类型: 用户线程: 用户态管理 内核线程: 内核管理 混合线程: 用户内核1.3 进程vs线程特性进程线程地址空间独立共享资源开销大小通信方式IPC共享内存并发粒度粗细2. 核心功能实现2.1 进程创建#include stdio.h #include stdlib.h #include unistd.h #include sys/wait.h int main() { printf(父进程 PID: %d\n, getpid()); // 创建子进程 pid_t pid fork(); if (pid 0) { perror(fork failed); exit(1); } else if (pid 0) { // 子进程 printf(子进程 PID: %d, 父进程 PPID: %d\n, getpid(), getppid()); // 执行新程序 execl(/bin/echo, echo, Hello from child process, NULL); perror(execl failed); exit(1); } else { // 父进程 printf(创建子进程 PID: %d\n, pid); // 等待子进程结束 int status; waitpid(pid, status, 0); if (WIFEXITED(status)) { printf(子进程正常结束退出码: %d\n, WEXITSTATUS(status)); } else if (WIFSIGNALED(status)) { printf(子进程被信号终止信号: %d\n, WTERMSIG(status)); } } return 0; }2.2 线程创建与同步#include stdio.h #include stdlib.h #include pthread.h #define NUM_THREADS 5 int counter 0; pthread_mutex_t mutex; void *worker(void *arg) { int id *(int *)arg; for (int i 0; i 1000; i) { pthread_mutex_lock(mutex); counter; pthread_mutex_unlock(mutex); } printf(线程 %d 完成counter %d\n, id, counter); pthread_exit(NULL); } int main() { pthread_t threads[NUM_THREADS]; int thread_ids[NUM_THREADS]; pthread_mutex_init(mutex, NULL); // 创建线程 for (int i 0; i NUM_THREADS; i) { thread_ids[i] i; int rc pthread_create(threads[i], NULL, worker, thread_ids[i]); if (rc ! 0) { printf(线程创建失败: %d\n, rc); exit(1); } } // 等待所有线程完成 for (int i 0; i NUM_THREADS; i) { pthread_join(threads[i], NULL); } printf(最终 counter %d\n, counter); pthread_mutex_destroy(mutex); return 0; }2.3 线程池实现#include stdio.h #include stdlib.h #include pthread.h #include queue using namespace std; typedef struct { void (*func)(void *); void *arg; } Task; class ThreadPool { private: pthread_t *threads; queueTask tasks; pthread_mutex_t mutex; pthread_cond_t cond; int num_threads; bool shutdown; static void *thread_func(void *arg) { ThreadPool *pool (ThreadPool *)arg; pool-worker(); return NULL; } void worker() { while (!shutdown) { pthread_mutex_lock(mutex); while (tasks.empty() !shutdown) { pthread_cond_wait(cond, mutex); } if (shutdown) { pthread_mutex_unlock(mutex); return; } Task task tasks.front(); tasks.pop(); pthread_mutex_unlock(mutex); task.func(task.arg); } } public: ThreadPool(int n) : num_threads(n), shutdown(false) { pthread_mutex_init(mutex, NULL); pthread_cond_init(cond, NULL); threads new pthread_t[num_threads]; for (int i 0; i num_threads; i) { pthread_create(threads[i], NULL, thread_func, this); } } ~ThreadPool() { shutdown true; pthread_cond_broadcast(cond); for (int i 0; i num_threads; i) { pthread_join(threads[i], NULL); } pthread_mutex_destroy(mutex); pthread_cond_destroy(cond); delete[] threads; } void add_task(void (*func)(void *), void *arg) { pthread_mutex_lock(mutex); tasks.push({func, arg}); pthread_mutex_unlock(mutex); pthread_cond_signal(cond); } };3. 性能对比3.1 进程vs线程开销操作进程线程创建时间约1ms约0.1ms内存开销约1MB约1KB切换时间约10us约1us3.2 同步机制对比机制开销适用场景互斥锁低临界区保护读写锁中读多写少条件变量低线程间通信信号量低资源计数3.3 调度算法对比算法特点适用场景FIFO简单实时系统RR公平分时系统SJF短作业优先批处理MLFQ多级反馈通用系统4. 最佳实践4.1 线程安全// 使用互斥锁保护共享资源 pthread_mutex_t mutex; void increment_counter() { pthread_mutex_lock(mutex); counter; pthread_mutex_unlock(mutex); } // 使用RAII封装锁 class LockGuard { private: pthread_mutex_t mutex; public: LockGuard(pthread_mutex_t m) : mutex(m) { pthread_mutex_lock(mutex); } ~LockGuard() { pthread_mutex_unlock(mutex); } };4.2 避免死锁// 按顺序获取锁 void safe_operation() { pthread_mutex_lock(mutex1); pthread_mutex_lock(mutex2); // 操作... pthread_mutex_unlock(mutex2); pthread_mutex_unlock(mutex1); } // 使用trylock bool try_operation() { if (pthread_mutex_trylock(mutex1) ! 0) { return false; } if (pthread_mutex_trylock(mutex2) ! 0) { pthread_mutex_unlock(mutex1); return false; } // 操作... pthread_mutex_unlock(mutex2); pthread_mutex_unlock(mutex1); return true; }5. 总结进程与线程是操作系统并发的核心进程资源分配的基本单位线程CPU调度的基本单位同步机制保证数据一致性线程池提高并发效率对比数据如下线程创建开销是进程的1/10互斥锁开销最低MLFQ是Linux默认调度算法推荐使用线程池管理大量并发任务