仨demo
一、 一个线程读文件,另一个线程将读取的内容输出到终端
1.1 要求
- 创建两个线程,
- 其中一个线程读取文件中的数据,
- 另外一个线程将读取到的内容打印到终端上,
- 类似实现cat一个文件。
- 提示:先读数据,读到数据后将数据打印到终端上。
1.2 代码实现
#include <my_head.h>
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);
}
二、 三个线程打印ABC,每个线程打一个字符,且顺序不变
2.1 要求
- 有三个线程,ID号分别为ABC,且每个线程中都在循环打印自己的ID。
2.2 代码实现
#include <my_head.h>
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);
}
三、 用信号量实现一个线程对字符串逆置,另一线程对字符串输出
3.1 要求
- 要求定义一个全局变量 char buf[] = “1234567”,创建两个线程,不考虑退出条件。
- A线程循环打印buf字符串,
- B线程循环倒置buf字符串,
- 即buf中本来存储1234567,倒置后buf中存储7654321.
- B线程中不打印!!
- 倒置不允许使用辅助数组。
- 要求A线程打印出来的结果只能为 1234567 或者 7654321
- 不允许出现7634521 7234567
- 不允许使用sleep函数
- 用信号量的方式实现上述代码顺序执行,不允许使用flag;
3.2 代码实现
#include <my_head.h>
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);
}