1.
逆置打印
#include <head.h>
char str[] = "1234567";
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
int flag = 0;
void* inversion(void* arg){
while(1){
//printf("%s __%d__\n",str,__LINE__);测试
pthread_mutex_lock(&mutex);
if(0 != flag){
pthread_cond_wait(&cond,&mutex);
}
int i = 0, j = strlen(str)-1;
while(i < j){//逆置
*(str+i) = *(str+i) + *(str+j);
*(str+j) = *(str+i) - *(str+j);
*(str+i) = *(str+i) - *(str+j);
i++;
j--;
}
flag = 1;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void* show(void* arg){
while(1){
pthread_mutex_lock(&mutex);
if(1 != flag){
pthread_cond_wait(&cond,&mutex);
}
printf("%s\n",str);
flag = 0;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
int main(int argc,const char * argv[])
{
pthread_mutex_init(&mutex,NULL);
pthread_cond_init(&cond,NULL);
//创建线程
pthread_t tid,tid1;
if(pthread_create(&tid,NULL,inversion,NULL) != 0){
printf("pthread_create error\n");
return -1;
}
if(pthread_create(&tid1,NULL,show,NULL) != 0){
printf("pthread_create error\n");
return -1;
}
//接收线程结束
pthread_join(tid,NULL);
pthread_join(tid1,NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}
2.cat的实现
#include <head.h>
pthread_mutex_t mutex;
pthread_cond_t cond;
//全局一个flag和一个数组
int flag = 0;
char buf[128] ="";
int res = 0;
void* Read(void* arg){
int fd = *((int*)arg);
while(1){
pthread_mutex_lock(&mutex);
if(0 != flag){//阻塞的条件
pthread_cond_wait(&cond,&mutex);
}
memset(buf,0,sizeof(buf));
res = read(fd,buf,sizeof(buf)-1);
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(void* arg){
while(1){
pthread_mutex_lock(&mutex);
if(1 != flag){//阻塞的条件
pthread_cond_wait(&cond,&mutex);
}
if(0 == res ){
break;
}
//输出到终端
printf("%s",buf);
fflush(stdout);
flag = 0;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
int main(int argc,const char * argv[])
{
int fd;
//打开文件
if((fd = open("/home/ubuntu/xwf/01_vacation/togethercopy3.c",O_RDONLY)) < 0){
perror("open");
printf("__%d__\n",__LINE__);
return -1;
}
//锁和条件的初始化
pthread_mutex_init(&mutex,NULL);
pthread_cond_init(&cond,NULL);
//创建两个线程
pthread_t tid,tid1;
if(pthread_create(&tid,NULL,Read,&fd) != 0){
printf("pthread_create error\n");
return -1;
}
if(pthread_create(&tid1,NULL,Write,NULL) != 0){
printf("pthread_create error\n");
return -1;
}
//阻塞等待
pthread_join(tid,NULL);
pthread_join(tid1,NULL);
//销毁锁和
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}
3.循环打印abc的tid号
#include <head.h>
sem_t sem,sem1,sem2;
void* A(void* arg){//打印A
while(1){
sem_wait(&sem);
printf("%ld\n",pthread_self());
//sleep(1);
sem_post(&sem1);
}
pthread_exit(NULL);
}
void* B(void* arg){//打印B
while(1){
sem_wait(&sem1);
printf("%ld\n",pthread_self());
//sleep(1);
sem_post(&sem2);
}
pthread_exit(NULL);
}
void* C(void* arg){//打印C
while(1){
sem_wait(&sem2);
printf("%ld\n",pthread_self());
//sleep(1);
sem_post(&sem);
}
pthread_exit(NULL);
}
int main(int argc,const char * argv[])
{
//创建三个信号灯
if(sem_init(&sem,0,1) > 0){
perror("sem_init");
printf("__%d__",__LINE__);
return -1;
}
if(sem_init(&sem1,0,0) > 0){
perror("sem_init");
printf("__%d__",__LINE__);
return -1;
}
if(sem_init(&sem2,0,0) > 0){
perror("sem_init");
printf("__%d__",__LINE__);
return -1;
}
//创建三个线程
pthread_t tid,tid1,tid2;
if(pthread_create(&tid,NULL,A,NULL) != 0){
printf("pthread_create error\n");
return -1;
}
if(pthread_create(&tid,NULL,B,NULL) != 0){
printf("pthread_create error\n");
return -1;
}
if(pthread_create(&tid,NULL,C,NULL) != 0){
printf("pthread_create error\n");
return -1;
}
//阻塞等待执行
pthread_join(tid,NULL);
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
//关闭三个灯
sem_destroy(&sem);
sem_destroy(&sem1);
sem_destroy(&sem2);
return 0;
}