#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> #include <errno.h> pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; int lock_var; time_t end_time; void pthread1(void *arg); void pthread2(void *arg); int main(int argc, char *argv[]) { pthread_t id1,id2; pthread_t mon_th_id; int ret; end_time = time(NULL)+10; pthread_mutex_init(&mutex,NULL); ret=pthread_create(&id1,NULL,(void *)pthread1, NULL); if(ret!=0) perror("pthread cread1"); ret=pthread_create(&id2,NULL,(void *)pthread2, NULL); if(ret!=0) perror("pthread cread2"); pthread_join(id1,NULL); pthread_join(id2,NULL); exit(0); } void pthread1(void *arg) { int i; while(time(NULL) < end_time){ if(pthread_mutex_lock(&mutex)!=0){ perror("pthread_mutex_lock"); } else printf("pthread1:pthread1 lock the variable\n"); for(i=0;i<2;i++){ sleep(1); lock_var++; } if(pthread_mutex_unlock(&mutex)!=0){ perror("pthread_mutex_unlock"); } else printf("pthread1:pthread1 unlock the variable\n"); sleep(1); } } void pthread2(void *arg) { int nolock=0; int ret; while(time(NULL) < end_time){ ret=pthread_mutex_trylock(&mutex); if(ret==EBUSY) printf("pthread2:the variable is locked by pthread1\n"); else{ if(ret!=0){ perror("pthread_mutex_trylock"); exit(1); } else printf("pthread2:pthread2 got lock.The variable is %d\n",lock_var); if(pthread_mutex_unlock(&mutex)!=0){ perror("pthread_mutex_unlock"); } else printf("pthread2:pthread2 unlock the variable\n"); } sleep(3); } }
时间: 2024-04-07 07:31:27 浏览: 205
这是一个使用pthread线程库实现的多线程同步程序。程序中定义了两个线程pthread1和pthread2,pthread1每隔1秒钟增加一个全局变量lock_var的值,然后释放互斥锁,而pthread2每隔3秒钟尝试获取该互斥锁,如果被pthread1占用,则显示提示信息,否则获取互斥锁并读取lock_var的值。程序中使用了互斥锁pthread_mutex_t来实现对全局变量的互斥访问。在主函数中启动了这两个线程并等待它们的结束。
相关问题
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> #include <semaphore.h> #include <time.h> #define N 5 pthread_mutex_t chopstick[N];//筷子信号量 //哲学家线程函数 void* philosopher(void* arg){ int *i; i = (int *)arg;//哲学家序号 for(;;){ //思考 printf("%d 号哲学家在思考......\n",*i); sleep(rand()%3);//休眠随机时间,不超过3秒 //尝试取回左右两边的筷子 if(pthread_mutex_trylock(&chopstick[*i]) == 0 && pthread_mutex_trylock(&chopstick[(*i+1)%N]) == 0)//尝试同时拿到两个筷子 { //成功拿到两个筷子 printf("%d号哲学家在进餐......\n",*i); sleep(rand()%3);//休眠随机时间,不超过3秒 //放回筷子 pthread_mutex_unlock(&chopstick[*i]); pthread_mutex_unlock(&chopstick[(*i+1)%N]); } else { //释放已拿到的筷子,等待随机时间后重试 pthread_mutex_unlock(&chopstick[*i]); pthread_mutex_unlock(&chopstick[(*i+1)%N]); sleep(rand()%2); } } } int main(){ pthread_t id[N]; int i; for(i=0;i<N;i++) pthread_mutex_init(&chopstick[i],NULL); for(i=0;i<N;i++) { int *p; p=malloc(sizeof(int)); *p=i; pthread_create(&id[i],NULL,philosopher,(void*)p); } for(i=0;i<N;i++) pthread_join(id[i],NULL); }给代码加注释
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <time.h>
#define N 5 //哲学家数量
pthread_mutex_t chopstick[N]; //筷子信号量
//哲学家线程函数
void* philosopher(void* arg){
int *i;
i = (int *)arg; //哲学家序号
for(;;){
//思考
printf("%d 号哲学家在思考......\n",*i);
sleep(rand()%3); //休眠随机时间,不超过3秒
//尝试取回左右两边的筷子
if(pthread_mutex_trylock(&chopstick[*i]) == 0 && pthread_mutex_trylock(&chopstick[(*i+1)%N]) == 0) //尝试同时拿到两个筷子
{
//成功拿到两个筷子
printf("%d号哲学家在进餐......\n",*i);
sleep(rand()%3); //休眠随机时间,不超过3秒
//放回筷子
pthread_mutex_unlock(&chopstick[*i]);
pthread_mutex_unlock(&chopstick[(*i+1)%N]);
}
else
{
//释放已拿到的筷子,等待随机时间后重试
pthread_mutex_unlock(&chopstick[*i]);
pthread_mutex_unlock(&chopstick[(*i+1)%N]);
sleep(rand()%2);
}
}
}
int main(){
pthread_t id[N];
int i;
//初始化筷子信号量
for(i=0;i<N;i++)
pthread_mutex_init(&chopstick[i],NULL);
//创建哲学家线程
for(i=0;i<N;i++)
{
int *p;
p=malloc(sizeof(int));
*p=i;
pthread_create(&id[i],NULL,philosopher,(void*)p);
}
//等待哲学家线程结束
for(i=0;i<N;i++)
pthread_join(id[i],NULL);
}
```
代码中实现了哲学家问题的解决方案,通过线程模拟哲学家的行为,使用互斥锁来解决筷子竞争的问题。其中,`pthread_mutex_init`用于初始化互斥锁,`pthread_create`用于创建线程,`pthread_join`用于等待线程结束。对于每一个哲学家线程,其会进入一个无限循环,先进行思考,然后尝试取回左右两边的筷子,如果成功则进餐,否则释放已拿到的筷子并等待随机时间后重试。
#include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<pthread.h> #include<semaphore.h> #include<time.h> #define inc(k) if(k<BUFFER_SIZE)k=k+1;else k=0 #define BUFFER_SIZE 10//缓冲区的大小 typedef int buffer_item;//定义缓冲区内的数据类型 buffer_item buffer[BUFFER_SIZE];//缓冲区 int in,out;//对缓冲区操作的变量,in表示缓冲区中下一个等待接收产品的空缓冲的下标,out表示下一个要被取走的产品所在满缓冲的下标 pthread_mutex_t mutex;//信号量mutex提供了对缓冲池访问的互斥要求 sem_t empty,full;//信号量empty和full分别表示空缓冲和满缓冲的个数 int pro_speed,con_speed;//可以设定生产者的生产速度及消费者的消费速度 int insert_item(buffer_item item) {//将生产的产品放入缓冲区 buffer[in]=item; printf("->->将产品%d放入缓冲池第%d号\n",item,in); inc(in); } int remove_item(buffer_item *item) { //从缓冲区内移走一个产品 *item = buffer[out]; printf("->->从缓冲池取出第%d号产品%d\n",out,item); inc(out); } /* 生产者线程 */ void *producer(void *param) { buffer_item item; int num = 0; // 记录生产的产品个数 while(1) { sleep(rand()%(16-pro_speed)); printf("\n******第%d次生产******\n",++num); printf("---等待empty信号\n"); sem_wait(&empty); printf("---仓库中有空位,申请进入仓库\n"); pthread_mutex_lock(&mutex); printf("---进入仓库,上锁,准备生产\n"); item=rand()%1000+1; printf("---生产产品%d\n",item); insert_item(item); printf("---解锁,出仓库\n"); pthread_mutex_unlock(&mutex); sem_post(&full); printf("---释放full信号\n"); printf("******第%d次生产结束******\n\n",num); } } /* 消费者线程 */ void *consumer(void *param) { buffer_item item; int num = 0; while(1){ sleep(rand()%(16-con_speed)); printf("\n******第%d次消费******\n",++num); printf("---等待full信号\n"); sem_wait(&full); printf("---仓库中有产品,申请进入仓库\n"); pthread_mutex_lock(&mutex); printf("---进入仓库,上锁,准备消费\n"); remove_item(&item); printf("---消费产品%d\n",item); printf("---解锁,出仓库\n",num); pthread_mutex_unlock(&mutex); sem_post(&empty); printf("---释放empty信号\n"); printf("******第%d次消费结束\n\n",num); } } int main(void)//主函数 { pthread_t thread_p,thread_c; srand(time(NULL)); //设置随机数种子 /* 初始化 */ pthread_mutex_init(&mutex,NULL); sem_init(&empty,0,BUFFER_SIZE); sem_init(&full,0,0); in=0; out=0; printf("=======================\n"); printf("设置生产者、消费者速度\n"); printf("生产者速度(1-15):\n"); scanf("%d",&pro_speed); printf("消费者速度(1-15):\n"); scanf("%d",&con_speed); printf("=======================\n"); /* 创建生产者、消费者线程 */ pthread_create(&thread_p,NULL,producer,NULL); pthread_create(&thread_c,NULL,consumer,NULL); /* 回收 */ pthread_join(thread_p, NULL); pthread_join(thread_c, NULL); pthread_mutex_destroy(&mutex); sem_destroy(&empty); sem_destroy(&full); printf("========程序over========\n"); return 0; } 参照以上程序,根据以下情景生成程序 服务器处理客户端请求时,若当前请求数超过服务器处理能力,新请求需进入队列等待。服务器空闲时进入休眠状态,直到有新请求到达。某服务器可同时处理 3 个请求,最多支持 5 个请求排队等待,当服务器空闲时,从队列中唤醒请求处理。设计信号量同步机制,确保服务器与客户端的正确协作。
以下是基于您提供的程序框架和需求的情景设计的新程序:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#define MAX_QUEUE_SIZE 5 // 最大排队等待请求数量
#define SERVER_CAPACITY 3 // 服务器同时处理的最大请求数量
typedef struct {
int request_id;
} Request;
Request queue[MAX_QUEUE_SIZE]; // 请求队列
int front = 0, rear = 0; // 队列的头指针和尾指针
pthread_mutex_t mutex_queue; // 锁定队列访问
pthread_cond_t cond_server_idle; // 条件变量:用于通知服务器有新的请求到来
sem_t available_slots, active_requests;
// 添加请求到队列中
void enqueue(Request req) {
if ((rear + 1) % (MAX_QUEUE_SIZE + 1) == front) { // 判断是否队列已满
printf("队列已满,无法添加更多请求。\n");
return;
}
queue[rear] = req;
rear = (rear + 1) % (MAX_QUEUE_SIZE + 1);
}
// 从队列中移除并返回最前面的请求
Request dequeue() {
if (front == rear) { // 如果队列为空,则直接返回无效请求
Request null_req = {-1};
return null_req;
}
Request removed_request = queue[front];
front = (front + 1) % (MAX_QUEUE_SIZE + 1);
return removed_request;
}
// 客户端模拟发送请求
void* client_thread(void* param) {
for (int i = 0; i < 8; ++i) { // 假设每个客户端发出8个请求
sleep(rand() % 4); // 发送间隔时间随机化
sem_wait(&available_slots); // 等待是否有空余位置
Request new_request;
new_request.request_id = rand() % 1000 + 1; // 创建唯一ID的请求
pthread_mutex_lock(&mutex_queue); // 加锁保护对共享资源的操作
enqueue(new_request);
printf("[Client] 新请求 ID:%d 已加入队列.\n", new_request.request_id);
pthread_mutex_unlock(&mutex_queue);
sem_post(&active_requests); // 标记活跃请求数加一,并可能唤醒服务端线程
pthread_cond_signal(&cond_server_idle); // 可能激活正在休眠的服务端线程
}
return NULL;
}
// 服务器模拟处理请求
void* server_thread(void* param) {
while (1) {
sem_wait(&active_requests); // 若无未完成的任务则阻塞在此处直至新增任务出现
pthread_mutex_lock(&mutex_queue); // 获取锁定保证临界段安全地修改全局数据结构等操作
Request current_request = dequeue();
if(current_request.request_id != -1){
printf("[Server] 正在处理请求 ID:%d .\n",current_request.request_id );
usleep((rand()%5)*1e6); // 模拟耗时作业
printf("[Server] 请求 ID:%d 处理完毕!\n",current_request.request_id);
}
else{
pthread_cond_wait(&cond_server_idle,&mutex_queue ); // 当没有活动请求的时候进入条件等待状态节省CPU资源
}
pthread_mutex_unlock(&mutex_queue);
sem_post(&available_slots); // 解放一个槽位以便其他客户机可以提交更多的工作项进来
}
}
int main(){
srand(time(NULL));
/* 初始化信号量及互斥体 */
pthread_mutex_init(&mutex_queue ,NULL);
pthread_cond_init(&cond_server_idle,NULL);
sem_init(&available_slots,0,MAX_QUEUE_SIZE);
sem_init(&active_requests,0,0);
pthread_t clients_threads [7],server_thread_handle ;
/* 启动多个客服端线程*/
for(int i=0;i<7;++i){
pthread_create(&(clients_threads[i]),NULL,(void*)client_thread,NULL);
}
/* 开启单个服务端线程 */
pthread_create(&server_thread_handle,NULL,(void*)server_thread,NULL);
/* 收集所有客户的线程结果 */
for(int j=0;j<7 ;j++){
pthread_join(clients_threads[j],NULL);
}
/* 终止服务端线程以及清理环境*/
pthread_cancel(server_thread_handle );
pthread_mutex_destroy(&mutex_queue);
pthread_cond_destroy(&cond_server_idle);
sem_destroy(&available_slots);
sem_destroy(&active_requests);
}
```
### 总结说明:
上述代码实现了这样一个场景——通过创建若干个代表不同用户发起请求的线程(`client_thread()`),它们会不断向系统递交任务;而另一个作为后台工作者的角色即“服务进程”(`server_thread()`)负责依次提取这些已经入列的任务并且开始执行之。为了协调二者之间的互动过程,我们采用了POSIX标准下的多种工具包括但不限于**互斥锁(mutex)**,**条件变量(cond_varible)**还有专门管理计数值变化情况的一组叫做**信号灯(semaphores)** 的特殊整型变量。
#### 特别注意点:
- `enqueue()` 函数检查了是否超过了允许存储的最大长度限制 (`MAX_QUEUE_SIZE`);
- 使用 **semaphores** 实现对于可用空间数量的有效追踪;
- 在服务端处于完全闲置阶段期间采用的是较轻便省电的方式—就是让其挂起而不是反复查询当前状况(busy-waiting),依靠 POSIX 提供的基础组件实现这一点。
阅读全文
相关推荐
















