wap建站程序源码,织梦网站百度推送加哪,常德公司做网站,网站域名出售仨demo
一、 一个线程读文件#xff0c;另一个线程将读取的内容输出到终端
1.1 要求
创建两个线程#xff0c;其中一个线程读取文件中的数据#xff0c;另外一个线程将读取到的内容打印到终端上#xff0c;类似实现cat一个文件。 cat数据完毕后#xff0c;要结束两个线…仨demo
一、 一个线程读文件另一个线程将读取的内容输出到终端
1.1 要求
创建两个线程其中一个线程读取文件中的数据另外一个线程将读取到的内容打印到终端上类似实现cat一个文件。 cat数据完毕后要结束两个线程。 提示先读数据读到数据后将数据打印到终端上。
1.2 代码实现
/*
创建两个线程
其中一个线程读取文件中的数据
另外一个线程将读取到的内容打印到终端上
类似实现cat一个文件。cat数据完毕后要结束两个线程。
提示先读数据读到数据后将数据打印到终端上。
*/
#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[])
{// 以只读的方式打开 “abc.c” 文件// 可以换也可以换成外部参数按自己需求int fd open(abc.c, O_RDONLY);// 创建线程1用于读文件并将文件描述符传过去pthread_t tid1;if (pthread_create(tid1, NULL, read_file, fd) ! 0){fprintf(stderr, pthread_create tid1 error\n);return -1;}// 创建线程2用于写到终端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);// 若标志不是0说明不该当前线程执行所以要沉睡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。 要求打印的结果为ABC。
2.2 代码实现
/*
有三个线程ID号分别为ABC且每个线程中都在循环打印自己的ID。要求打印的结果为ABC。
*/
#include my_head.h// 互斥锁
pthread_mutex_t mutex PTHREAD_MUTEX_INITIALIZER;
// 条件变量
pthread_cond_t cond PTHREAD_COND_INITIALIZER;
// 标志:0为A1位B2为C
int flag 0;// 线程A
void *print_A(void *);
// 线程B
void *print_B(void *);
// 线程C
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;
}// 线程A
void *print_A(void *arg)
{while (1){// 上锁pthread_mutex_lock(mutex);// 判断是否是输出A的时机if (flag 0){// 是输出A的时机输出A然后将标志改为1准备输出B之后唤醒其他线程printf(A);flag 1;// 唤醒其他线程pthread_cond_signal(cond);}// 不是输出A的时机else{// 直接进入休眠pthread_cond_wait(cond, mutex);}// 解锁pthread_mutex_unlock(mutex);}// 结束当前线程(虽然死循环不会结束线程但是为了规范点还是写上)pthread_exit(NULL);
}
// 线程B
void *print_B(void *arg)
{while (1){// 上锁pthread_mutex_lock(mutex);// 判断是否是输出B的时机if (flag 1){// 是输出B的时机输出B然后将标志改为2准备输出C之后唤醒其他线程printf(B);flag 2;// 唤醒其他线程pthread_cond_signal(cond);}// 不是输出B的时机else{// 直接进入休眠pthread_cond_wait(cond, mutex);}// 解锁pthread_mutex_unlock(mutex);}// 结束当前线程(虽然死循环不会结束线程但是为了规范点还是写上)pthread_exit(NULL);
}
// 线程C
void *print_C(void *arg)
{while (1){// 上锁pthread_mutex_lock(mutex);// 判断是否是输出C的时机if (flag 2){// 是输出C的时机输出C然后将标志改为0准备输出A之后唤醒其他线程printf(C\n);flag 0;// 唤醒其他线程pthread_cond_signal(cond);}// 不是输出C的时机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 代码实现
/*
要求定义一个全局变量 char buf[] 1234567创建两个线程不考虑退出条件。
A线程循环打印buf字符串
B线程循环倒置buf字符串
即buf中本来存储1234567倒置后buf中存储7654321.B线程中不打印倒置不允许使用辅助数组。要求A线程打印出来的结果只能为 1234567 或者 7654321不允许出现7634521 7234567不允许使用sleep函数分析出现错误的原因。用信号量的方式实现上述代码顺序执行不允许使用flag;
*/
#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[])
{/*这俩信号量第一个初始化为1第二个初始化为0能够保证是先进行线程1即输出线程后进行线程2即逆置线程而在输出线程中先获取信号量1操作执行完再释放信号量2(注意是释放信号量2)因为信号量1的初始值为1所以只会执行一次线程1信号量2的初始值为0在线程1中释放信号量2后变成了1这样可以执行一次线程2在逆置线程中也是同理先获取线程1中释放的信号量2操作执行完再释放信号量1获取线程1释放的信号量2而信号量2只够线程2执行一次在线程2操作执行完后释放信号量1可以让线程1能够再次执行1次如此往复*/// 信号量初始化为1也就是在这个信号量下初始只能走一个线程if (sem_init(sem1, 0, 1) 0){ERR_MSG(sem_init);return -1;}// 信号量初始化为0也就是初始的时候没有可用的信号量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){// P操作if (sem_wait(sem1) 0){ERR_MSG(print_str sem_wait);return NULL;}// 要执行的操作printf(%s\n, buff);// V操作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){// P操作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--;}// V操作if (sem_post(sem1) 0){ERR_MSG(print_str sem_post);return NULL;}}// 结束当前线程(虽然死循环不会结束线程但是为了规范点还是写上)pthread_exit(NULL);
}