多任务互斥与同步(互斥锁, 读写锁, 条件变量, 信号量, 命名信号量): 作业1,2,3,4修订

This commit is contained in:
flykhan 2023-08-22 23:49:28 +08:00
parent 663a501a63
commit 721846a61d
6 changed files with 244 additions and 1 deletions

85
day7/homework/h1_2.c Normal file
View File

@ -0,0 +1,85 @@
#include <stdio.h>
#include <pthread.h>
pthread_mutex_t mutex;
pthread_cond_t condition;
int max_number;
int current_number;
void *print_odd(void *arg)
{
while (1)
{
pthread_mutex_lock(&mutex);
while (current_number % 2 == 0)
{ // 等待偶数线程通知
pthread_cond_wait(&condition, &mutex);
}
if (current_number > max_number)
{ // 达到最大值时退出
pthread_mutex_unlock(&mutex);
break;
}
printf("%d", current_number);
if (current_number != max_number)
printf("");
else if (current_number == max_number)
printf("\n");
fflush(stdout);
current_number++;
pthread_cond_signal(&condition); // 通知偶数线程
pthread_mutex_unlock(&mutex);
}
return NULL;
}
void *print_even(void *arg)
{
while (1)
{
pthread_mutex_lock(&mutex);
while (current_number % 2 != 0)
{ // 等待奇数线程通知
pthread_cond_wait(&condition, &mutex);
}
if (current_number > max_number)
{ // 达到最大值时退出
pthread_mutex_unlock(&mutex);
break;
}
printf("%d", current_number);
if (current_number != max_number)
printf("");
else if (current_number == max_number)
printf("\n");
fflush(stdout);
current_number++;
pthread_cond_signal(&condition); // 通知奇数线程
pthread_mutex_unlock(&mutex);
}
return NULL;
}
int main()
{
printf("请输入指定的最大值: ");
scanf("%d", &max_number);
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&condition, NULL);
current_number = 1;
pthread_t odd_thread, even_thread;
pthread_create(&odd_thread, NULL, print_odd, NULL);
pthread_create(&even_thread, NULL, print_even, NULL);
pthread_join(odd_thread, NULL);
pthread_join(even_thread, NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&condition);
return 0;
}

View File

@ -63,5 +63,7 @@ int main(int argc, char const *argv[])
pthread_join(t1, NULL); pthread_join(t1, NULL);
pthread_join(t2, NULL); pthread_join(t2, NULL);
pthread_rwlock_destroy(&rwlock); // 销毁读写锁
return 0; return 0;
} }

View File

@ -59,5 +59,8 @@ int main(int argc, char const *argv[])
pthread_join(t1, NULL); pthread_join(t1, NULL);
pthread_join(t2, NULL); pthread_join(t2, NULL);
pthread_cond_destroy(&cond); // 销毁条件变量
pthread_mutex_destroy(&mutex); // 销毁互斥锁
return 0; return 0;
} }

View File

@ -36,7 +36,7 @@ int main(int argc, char const *argv[])
printf("现在的 number = %d\n", number); printf("现在的 number = %d\n", number);
sem_destroy(&sem); sem_destroy(&sem); // 销毁信号量
return 0; return 0;
} }

95
day7/homework/h4.c Normal file
View File

@ -0,0 +1,95 @@
/*
, 线, 线线, 线线
*/
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
typedef struct array_s
{
int *arr; // 数组元素头指针
int start_pos; // 起始位置
int end_pos; // 结束位置
pthread_mutex_t *mutex; // 互斥锁
} ARRAY;
void *sort(void *data)
{
ARRAY *array = (ARRAY *)data;
pthread_mutex_lock(array->mutex); // 线程加锁
printf("%ld 线程开始排序\n", pthread_self());
for (int i = array->start_pos; i < array->end_pos; i++)
{
for (int j = array->start_pos; j < array->end_pos - i; j++)
{
if (array->arr[j] > array->arr[j + 1])
{
array->arr[j] ^= array->arr[j + 1];
array->arr[j + 1] ^= array->arr[j];
array->arr[j] ^= array->arr[j + 1];
}
}
}
pthread_mutex_unlock(array->mutex); // 线程解锁
return NULL;
}
int main()
{
int arr[9] = {1, 4, 2, 9, 0, 5, 6, 1, 8};
pthread_mutex_t mutex;
pthread_mutex_init(&mutex, NULL);
ARRAY array1 = {arr, 0, 2, &mutex};
ARRAY array2 = {arr, 3, 5, &mutex};
ARRAY array3 = {arr, 6, 8, &mutex};
pthread_t t1, t2, t3;
pthread_create(&t1, NULL, sort, &array1);
pthread_create(&t2, NULL, sort, &array2);
pthread_create(&t3, NULL, sort, &array3);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
pthread_join(t3, NULL);
// 合并排序结果
int merged[9];
int i = 0;
for (int j = array1.start_pos; j <= array1.end_pos; j++)
{
merged[i++] = arr[j];
}
for (int j = array2.start_pos; j <= array2.end_pos; j++)
{
merged[i++] = arr[j];
}
for (int j = array3.start_pos; j <= array3.end_pos; j++)
{
merged[i++] = arr[j];
}
pthread_mutex_unlock(&mutex); // 解锁
// 创建数组结构和新线程,将合并后的数组进行排序
ARRAY arr4 = {merged, 0, 8, &mutex};
pthread_t t4;
pthread_create(&t4, NULL, sort, &arr4);
pthread_join(t4, NULL);
// 输出排序结果
printf("排序后的数组:");
for (int i = 0; i < 9; i++)
{
printf("%d ", merged[i]);
}
printf("\n");
pthread_mutex_destroy(&mutex); // 销毁互斥锁
return 0;
}

58
day7/homework/test.c Normal file
View File

@ -0,0 +1,58 @@
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <semaphore.h>
//设置办理业务的人数
int num = 5;
//创建信号量
sem_t sem;
//模拟办理业务的过程
void *get_service(void *arg)
{
int id = *((int *)arg);
//信号量成功“减 1”后才能继续执行
if (sem_wait(&sem) == 0)
{
printf("---customer%d 正在办理业务\n", id);
sleep(2);
printf("---customer%d 已办完业务\n", id);
//信号量“加 1”
sem_post(&sem);
}
return 0;
}
int main()
{
int flag, i, j;
//创建 5 个线程代表 5 个人
pthread_t customer[5];
//初始化信号量
sem_init(&sem, 0, 2);
for (i = 0; i < num; i++)
{
flag = pthread_create(&customer[i], NULL, get_service, &i);
if (flag != 0)
{
printf("线程创建失败!\n");
return 0;
}
else
{
printf("customer%d 来办理业务\n", i);
}
sleep(1);
}
for (j = 0; j < num; j++)
{
flag = pthread_join(customer[j], NULL);
if (flag != 0)
{
printf("tid=%d 等待失败!", customer[i]);
return 0;
}
}
sem_destroy(&sem);
return 0;
}