一、创建两个线程,一个线程打印字符串,另一个线程逆置字符串,要求顺序执行。例如:str[]="1234567",打印1234567后紧跟7654321。
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
char buf[]="1234567";
sem_t sem_pri;
sem_t sem_rev;
void *callBack1(void *arg)
{
while(1)
{
sem_wait(&sem_pri);
fprintf(stderr,"%s\n",buf);
sem_post(&sem_rev);
}
}
void *callBack2(void *arg)
{
while(1)
{
sem_wait(&sem_rev);
char *p = buf;
char *q = p+strlen(buf)-1;
while(p<q)
{
*p=*p^*q;
*q=*p^*q;
*p=*p^*q;
p++;
q--;
}
sem_post(&sem_pri);
}
}
int main(int argc, const char *argv[])
{
if(sem_init(&sem_pri,0,1) < 0)
{
perror("sem_init");
return -1;
}
if(sem_init(&sem_rev,0,0) < 0)
{
perror("sem_init");
return -1;
}
pthread_t tid1,tid2;
if(pthread_create(&tid1,NULL,callBack1,NULL) != 0)
{
fprintf(stderr,"pthread_create failed __%d__",__LINE__);
return -1;
}
if(pthread_create(&tid2,NULL,callBack2,NULL) != 0)
{
fprintf(stderr,"pthread_create failed __%d__",__LINE__);
return -1;
}
while(1)
{}
return 0;
}
二、创建两个线程,其中一个线程读取文件中的数据,另一个线程将读取到的内容打印在终端上。
方法一:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
int fd;
char c;
sem_t sem_r,sem_w;
int res = -1;
void *callBack1(void *arg)
{
while(1)
{
sem_wait(&sem_r);
if(read(fd,&c,1) == 0)
{
res = close(fd);
sem_post(&sem_w);
break;
}
sem_post(&sem_w);
}
printf("线程1准备结束\n");
pthread_exit(NULL);
}
void *callBack2(void *arg)
{
while(1)
{
sem_wait(&sem_w);
if(res == 0)
break;
write(1,&c,1);
sem_post(&sem_r);
}
printf("线程2准备结束\n");
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
fd = open("1.txt",O_RDONLY);
if(fd < 0)
{
perror("open");
return -1;
}
if(sem_init(&sem_r,0,1) < 0)
{
perror("sem_init");
return -1;
}
if(sem_init(&sem_w,0,0) < 0)
{
perror("sem_init");
return -1;
}
pthread_t tid1,tid2;
if(pthread_create(&tid1,NULL,callBack1,NULL) < 0)
{
fprintf(stderr,"pthread_create falied __%d__",__LINE__);
return -1;
}
if(pthread_create(&tid2,NULL,callBack2,NULL) < 0)
{
fprintf(stderr,"pthread_create falied __%d__",__LINE__);
return -1;
}
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
return 0;
}
方法二:
#include <stdio.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int fd;
pthread_t tid1,tid2;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER; //创建一个条件变量
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; //创建一把互斥锁
int flag = 0;
char c;
int res = -1;
void *callBack1(void *arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
if(flag != 0)
{
pthread_cond_wait(&cond,&mutex);
}
if(read(fd,&c,1) == 0)
{
res = close(fd);
flag = 1;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
break;
}
flag = 1;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void *callBack2(void *arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
if(flag != 1)
{
pthread_cond_wait(&cond,&mutex);
}
if(res == 0)
break;
write(1,&c,1);
flag = 0;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
fd = open("1.txt",O_RDONLY);
if(fd < 0)
{
perror("open");
return -1;
}
//创建两个分支线程
if(pthread_create(&tid1,NULL,callBack1,NULL) < 0)
{
fprintf(stderr,"pthread_create failed __%d__",__LINE__);
return -1;
}
if(pthread_create(&tid2,NULL,callBack2,NULL) < 0)
{
fprintf(stderr,"pthread_create faided __%d__",__LINE__);
return -1;
}
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
pthread_cond_destroy(&cond);
pthread_mutex_destroy(&mutex);
return 0;
}