IO练习
互斥机制练习
#include <myhead.h>
int num = 520;
pthread_mutex_t mutex;
void *task1(void *arg)
{
printf("11111111\n");
pthread_mutex_lock(&mutex);
num = 1314;
sleep(3);
printf("task1:num=%d\n",num);
pthread_mutex_unlock(&mutex);
}
void *task2(void *arg)
{
printf("22222222\n");
pthread_mutex_lock(&mutex);
num++;
sleep(1);
printf("task2:num=%d\n",num);
pthread_mutex_unlock(&mutex);
}
int main(int argc, const char *argv[])
{
pthread_mutex_init(&mutex,NULL);
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);
pthread_mutex_destroy(&mutex);
return 0;
}
效果图
![](https://img-blog.csdnimg.cn/direct/0389cb4312b149d3b41d72225e6ad2ed.png)
无名信号量练习
#include<myhead.h>
//1、创建无名信号量
sem_t sem;
//定义生产者线程
void *task1(void *arg)
{
int num = 5;
while(num --)
{
sleep(1);
printf("我生产了一辆特斯拉\n");
//4、释放资源
sem_post(&sem);
}
pthread_exit(NULL); //退出线程
}
//定义消费者线程
void *task2(void *arg)
{
int num = 5;
while(num--)
{
//3、申请资源
sem_wait(&sem);
printf("我消费了一辆特斯拉\n");
}
pthread_exit(NULL);
}
/************************主程序********************/
int main(int argc, const char *argv[])
{
//2、初始化无名信号量
sem_init(&sem, 0, 0);
//第一个0:表示用于线程的同步
//第二个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;
}
效果图
![](https://img-blog.csdnimg.cn/direct/cd4b0b661d9b472cac36f2bd73b7c89f.png)
条件变量的练习
#include <myhead.h>
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_mutex_init(&mutex, NULL);
pthread_t tid1, tid2, tid3, tid4, tid5, tid6;
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;
}
if(pthread_create(&tid3, NULL, task2, NULL) != 0)
{
printf("tid3 create error\n");
return 0;
}
if(pthread_create(&tid4, NULL, task2, NULL) != 0)
{
printf("tid4 create error\n");
return 0;
}
if(pthread_create(&tid5, NULL, task2, NULL) != 0)
{
printf("tid5 create error\n");
return 0;
}
if(pthread_create(&tid6, NULL, task2, NULL) != 0)
{
printf("tid6 create error\n");
return 0;
}
printf("tid1:%#lx, tid2:%#lx, tid3:%#lx, tid4:%#lx, tid5:%#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;
}
效果图
![](https://img-blog.csdnimg.cn/direct/9a46828d22564e3cb01d29a7a20e6366.png)
无名管道的练习
#include <myhead.h>
int main(int argc, const char *argv[])
{
int pipefd[2]={0};
if(pipe(pipefd)==-1)
{
perror("pipe error");
return -1;
}
printf("pipefd[0]=%d,pipefd[1]=%d\n",pipefd[0],pipefd[1]);
pid_t pid = fork();
if(pid>0)
{
close(pipefd[0]);
char wbuf[128]="";
while(1)
{
bzero(wbuf,sizeof(wbuf));
fgets(wbuf,sizeof(wbuf),stdin);
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)
{
bzero(rbuf,sizeof(rbuf));
read(pipefd[0],rbuf,sizeof(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;
}
效果图
![](https://img-blog.csdnimg.cn/direct/05c7b6653d8f4a39bc256c2b15cf4952.png)
有名管道的练习
创建
#include <myhead.h>
int main(int argc, const char *argv[])
{
if((mkfifo("./MYFIFO",0664))==-1)
{
perror("error");
return -1;
}
//阻塞
getchar();
system("rm MYFIFO");
return 0;
}
发送
#include <myhead.h>
int main(int argc, const char *argv[])
{
int wfd=-1;
if((wfd=open("./MYFIFO",O_WRONLY))==-1)
{
perror("error");
return -1;
}
char str[128]="";
while(1)
{
fgets(str,sizeof(str),stdin);
str[strlen(str)-1]=0;
write(wfd,str,strlen(str));
if(strcmp(str,"quit")==0)
{
break;
}
}
close(wfd);
return 0;
}
接收
#include <myhead.h>
int main(int argc, const char *argv[])
{
int rfd=-1;
if((rfd=open("./MYFIFO",O_RDONLY))==-1)
{
perror("error");
return -1;
}
char str[128]="";
while(1)
{
bzero(str,sizeof(str));
read(rfd,str,sizeof(str));
printf("接受的数据:%s\n",str);
if(strcmp(str,"quit")==0)
{
break;
}
}
close(rfd);
return 0;
}
效果图
![](https://img-blog.csdnimg.cn/direct/077dd069fb4f4e0d9fdbec07d862a209.png)
使用有名管道完成两个进程的相互通信(使用多线程完成)
创建
#include <myhead.h>
int main(int argc, const char *argv[])
{
if(mkfifo("./myfifo1",0664)==-1)
{
perror("mkfifo myfifo1 error");
return -1;
}
if(mkfifo("./myfifo2",0664)==-1)
{
perror("mkfifo myfifo2 error");
return -1;
}
printf("创建管道文件成功\n");
printf("按回车删除管道文件>>>>");
getchar();
system("rm myfifo1");
system("rm myfifo2");
return 0;
}
user1
#include <myhead.h>
//线程1:实现写功能
void *task1(void *arg)
{
int wfd=-1;
if((wfd=open("./myfifo1",O_WRONLY))==-1)
{
perror("error");
}
char str[128]="";
while(1)
{
fgets(str,sizeof(str),stdin);
str[strlen(str)-1]=0;
write(wfd,str,strlen(str));
if(strcmp(str,"quit")==0)
{
break;
}
}
close(wfd);
pthread_exit(NULL);
}
//线程2:实现读功能
void *task2(void *arg)
{
int rfd=-1;
if((rfd=open("./myfifo2",O_RDONLY))==-1)
{
perror("error");
}
char str[128]="";
while(1)
{
bzero(str,sizeof(str));
read(rfd,str,sizeof(str));
printf("慕容二狗:%s\n",str);
if(strcmp(str,"quit")==0)
{
break;
}
}
close(rfd);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
//创建分线程
pthread_t pid1=-1;
pthread_t pid2=-1;
if(pthread_create(&pid1,NULL,task1,0)!=0)
{
printf("ERROR");
return -1;
}
if(pthread_create(&pid2,NULL,task2,0)!=0)
{
printf("ERROR");
return -1;
}
pthread_join(pid1,NULL);
pthread_join(pid2,NULL);
return 0;
}
user2
#include <myhead.h>
//线程1:实现写功能
void *task1(void *arg)
{
int wfd=-1;
if((wfd=open("./myfifo2",O_WRONLY))==-1)
{
perror("error");
}
char str[128]="";
while(1)
{
fgets(str,sizeof(str),stdin);
str[strlen(str)-1]=0;
write(wfd,str,strlen(str));
if(strcmp(str,"quit")==0)
{
break;
}
}
close(wfd);
pthread_exit(NULL);
}
//线程2:实现读功能
void *task2(void *arg)
{
int rfd=-1;
if((rfd=open("./myfifo1",O_RDONLY))==-1)
{
perror("error");
}
char str[128]="";
while(1)
{
bzero(str,sizeof(str));
read(rfd,str,sizeof(str));
printf("西门苟剩:%s\n",str);
if(strcmp(str,"quit")==0)
{
break;
}
}
close(rfd);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
//创建分线程
pthread_t pid1=-1;
pthread_t pid2=-1;
//设置无名
if(pthread_create(&pid1,NULL,task1,0)!=0)
{
printf("ERROR");
return -1;
}
if(pthread_create(&pid2,NULL,task2,0)!=0)
{
printf("ERROR");
return -1;
}
pthread_join(pid1,NULL);
pthread_join(pid2,NULL);
return 0;
}
效果图
![](https://img-blog.csdnimg.cn/direct/87878b80f78e4e2293bc860dced6013c.png)