作业1
//临界资源
int fd=open("./work.txt",O_RDONLY);
if(fd<0)
{
perror("open");
return -1;
}
char buf[20]="";
ssize_t res=0;
//创建互斥锁
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
//创建条件变量
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
int flag=0 //0:打印 1:倒置
void* callback1(void* arg)
{
while(1)
{
pthread_mutex_lock; //上锁
if(flag!=0) //不是当前线程访问时机
{
//解开互斥锁,设置一个唤醒条件,等待被唤醒
pthread_cond_wait(&mutex,&wait);
//当被唤醒后,会立即尝试上锁
//上锁成功,则完全被唤醒,从当前位置往后执行
//上锁失败,重新回到cond上继续休眠,等待下一次唤醒
}
//临界区
while(1)
{
bzero(buf,sizeof(buf));
res=read(fd,buf,sizeof(buf));
if(res=0)
break;
}
//临界区
flag=1; //修改访问时机
pthread_cond_signal(&cond); //通过指定的条件变量唤醒对方线程;只要唤醒了,对方线程肯定能上锁成功
pthread_mutex_unlock(&mutex); //解锁
}
pthread_exit(NULL);
}
void* callback2(void* arg)
{
char temp = 0;
while(1)
{
pthread_mutex_lock; //上锁
if(flag!=1) //不是当前线程访问时机
{
//解开互斥锁,设置一个唤醒条件,等待被唤醒
pthread_cond_wait(&mutex,&wait);
//当被唤醒后,会立即尝试上锁
//上锁成功,则完全被唤醒,从当前位置往后执行
//上锁失败,重新回到cond上继续休眠,等待下一次唤醒
}
/************临界区****************/
while(1)
{
write(1,buf,sizeof(buf));
}
/************临界区****************/
close(fd);
flag=0; //修改访问时机
pthread_cond_signal(&cond); //通过指定的条件变量唤醒对方线程;只要唤醒了,对方线程肯定能上锁成功
pthread_mutex_unlock(&mutex); //解锁
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
pthread_t tid1,tid2;
if(pthread_create(&tid1,NULL,callback1,NULL)!=0)
{
fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
return -1;
}
if(pthread_create(&tid2,NULL,callback2,NULL)!=0)
{
fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
return -1;
}
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
//销毁互斥锁
pthread_mutex_destroy(&mutex);
//销毁条件变量
pthread_cond_destroy(&cond);
return 0;
}
作业2
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int flag=0;
void* callback1(void* arg)
{
while(1)
{
if(flag=0)
{
sleep(1);
printf("this is first func %A",*arg);
}
flag=1;
}
pthread_exit(NULL);
}
void* callback2(void* arg)
{
while(1)
{
if(flag=1)
{
sleep(1);
printf("this is first func %B",*arg);
}
flag=2;
}
pthread_exit(NULL);
}
void* callback3(void* arg)
{
while(1)
{
if(flag=2)
{
sleep(1);
printf("this is first func %C",*arg);
}
flag=0;
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
pthread_t A,B,C;
if(pthread_create(&A,NULL,callback1,NULL)!=0)
{
fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
return -1;
}
if(pthread_create(&B,NULL,callback2,NULL)!=0)
{
fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
return -1;
}
if(pthread_create(&C,NULL,callback3,NULL)!=0)
{
fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
return -1;
}
pthread_join(A,NULL);
pthread_join(B,NULL);
pthread_join(C,NULL);
pthread_mutex_destroy(&mutex);
return 0;
}
作业3
char buf[]="1234567";
//V操作
void* callback(void* arg)
{
if(sem_post((sem_t*)arg)<0)
{
perror("sem_post");
return NULL;
}
char temp = 0;
for(int i=0;i<strlen(buf)/2;i++)
{
temp = buf[i];
buf[i] = buf[strlen(buf)-1-i];
buf[strlen(buf)-1-i]=temp;
}
printf("V operation success __%d__\n",__LINE__);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
sem_t sem;
//创建并初始化一个信号灯
if(sem_init(&sem,0,2)<0)
{
perror("sem_init");
return -1;
}
printf("sem_init success\n");
pthread_t tid;
pthread_create(&tid,NULL,callback,&sem);
//P操作
if(sem_wait(&sem)<0)
{
perror("sem_wait");
return -1;
}
printf("%s\n",buf);
printf("P operation success __%d__\n",__LINE__);
//销毁信号灯
sem_destroy(&sem);
return 0;
}
思维导图