在Unix系统中,多道编程是一种常见的编程方式,它允许系统同时运行多个程序,从而提高资源利用率和系统性能。本文将揭秘Unix系统下的多道编程技巧,帮助您轻松实现高效并发处理。
一、进程与线程
1.1 进程
在Unix系统中,进程是系统进行资源分配和调度的基本单位。每个进程都有自己的地址空间、数据段、堆栈等资源。
#include <sys/types.h>
#include <unistd.h>
int main() {
pid_t pid = fork();
if (pid == 0) {
// 子进程
printf("子进程,PID:%d\n", getpid());
} else {
// 父进程
printf("父进程,PID:%d\n", getpid());
}
return 0;
}
1.2 线程
线程是进程中的一个实体,被系统独立调度和分派的基本单位。Unix系统中,线程可以通过pthread库进行操作。
#include <pthread.h>
#include <stdio.h>
void *thread_func(void *arg) {
printf("线程ID:%ld\n", pthread_self());
return NULL;
}
int main() {
pthread_t tid;
pthread_create(&tid, NULL, thread_func, NULL);
pthread_join(tid, NULL);
return 0;
}
二、并发编程
2.1 线程同步
在多线程编程中,线程同步是保证数据一致性和程序正确性的关键。Unix系统中,线程同步可以通过互斥锁、条件变量、信号量等机制实现。
#include <pthread.h>
#include <stdio.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void *thread_func(void *arg) {
pthread_mutex_lock(&mutex);
printf("线程ID:%ld\n", pthread_self());
pthread_mutex_unlock(&mutex);
return NULL;
}
int main() {
pthread_t tid1, tid2;
pthread_create(&tid1, NULL, thread_func, NULL);
pthread_create(&tid2, NULL, thread_func, NULL);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
return 0;
}
2.2 线程通信
线程通信是指线程之间交换数据和信息的过程。Unix系统中,线程通信可以通过消息队列、信号量、共享内存等机制实现。
#include <pthread.h>
#include <stdio.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
int data = 0;
void *producer(void *arg) {
for (int i = 0; i < 10; i++) {
pthread_mutex_lock(&mutex);
data = i;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
return NULL;
}
void *consumer(void *arg) {
for (int i = 0; i < 10; i++) {
pthread_mutex_lock(&mutex);
while (data == 0) {
pthread_cond_wait(&cond, &mutex);
}
printf("消费数据:%d\n", data);
pthread_mutex_unlock(&mutex);
}
return NULL;
}
int main() {
pthread_t tid1, tid2;
pthread_create(&tid1, NULL, producer, NULL);
pthread_create(&tid2, NULL, consumer, NULL);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
return 0;
}
三、多进程编程
3.1 进程间通信
进程间通信是指不同进程之间交换数据和信息的过程。Unix系统中,进程间通信可以通过管道、命名管道、信号量、共享内存等机制实现。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
int pipefd[2];
pid_t cpid;
char message[20];
if (pipe(pipefd) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
cpid = fork();
if (cpid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (cpid == 0) { // 子进程
close(pipefd[1]); // 关闭写端
read(pipefd[0], message, sizeof(message)); // 读取数据
printf("Received: %s\n", message);
close(pipefd[0]);
exit(EXIT_SUCCESS);
} else { // 父进程
close(pipefd[0]); // 关闭读端
write(pipefd[1], "Hello, child!\n", 17); // 发送数据
close(pipefd[1]);
wait(NULL); // 等待子进程结束
exit(EXIT_SUCCESS);
}
}
3.2 进程池
进程池是一种常用的多进程编程模式,它通过创建一定数量的进程来处理任务,从而提高程序性能。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define POOL_SIZE 5
void *worker(void *arg) {
int id = *(int *)arg;
printf("Worker %d started\n", id);
sleep(1);
printf("Worker %d finished\n", id);
free(arg);
return NULL;
}
int main() {
pthread_t threads[POOL_SIZE];
int ids[POOL_SIZE];
for (int i = 0; i < POOL_SIZE; i++) {
ids[i] = i;
if (pthread_create(&threads[i], NULL, worker, &ids[i]) != 0) {
perror("pthread_create");
exit(EXIT_FAILURE);
}
}
for (int i = 0; i < POOL_SIZE; i++) {
pthread_join(threads[i], NULL);
}
return 0;
}
四、总结
Unix系统下的多道编程是一种高效的处理并发任务的方式。通过掌握进程、线程、并发编程和多进程编程等技巧,您可以轻松实现高效并发处理。希望本文能帮助您更好地理解Unix系统下的多道编程,为您的编程之路助力。
