作业1:信号量的方式,实现打印一次,倒置一次
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <semaphore.h>
char buf[]="1234567";
sem_t sem1,sem2;
void* Output(void* arg) //分支线程1,循环打印
{
while(1)
{
if(sem_wait(&sem1)<0)
{
perror("sem_wait");
break;
}
printf("%s\n",buf);
if(sem_post(&sem2)<0)
{
perror("sem_post(&sem)");
break;
}
}
}
void* Inversion(void* arg) //分支线程2,字符串逆置
{
while(1)
{
if(sem_wait(&sem2)<0)
{
perror("sem_wait");
break;
}
int i=0,j=strlen(buf)-1;
while(i<j)
{
char t=buf[i];
buf[i]=buf[j];
buf[j]=t;
i++;j--;
}
if(sem_post(&sem1)<0)
{
perror("sem_post");
break;
}
}
return NULL;
}
int main(int argc, const char *argv[])
{
//创建信号量
if(sem_init(&sem1,0,1)<0)
{
perror("sem_init");
return -1;
}
if(sem_init(&sem2,0,1)<0)
{
perror("sem_init");
return -1;
}
pthread_t tid1,tid2;
if(pthread_create(&tid1,NULL,Output,NULL)!=0) //创建分支线程1
{
printf("line=%d\n",__LINE__);
printf("pthread_create failed!!!\n");
return -1;
}
if(pthread_create(&tid2,NULL,Inversion,NULL)!=0) //创建分支线程2
{
printf("line=%d\n",__LINE__);
printf("pthread_create failed!!!\n");
return -1;
}
pthread_join(tid2,NULL);
//销毁信号量
sem_destroy(&sem1);
sem_destroy(&sem2);
printf("over\n");
return 0;
}
作业2:文件中的数据打印到终端,类似cat功能
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <fcntl.h>
#include <unistd.h>
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;//初始化互斥锁
pthread_cond_t cond=PTHREAD_COND_INITIALIZER;//初始化条件变量
ssize_t res=0;
char str[50]="";
int ss;
int flag=0;
void* callback1(void* arg)
{
while(1)
{
/********临界区**********/
pthread_mutex_lock(&mutex); //互斥锁
//不是当前线程要访问的时机,让当前线程休眠
if(0!=flag)
{
pthread_cond_wait(&cond,&mutex);
}
bzero(str,sizeof(str));
res=read(ss,str,sizeof(str)); //读文件内容
if(0==res)
{
break;
}
flag=1; //修改运行时机,设置为非0;
//通过制定条件变量唤醒指定线程
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex); //解锁
/*********临界区***********/
}
pthread_exit(NULL);
}
void* callback2(void* arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
if(1!=flag)
{
pthread_cond_wait(&cond,&mutex);
}
for(int i=0;i<res;i++)
{
printf("%c",str[i]);
}
flag=0;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
ss=open("./01.c",O_RDONLY);
if(ss<0)
{
perror("read");
return -1;
}
pthread_t tid1,tid2;
if(pthread_create(&tid1,NULL,callback1,NULL)!=0)
{
printf("pthread_create failed line=%d\n",__LINE__);
return -1;
}
if(pthread_create(&tid2,NULL,callback2,NULL)!=0)
{
printf("pthread_create failed line=%d\n",__LINE__);
return -1;
}
pthread_join(tid1,NULL);
pthread_mutex_destroy(&mutex);//销毁互斥锁
pthread_cond_destroy(&cond);//销毁条件变量
return 0;
}
作业3:三个线程,循环打印自己的id号,顺序为abc
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond1=PTHREAD_COND_INITIALIZER;
pthread_cond_t cond2=PTHREAD_COND_INITIALIZER;
pthread_cond_t cond3=PTHREAD_COND_INITIALIZER;
int flag=0;
void* tid_a(void* arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
if(0!=flag)
{
pthread_cond_wait(&cond1,&mutex);
}
printf("a\n");
flag=1;
pthread_cond_signal(&cond2);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void* tid_b(void* arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
if(1!=flag)
{
pthread_cond_wait(&cond2,&mutex);
}
printf("b\n");
flag=2;
pthread_cond_signal(&cond3);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void* tid_c(void* arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
if(2!=flag)
{
pthread_cond_wait(&cond3,&mutex);
}
printf("c\n");
flag=0;
pthread_cond_signal(&cond1);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
pthread_t a,b,c;
if(pthread_create(&a,NULL,tid_a,NULL)!=0)
{
printf("pthread_create failed ,line=%d\n",__LINE__);
return -1;
}
if(pthread_create(&b,NULL,tid_b,NULL)!=0)
{
printf("pthread_create failed ,line=%d\n",__LINE__);
return -1;
}
if(pthread_create(&c,NULL,tid_c,NULL)!=0)
{
printf("pthread_create failed ,line=%d\n",__LINE__);
return -1;
}
pthread_join(a,NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond1);
pthread_cond_destroy(&cond2);
pthread_cond_destroy(&cond3);
return 0;
}