仨demo
一、 一个线程读文件,另一个线程将读取的内容输出到终端
1.1 要求
- 创建两个线程,
- 其中一个线程读取文件中的数据,
- 另外一个线程将读取到的内容打印到终端上,
- 类似实现cat一个文件。
- 提示:先读数据,读到数据后将数据打印到终端上。
1.2 代码实现
#include
char buff[16];
ssize_t res;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
int flag = 0;
void *read_file(void *);
void *write_out(void *);
int main(int argc, const char *argv[])
{
int fd = open("abc.c", O_RDONLY);
pthread_t tid1;
if (pthread_create(&tid1, NULL, read_file, &fd) != 0)
{
fprintf(stderr, "pthread_create tid1 error\n");
return -1;
}
pthread_t tid2;
if (pthread_create(&tid2, NULL, write_out, NULL) != 0)
{
fprintf(stderr, "pthread_create tid2 error\n");
return -1;
}
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
return 0;
}
void *read_file(void *arg)
{
int fd = *(int *)arg;
while (1)
{
pthread_mutex_lock(&mutex);
if (0 != flag)
{
pthread_cond_wait(&cond, &mutex);
}
bzero(buff, sizeof(buff));
res = read(fd, buff, sizeof(buff));
if (0 > res)
{
ERR_MSG("read error");
return NULL;
}
else if (0 == res)
{
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
break;
}
flag = 1;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void *write_out(void *arg)
{
while (1)
{
pthread_mutex_lock(&mutex);
if (1 != flag)
{
pthread_cond_wait(&cond, &mutex);
}
if (0 == res)
{
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
break;
}
write(1, buff, res);
flag = 0;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
二、 三个线程打印ABC,每个线程打一个字符,且顺序不变
2.1 要求
- 有三个线程,ID号分别为ABC,且每个线程中都在循环打印自己的ID。
2.2 代码实现
#include
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
int flag = 0;
void *print_A(void *);
void *print_B(void *);
void *print_C(void *);
int main(int argc, const char *argv[])
{
pthread_t tid1;
pthread_t tid2;
pthread_t tid3;
if (pthread_create(&tid1, NULL, print_A, NULL) != 0)
{
fprintf(stderr, "pthread_create A error\n");
return -1;
}
if (pthread_create(&tid2, NULL, print_B, NULL) != 0)
{
fprintf(stderr, "pthread_create A error\n");
return -1;
}
if (pthread_create(&tid3, NULL, print_C, NULL) != 0)
{
fprintf(stderr, "pthread_create A error\n");
return -1;
}
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
pthread_join(tid3, NULL);
return 0;
}
void *print_A(void *arg)
{
while (1)
{
pthread_mutex_lock(&mutex);
if (flag == 0)
{
printf("A");
flag = 1;
pthread_cond_signal(&cond);
}
else
{
pthread_cond_wait(&cond, &mutex);
}
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void *print_B(void *arg)
{
while (1)
{
pthread_mutex_lock(&mutex);
if (flag == 1)
{
printf("B");
flag = 2;
pthread_cond_signal(&cond);
}
else
{
pthread_cond_wait(&cond, &mutex);
}
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void *print_C(void *arg)
{
while (1)
{
pthread_mutex_lock(&mutex);
if (flag == 2)
{
printf("C\n");
flag = 0;
pthread_cond_signal(&cond);
}
else
{
pthread_cond_wait(&cond, &mutex);
}
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
三、 用信号量实现一个线程对字符串逆置,另一线程对字符串输出
3.1 要求
- 要求定义一个全局变量 char buf[] = “1234567”,创建两个线程,不考虑退出条件。
- A线程循环打印buf字符串,
- B线程循环倒置buf字符串,
- 即buf中本来存储1234567,倒置后buf中存储7654321.
- B线程中不打印!!
- 倒置不允许使用辅助数组。
- 要求A线程打印出来的结果只能为 1234567 或者 7654321
- 不允许出现7634521 7234567
- 不允许使用sleep函数
- 用信号量的方式实现上述代码顺序执行,不允许使用flag;
3.2 代码实现
#include
sem_t sem1;
sem_t sem2;
char buff[] = "1234567";
void *print_str(void *arg);
void *turn_str(void *arg);
int main(int argc, const char *argv[])
{
if (sem_init(&sem1, 0, 1) < 0)
{
ERR_MSG("sem_init");
return -1;
}
if (sem_init(&sem2, 0, 0) < 0)
{
ERR_MSG("sem_init");
return -1;
}
pthread_t tid1;
pthread_t tid2;
if (pthread_create(&tid1, NULL, print_str, NULL) != 0)
{
fprintf(stderr, "pthread_create tid1 error\n");
return -1;
}
if (pthread_create(&tid2, NULL, turn_str, NULL) != 0)
{
fprintf(stderr, "pthread_create tid2 error\n");
return -1;
}
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
return 0;
}
void *print_str(void *arg)
{
while (1)
{
if (sem_wait(&sem1) < 0)
{
ERR_MSG("print_str sem_wait");
return NULL;
}
printf("%s\n", buff);
if (sem_post(&sem2) < 0)
{
ERR_MSG("print_str sem_post");
return NULL;
}
}
pthread_exit(NULL);
}
void *turn_str(void *arg)
{
int len = strlen(buff);
while (1)
{
if (sem_wait(&sem2) < 0)
{
ERR_MSG("print_str sem_wait");
return NULL;
}
char *s = buff, *s1 = buff + len - 1;
while (s < s1)
{
(*s) ^= (*s1);
(*s1) ^= (*s);
(*s) ^= (*s1);
s++;
s1--;
}
if (sem_post(&sem1) < 0)
{
ERR_MSG("print_str sem_post");
return NULL;
}
}
pthread_exit(NULL);
}

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135