在Unix系统中,进程并发执行是一种高效利用系统资源、提高系统性能的重要手段。本文将详细解析Unix系统中实现进程并发执行的技巧,帮助读者轻松掌握这一技能。
Unix进程并发概述
Unix系统中的进程并发主要依靠以下几种机制实现:
- 进程创建:通过
fork()系统调用创建新的进程。 - 进程间通信:使用管道(pipe)、信号(signal)、共享内存(shared memory)、消息队列(message queue)等机制实现进程间通信。
- 进程同步:使用互斥锁(mutex)、条件变量(condition variable)等机制实现进程同步。
进程创建
进程创建是并发执行的基础。以下是一个使用fork()系统调用创建进程的示例代码:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main() {
pid_t pid = fork();
if (pid == -1) {
// 创建进程失败
perror("fork");
return 1;
} else if (pid == 0) {
// 子进程
printf("Hello from child process!\n");
return 0;
} else {
// 父进程
printf("Hello from parent process! PID of child: %d\n", pid);
return 0;
}
}
进程间通信
进程间通信是进程并发执行的关键。以下是一些常用的进程间通信机制:
管道(pipe)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
int pipefd[2];
if (pipe(pipefd) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
pid_t pid = fork();
if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (pid == 0) {
// 子进程
close(pipefd[0]); // 关闭读端
write(pipefd[1], "Hello from child!\n", 18);
} else {
// 父进程
close(pipefd[1]); // 关闭写端
char buffer[20];
read(pipefd[0], buffer, 19);
printf("Hello from parent! %s", buffer);
}
return 0;
}
信号(signal)
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
void handler(int sig) {
printf("Received signal %d\n", sig);
}
int main() {
signal(SIGINT, handler);
while (1) {
printf("Hello from main process!\n");
sleep(1);
}
return 0;
}
共享内存(shared memory)
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
int main() {
key_t key = ftok("shmfile", 65);
int shmid = shmget(key, 1024, 0666 | IPC_CREAT);
char *shm = shmat(shmid, (void*)0, 0);
char *s = "Hello from shared memory!";
strcpy(shm, s);
printf("Data written into shared memory\n");
sleep(10);
return 0;
}
进程同步
进程同步是确保多个进程正确执行的关键。以下是一些常用的进程同步机制:
互斥锁(mutex)
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
pthread_mutex_t lock;
void *func(void *arg) {
pthread_mutex_lock(&lock);
printf("Thread %ld: entered critical section\n", (long)arg);
// ... 执行临界区代码 ...
pthread_mutex_unlock(&lock);
printf("Thread %ld: exited critical section\n", (long)arg);
return NULL;
}
int main() {
pthread_t thread1, thread2;
pthread_mutex_init(&lock, NULL);
pthread_create(&thread1, NULL, func, (void*)1L);
pthread_create(&thread2, NULL, func, (void*)2L);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
pthread_mutex_destroy(&lock);
return 0;
}
条件变量(condition variable)
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
pthread_mutex_t lock;
pthread_cond_t cond;
void *func(void *arg) {
pthread_mutex_lock(&lock);
// ... 执行一些代码 ...
pthread_cond_signal(&cond);
pthread_mutex_unlock(&lock);
return NULL;
}
int main() {
pthread_t thread1, thread2;
pthread_mutex_init(&lock, NULL);
pthread_cond_init(&cond, NULL);
pthread_create(&thread1, NULL, func, (void*)1L);
pthread_create(&thread2, NULL, func, (void*)2L);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
pthread_mutex_destroy(&lock);
pthread_cond_destroy(&cond);
return 0;
}
总结
通过以上解析,相信读者已经掌握了Unix系统中实现进程并发执行的技巧。在实际应用中,可以根据具体需求选择合适的并发机制,提高系统性能。
