1.将一个文件打印到终端,类似cat
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
int fd;
ssize_t res;
char buf[20];
int flag = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
void* callBack_read(void* arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
if(flag != 0)
{
pthread_cond_wait(&cond,&mutex);
}
res=read(fd,buf,sizeof(buf));
if(res < 0)
{
perror("read");
return NULL;
}
flag = 1;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
if(0 == res)
{
break;
}
}
pthread_exit(NULL);
}
void* callBack_write(void* arg)
{
ssize_t res_w;
while(1)
{
pthread_mutex_lock(&mutex);
if(flag != 1)
{
pthread_cond_wait(&cond,&mutex);
}
res_w = write(1,buf,res);
if(res_w<0)
{
perror("write");
return NULL;
}
flag = 0;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
if(res_w == 0)
{
break;
}
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
fd = open("sem.c",O_RDONLY);
if(fd<0)
{
perror("open");
return -1;
}
pthread_t tid1,tid2;
if(pthread_create(&tid1,NULL,callBack_read,NULL)<0)
{
fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
return -1;
}
pthread_detach(tid1);
if(pthread_create(&tid2,NULL,callBack_write,NULL)<0)
{
fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
return -1;
}
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
close(fd);
return 0;
}
2.用条件变量实现,ABC三个线程分别打印编号,顺序为ABC
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond2 = PTHREAD_COND_INITIALIZER;
sem_t sem;
int flag = 0;
void* callBackA(void* arg)
{
char a='A';
while(1)
{
pthread_mutex_lock(&mutex);
if(flag!=0)
{
pthread_cond_wait(&cond,&mutex);
}
printf("%c",a);
flag = 1;
pthread_cond_signal(&cond1);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void* callBackB(void* arg)
{
char a='B';
while(1)
{
pthread_mutex_lock(&mutex);
if(flag!=1)
{
pthread_cond_wait(&cond1,&mutex);
}
printf("%c",a);
flag = 2;
pthread_cond_signal(&cond2);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void* callBackC(void* arg)
{
char a='C';
while(1)
{
pthread_mutex_lock(&mutex);
if(flag!=2)
{
pthread_cond_wait(&cond2,&mutex);
}
printf("%c\n",a);
flag = 0;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
pthread_t tida,tidb,tidc;
if(pthread_create(&tida,NULL,callBackA,NULL)<0)
{
fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
return -1;
}
pthread_detach(tida);
if(pthread_create(&tidb,NULL,callBackB,NULL)<0)
{
fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
return -1;
}
pthread_detach(tidb);
if(pthread_create(&tidc,NULL,callBackC,NULL)<0)
{
fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
return -1;
}
pthread_join(tidc,NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
sem_destroy(&sem);
return 0;
}
3.信号变量实现,打印一次倒置一次
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <semaphore.h>
#include <pthread.h>
#include <string.h>
char buf[]="1234567";
sem_t sem;
sem_t sem1;
void* callBash(void* arg)
{
while(1)
{
sem_wait(&sem);
printf("%s\n",buf);
sem_post(&sem1);
}
pthread_exit(NULL);
}
void* callBash1(void* arg)
{
char temp;
int len = strlen(buf);
while(1)
{
sem_wait(&sem1);
for(int i=0;i<len/2;i++)
{
temp=buf[i];
buf[i]=buf[len-i-1];
buf[len-i-1]=temp;
}
sem_post(&sem);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
if(sem_init(&sem,0,1)<0)
{
perror("sem_init");
return -1;
}
if(sem_init(&sem1,0,0)<0)
{
perror("sem1_init");
return -1;
}
pthread_t tid,tid1;
if(pthread_create(&tid,NULL,callBash,NULL)!=0)
{
fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
return -1;
}
pthread_detach(tid);
if(pthread_create(&tid1,NULL,callBash1,NULL)!=0)
{
fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
return -1;
}
pthread_join(tid1,NULL);
sem_destroy(&sem);
sem_destroy(&sem1);
return 0;
}