在操作系统学习中,线程是其中一个非常重要的概念。线程是操作系统能够进行运算调度的最小单位,它是进程中的一个实体,被系统独立调度和分派的基本单位。掌握线程对于理解操作系统的工作原理至关重要。以下是一些实战习题,帮助你轻松入门操作系统线程。
实战习题一:线程创建与终止
1.1 创建线程
问题描述: 编写一个简单的C++程序,使用POSIX线程(pthread)创建一个线程,并让主线程等待该线程执行完毕。
代码示例:
#include <pthread.h>
#include <iostream>
#include <unistd.h>
void* threadFunction(void* arg) {
std::cout << "Thread is running..." << std::endl;
sleep(2); // 模拟线程执行任务
return nullptr;
}
int main() {
pthread_t thread_id;
int ret = pthread_create(&thread_id, nullptr, threadFunction, nullptr);
if (ret != 0) {
std::cerr << "Failed to create thread: " << ret << std::endl;
return 1;
}
pthread_join(thread_id, nullptr); // 等待线程结束
std::cout << "Thread finished." << std::endl;
return 0;
}
1.2 终止线程
问题描述: 在上述程序中,添加一个函数来安全地终止线程。
代码示例:
#include <pthread.h>
#include <iostream>
#include <unistd.h>
volatile sig_atomic_t keepRunning = 1;
void* threadFunction(void* arg) {
while (keepRunning) {
std::cout << "Thread is running..." << std::endl;
sleep(1);
}
return nullptr;
}
void stopThread(pthread_t thread_id) {
keepRunning = 0;
pthread_join(thread_id, nullptr);
}
int main() {
pthread_t thread_id;
int ret = pthread_create(&thread_id, nullptr, threadFunction, nullptr);
if (ret != 0) {
std::cerr << "Failed to create thread: " << ret << std::endl;
return 1;
}
sleep(5); // 运行一段时间后停止线程
stopThread(thread_id);
std::cout << "Thread finished." << std::endl;
return 0;
}
实战习题二:线程同步
2.1 使用互斥锁
问题描述: 编写一个程序,使用互斥锁(mutex)来保护共享资源,防止多个线程同时访问。
代码示例:
#include <pthread.h>
#include <iostream>
#include <unistd.h>
pthread_mutex_t mutex;
void* threadFunction(void* arg) {
for (int i = 0; i < 10; ++i) {
pthread_mutex_lock(&mutex);
std::cout << "Thread " << (long)arg << ": " << i << std::endl;
pthread_mutex_unlock(&mutex);
sleep(1);
}
return nullptr;
}
int main() {
pthread_t threads[10];
for (int i = 0; i < 10; ++i) {
pthread_mutex_init(&mutex, nullptr);
pthread_create(&threads[i], nullptr, threadFunction, (void*)i);
}
for (int i = 0; i < 10; ++i) {
pthread_join(threads[i], nullptr);
pthread_mutex_destroy(&mutex);
}
return 0;
}
2.2 条件变量
问题描述: 使用条件变量实现一个生产者-消费者模型。
代码示例:
#include <pthread.h>
#include <iostream>
#include <unistd.h>
pthread_mutex_t mutex;
pthread_cond_t cond;
int buffer[10] = {0};
int in = 0, out = 0;
void* producer(void* arg) {
while (true) {
pthread_mutex_lock(&mutex);
while (buffer[in] != 0) {
pthread_cond_wait(&cond, &mutex);
}
buffer[in] = 1;
in = (in + 1) % 10;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
sleep(1);
}
return nullptr;
}
void* consumer(void* arg) {
while (true) {
pthread_mutex_lock(&mutex);
while (buffer[out] == 0) {
pthread_cond_wait(&cond, &mutex);
}
buffer[out] = 0;
out = (out + 1) % 10;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
sleep(1);
}
return nullptr;
}
int main() {
pthread_t producer_thread, consumer_thread;
pthread_mutex_init(&mutex, nullptr);
pthread_cond_init(&cond, nullptr);
pthread_create(&producer_thread, nullptr, producer, nullptr);
pthread_create(&consumer_thread, nullptr, consumer, nullptr);
pthread_join(producer_thread, nullptr);
pthread_join(consumer_thread, nullptr);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}
通过以上实战习题,你可以更好地理解操作系统线程的概念和应用。在实际开发中,线程的使用非常广泛,掌握线程编程对于提高程序性能和并发处理能力至关重要。希望这些习题能够帮助你入门操作系统线程编程。
