1> 将互斥机制的代码实现重新敲一遍
#include<myhead.h>
int num=520; //临界资源
//1、创建一个互斥锁变量
pthread_mutex_t mutex;
void *task1(void *arg)
{printf("11111111\n");//3、获取锁资源pthread_mutex_lock(&mutex);num=1314;sleep(3);printf("task1:num=%d\n",num);//4、释放锁资源pthread_mutex_unlock(&mutex);
}
void *task2(void *arg)
{printf("2222222222\n");//获取锁资源pthread_mutex_lock(&mutex);num++; //521sleep(1); //休眠时任务1执行到赋值语句printf("task2:num=%d\n",num);//释放锁资源pthread_mutex_unlock(&mutex);
}
int main(int argc, const char *argv[])
{//2、初始化互斥锁pthread_mutex_init(&mutex,NULL);//创建两个线程pthread_t tid1,tid2;if( pthread_create(&tid1,NULL,task1,NULL) !=0 ){perror("tid1 create error\n");return 0;}if( pthread_create(&tid2,NULL,task2,NULL) !=0 ){perror("tid1 create error\n");return 0;}printf("tid1:%#lx,tid2:%#lx\n",tid1,tid2);//回收线程资源pthread_join(tid1,NULL);pthread_join(tid2,NULL);//销毁锁资源pthread_mutex_destroy(&mutex);return 0;return 0;
}
2> 将无名信号量的代码实现重新敲一遍
#include<myhead.h>
//1、创建无名信号量
sem_t sem;
void *task1(void *arg)
{int num=5;while(num--){sleep(1);printf("我生产了一辆特斯拉\n");sem_post(&sem);}pthread_exit(NULL);}
void *task2(void *arg)
{int num=5;while(num--){sem_wait(&sem);printf("我消费了一辆特斯拉\n");}pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{//2、初始化无名信号量sem_init(&sem,0,0);//创建两个线程分别是生产者和消费者pthread_t tid1,tid2;if(pthread_create(&tid1,NULL,task1,NULL)!=0){printf("tid1 create error\n");return 0;}if(pthread_create(&tid2,NULL,task2,NULL)!=0){printf("tid2 create error\n");return 0;}printf("tid1:%#lx,tid2:%#lx\n",tid1,tid2);//回收线程资源pthread_join(tid1,NULL);pthread_join(tid2,NULL);//释放无名变量sem_destroy(&sem);return 0;
}
3> 将条件变量的代码实现重新敲一遍
#include<myhead.h>
//1、定义条件变量
pthread_cond_t cond;
//定义互斥锁变量
pthread_mutex_t mutex;
void *task1(void *arg)
{int num=5;while(num--){sleep(1);printf("%#lx:生产一辆特斯拉\n",pthread_self());//唤醒一个消费者pthread_cond_signal(&cond);}pthread_exit(NULL);//退出线程}
void *task2(void *arg)
{//上锁pthread_mutex_lock(&mutex);//进入等待队列pthread_cond_wait(&cond,&mutex);printf("%#lx:消费了一辆特斯拉\n",pthread_self());//解锁pthread_mutex_unlock(&mutex);pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{//初始化条件变量pthread_cond_init(&cond,NULL);//初始化互斥锁//创建两个线程,分别是生产者和消费者pthread_t tid1,tid2,tid3,tid4,tid5,tid6;if(pthread_create(&tid1,NULL,task1,NULL)!=0){perror("tid1 create error\n");return 0;}if(pthread_create(&tid2,NULL,task2,NULL)!=0){perror("tid2 create error\n");return 0;}if(pthread_create(&tid3,NULL,task2,NULL)!=0){perror("tid3 create error\n");return 0;}if(pthread_create(&tid4,NULL,task2,NULL)!=0){perror("tid4 create error\n");return 0;}if(pthread_create(&tid5,NULL,task2,NULL)!=0){perror("tid5 create error\n");return 0;}if(pthread_create(&tid6,NULL,task2,NULL)!=0){perror("tid6 create error\n");return 0;}printf("tid1:%#lx,tid2:%#lx,tid4:%#lx,tid5:%#lx,tid:%#lx,tid6:%#lx\n",\tid1,tid2,tid3,tid4,tid5,tid6);//回收线程资源pthread_join(tid1,NULL);pthread_join(tid2,NULL);pthread_join(tid3,NULL);pthread_join(tid4,NULL);pthread_join(tid5,NULL);pthread_join(tid6,NULL);//销毁条件变量pthread_cond_destroy(&cond);//销毁互斥锁pthread_mutex_destroy(&mutex);return 0;
}
4> 将无名管道的代码实现重新敲一遍
#include<myhead.h>
int main(int argc, const char *argv[])
{//创建管道文件,并返回该管道文件的文件描述符int pipefd[2]={0};if(pipe(pipefd)==-1){perror("pipe error\n");return -1;}printf("pipefd[0]=%d,pipefd[1]=%d\n",pipefd[0],pipefd[1]);//创建一个子进程pid_t pid=fork();if(pid>0){//父进程//关闭管道的读端char wbuf[128]="";while(1){bzero(wbuf,sizeof(wbuf));//清空数组内容fgets(wbuf,sizeof(wbuf),stdin);wbuf[strlen(wbuf)-1]=0;//讲数据写入管道文件中write(pipefd[1],wbuf,strlen(wbuf));//对写入的文件进行判断if(strcmp(wbuf,"quit")==0)break;}//关闭写端close(pipefd[1]);wait(NULL);}else if(pid==0){//子进程//关闭写端close(pipefd[1]);char rbuf[128]="";while(1){//清空rbuf内容bzero(rbuf,sizeof(rbuf));//从管道文件中读取数据read(pipefd[0],rbuf,sizeof(rbuf));//输出rbuf的数据printf("父进程传来的数据为:%s\n",rbuf);//对读取的数据进行判断if(strcmp(rbuf,"quit")==0)break;}//关闭读端close(pipefd[0]);exit(EXIT_SUCCESS);}else{perror("fork error");return -1;}return 0;
}
5> 将有名管道的代码实现重新敲一遍
create.c
#include<myhead.h>
int main(int argc, const char *argv[])
{//创造一个管道文件if(mkfifo("./myfifo",0664)==-1){perror("mkfifo error");return -1;}getchar();//阻塞return 0;
}
send.c
ubuntu@ubuntu:~/IO23121/day6$ cat 5work2.c
#include<myhead.h>
int main(int argc, const char *argv[])
{//打开管道文件int wfd=-1;//以只写的形式打开文件if((wfd=open("./myfifo",O_WRONLY))==-1){perror("open error");return -1;}//定义容器char wbuf[128]="";while(1){printf("请输入>>>");fgets(wbuf,sizeof(wbuf),stdin);wbuf[strlen(wbuf)-1]=0;//将数据写入管道write(wfd,wbuf,strlen(wbuf));//判断结果if(strcmp(wbuf,"quit")==0)break;}close(wfd);return 0;
}
recevie.c
#include<myhead.h>
int main(int argc, const char *argv[])
{//打开管道文件int rfd=-1;//以只写的形式打开文件if((rfd=open("./myfifo",O_RDONLY))==-1){perror("open error");return -1;}//定义容器char rbuf[128]="";while(1){//清空数组bzero(rbuf,sizeof(rbuf));//读取管道中的数据read(rfd,rbuf,sizeof(rbuf));//输出结果printf("收到的数据为:%s\n",rbuf);//判断结果if(strcmp(rbuf,"quit")==0)break;}close(rfd);return 0;
}