IO第5天
- 互联网
- 2025-07-21 19:11:47

1.有三个线程,ID号分别为ABC,且每个线程中都在循环打印自己的ID。要求打印的结果为ABC。
用同步互斥方法控制先后顺序
用条件变量结合互斥锁来实现
#include <myhead.h> pthread_t pid1,pid2, pid3; pthread_cond_t cid1,cid2,cid3; pthread_mutex_t mid; int flag=0; void* pthreadA(void* arg){ //flag:0时运行 while(1){ //上锁 pthread_mutex_lock(&mid); //判断是否应该解锁退出并休眠 if(flag!=0){ pthread_cond_wait(&cid1,&mid); } //临界区 printf("A"); //修改下一个访问条件 flag=1; //唤醒其他线程 pthread_cond_signal(&cid2); //解锁 pthread_mutex_unlock(&mid); } pthread_exit(NULL); } void* pthreadB(void* arg){ //flag:1时运行 while(1){ //上锁 pthread_mutex_lock(&mid); //判断是否符合条件 if(flag!=1){ pthread_cond_wait(&cid2,&mid); } //临界值 printf("B"); //修改下一个访问条件 flag=2; //唤醒其他线程 pthread_cond_signal(&cid3); //解锁 pthread_mutex_unlock(&mid); } pthread_exit(NULL); } void* pthreadC(void* arg){ //flag:2时运行 while(1){ //上锁 pthread_mutex_lock(&mid); //判断是否符合条件 if(flag!=2){ pthread_cond_wait(&cid3,&mid); } //临界值 printf("C"); //修改下一个访问条件 flag=0; //唤醒其他线程 pthread_cond_signal(&cid1); //解锁 pthread_mutex_unlock(&mid); } pthread_exit(NULL); } int main(int argc, const char *argv[]) { //创建互斥锁 pthread_mutex_init(&mid,NULL); //创建条件变量 pthread_cond_init(&cid1,NULL); pthread_cond_init(&cid2,NULL); pthread_cond_init(&cid3,NULL); //创建线程 if((pthread_create(&pid1,NULL,pthreadA,NULL))<0){ printf("create pthreadA fail\n"); return -1; } if((pthread_create(&pid2,NULL,pthreadB,NULL))<0){ printf("create pthreadB fail\n"); return -1; } if((pthread_create(&pid3,NULL,pthreadC,NULL))<0){ printf("create pthreadC fail\n"); return -1; } //回收线程 pthread_join(pid1,NULL); pthread_join(pid2,NULL); pthread_join(pid3,NULL); //销毁条件变量 pthread_cond_destroy(&cid1); pthread_cond_destroy(&cid2); pthread_cond_destroy(&cid3); //销毁锁 pthread_mutex_destroy(&mid); return 0; } 2.实现AB进程对话A进程发送一句话后,B进程接收到打印。然后B进程发送一句话,A进程接收后打印b。
重复上述步骤。直到AB接收或者发送完quit后,结束AB进程
chatA
#include <myhead.h> int main(int argc, const char *argv[]) { umask(0); //创建有名管道文件 if(mkfifo("./fifo",0664)<0){ if(errno!=17){ perror("mkfifo"); return -1; } } printf("mkfifo success\n"); char buf[128]=""; ssize_t res=0; while(1){ //写打开管道文件 int fd=open("./fifo",O_WRONLY); if(fd<0){ perror("open"); return -1; } bzero(buf,sizeof(buf)); //获取信息 fgets(buf,sizeof(buf),stdin); buf[strlen(buf)-1]='\0'; //写入管道文件 if(write(fd,buf,sizeof(buf))<0){ perror("write"); return -1; } printf("发送成功\n"); if(strcmp(buf,"quit")==0){ break; } close(fd); //读打开管道文件 fd=open("./fifo",O_RDONLY); if(fd<0){ perror("open"); return -1; } res=read(fd,buf,sizeof(buf)); if(res<0){ perror("read"); return -1; }else if(0==res){ printf("对方进程结束\n"); break; } printf("B:%s\n",buf); if(strcmp(buf,"quit")==0){ break; } close(fd); } return 0; }chatB
#include <myhead.h> int main(int argc, const char *argv[]) { umask(0); if(mkfifo("./fifo",0664)<0){ if(errno!=17){ perror("mkfifo"); return -1; } } printf("mkfifo success\n"); char buf[128]=""; int res=0; while(1){ //读打开管道文件 int fd=open("./fifo",O_RDONLY); if(fd<0){ perror("open"); return -1; } bzero(buf,sizeof(buf)); res=read(fd,buf,sizeof(buf)); if(res<0){ perror("read"); return -1; } if(res==0){ printf("对方进程结束\n"); break; } printf("A:%s\n",buf); if(strcmp(buf,"quit")==0){ break; } close(fd); //写 fd=open("./fifo",O_WRONLY); if(fd<0){ perror("open"); return -1; } bzero(buf,sizeof(buf)); fgets(buf,sizeof(buf),stdin); buf[strlen(buf)-1]='\0'; if(write(fd,buf,sizeof(buf))<0){ perror("write"); return -1; } printf("发送成功\n"); if(strcmp(buf,"quit")==0){ break; } close(fd); } return 0; }上一篇
AXure的情景交互