1、利用两个线程实现一个线程读文件,另一个把读的内容输出的终端
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <semaphore.h>
sem_t sem1,sem2;
char c;
off_t size;
void * callBack1(void *arg)
{
for(int i=0;i<size;i++){
sem_wait(&sem1);
//printf("%c",c);
if(write(1, &c, sizeof(c)) < 0)
{
perror("write");
return NULL;
}
//printf("打印中\n");
sem_post(&sem2);
}
return NULL;
}
void * callBack2(void *arg)
{
while(1){
sem_wait(&sem2);
ssize_t ret=read(*(int*)arg,&c,sizeof(c));
if(ret<0)
{
perror("read");
return NULL;
}
else if(ret==0){
break;
}
sem_post(&sem1);
}
sem_post(&sem1);
return NULL;
}
int main(int argc, const char *argv[])
{
int fd=open("./mutex1.c",O_RDONLY);
if(fd<0){
perror("open");
return -1;
}
size=lseek(fd,0,SEEK_END);
lseek(fd,0,SEEK_SET);
pthread_t tid1,tid2;
if(sem_init(&sem1,0,0)<0)
{
perror("sem_init");
return -1;
}
if(sem_init(&sem2,0,1)<0)
{
perror("sem_init");
return -1;
}
if(pthread_create(&tid1,NULL,callBack1,&fd)!=0){
fprintf(stderr,"pthread_create failed line:%d\n",__LINE__);
return -1;
}
pthread_detach(tid2);
if(pthread_create(&tid2,NULL,callBack2,&fd)!=0){
fprintf(stderr,"pthread_create failed line:%d\n",__LINE__);
return -1;
}
pthread_join(tid1,NULL);
sem_destroy(&sem1);
sem_destroy(&sem2);
return 0;
}
2、现有ID号为abc的三个线程,每个线程的任多都是循环印自己id号,要求打的顺序为abc
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <semaphore.h>
sem_t sem1,sem2,sem3;
void * callBack1(void *arg)
{
while(1){
sem_wait(&sem1);
printf("a");
sem_post(&sem2);
}
return NULL;
}
void * callBack2(void *arg)
{
while(1){
sem_wait(&sem2);
printf("b");
sem_post(&sem3);
}
return NULL;
}
void * callBack3(void *arg)
{
while(1){
sem_wait(&sem3);
printf("c\n");
sem_post(&sem1);
}
return NULL;
}
int main(int argc, const char *argv[])
{
pthread_t tid1,tid2,tid3;
if(sem_init(&sem1,0,1)<0)
{
perror("sem_init");
return -1;
}
if(sem_init(&sem2,0,0)<0)
{
perror("sem_init");
return -1;
}
if(sem_init(&sem3,0,0)<0)
{
perror("sem_init");
return -1;
}
if(pthread_create(&tid1,NULL,callBack1,NULL)!=0){
fprintf(stderr,"pthread_create failed line:%d\n",__LINE__);
return -1;
}
if(pthread_create(&tid2,NULL,callBack2,NULL)!=0){
fprintf(stderr,"pthread_create failed line:%d\n",__LINE__);
return -1;
}
if(pthread_create(&tid3,NULL,callBack3,NULL)!=0){
fprintf(stderr,"pthread_create failed line:%d\n",__LINE__);
return -1;
}
pthread_detach(tid2);
pthread_detach(tid3);
pthread_join(tid1,NULL);
sem_destroy(&sem1);
sem_destroy(&sem2);
sem_destroy(&sem3);
return 0;
}
用条件变量实现
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <semaphore.h>
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond=PTHREAD_COND_INITIALIZER;
int flag=0;
void * callBack1(void *arg)
{
while(1){
pthread_mutex_lock(&mutex);
if(flag!=0){
pthread_cond_wait(&cond,&mutex);
}
else{
printf("a");
flag=1;
}
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
return NULL;
}
void * callBack2(void *arg)
{
while(1){
pthread_mutex_lock(&mutex);
if(flag!=1){
pthread_cond_wait(&cond,&mutex);
}
else{
printf("b");
flag=2;
}
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
return NULL;
}
void * callBack3(void *arg)
{
while(1){
pthread_mutex_lock(&mutex);
if(flag!=2){
pthread_cond_wait(&cond,&mutex);
}
else{
printf("c\n");
flag=0;
}
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
return NULL;
}
int main(int argc, const char *argv[])
{
pthread_t tid1,tid2,tid3;
if(pthread_create(&tid1,NULL,callBack1,NULL)!=0){
fprintf(stderr,"pthread_create failed line:%d\n",__LINE__);
return -1;
}
if(pthread_create(&tid2,NULL,callBack2,NULL)!=0){
fprintf(stderr,"pthread_create failed line:%d\n",__LINE__);
return -1;
}
if(pthread_create(&tid3,NULL,callBack3,NULL)!=0){
fprintf(stderr,"pthread_create failed line:%d\n",__LINE__);
return -1;
}
pthread_detach(tid2);
pthread_detach(tid3);
pthread_join(tid1,NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}