一、思维导图
二、习题
1> 将互斥机制的代码实现
#include <myhead.h>//定义一个全局变量
char str[128]="我是一个全局字符串数组";
//1、创建一个互斥锁变量
pthread_mutex_t mutex;//线程1
void *pth1(void *arg)
{//上锁pthread_mutex_lock(&mutex);//对全局变量进行操作strcat(str,",我现在在线程1");//输出printf("我是线程1输出:%s\n",str);//解锁pthread_mutex_unlock(&mutex);
}//线程2
void *pth2(void *arg)
{//上锁pthread_mutex_lock(&mutex);//对全局变量进行操作strcpy(str,",我现在在线程2");printf("我是线程2输出:%s\n",str);//解锁pthread_mutex_unlock(&mutex);
}int main(int argc, const char *argv[])
{//2、初始化所资源pthread_mutex_init(&mutex,NULL);//定义线程号pthread_t tid1=-1;pthread_t tid2=-1;//创建分支线程1 if(pthread_create(&tid1,NULL,pth1,NULL)!=0){printf("ERROR");return -1;}//创建分支线程2if(pthread_create(&tid2,NULL,pth2,NULL)!=0){printf("ERROR");return -1;}pthread_join(tid1,NULL);pthread_join(tid2,NULL);printf("程序结束\n");//销毁互斥锁pthread_mutex_destroy(&mutex);return 0;
}
2> 将无名信号量的代码实现
#include <myhead.h>//定义无名信号量
sem_t sem;//线程1
void *pth1(void *arg)
{int n=5;while(n--){sleep(1);//输出printf("我是特斯拉顶级销售,谁要买车!!!\n");sem_post(&sem);}pthread_exit(NULL);
}//线程2
void *pth2(void *arg)
{int n=5;while(n--){sem_wait(&sem);printf("我要买车\n");}pthread_exit(NULL);
}int main(int argc, const char *argv[])
{//初始化无名信号量sem_init(&sem,0,0);//定义线程号pthread_t tid1=-1;pthread_t tid2=-1;//创建分支线程1 if(pthread_create(&tid1,NULL,pth1,NULL)!=0){printf("ERROR");return -1;}//创建分支线程2if(pthread_create(&tid2,NULL,pth2,NULL)!=0){printf("ERROR");return -1;}pthread_join(tid1,NULL);pthread_join(tid2,NULL);printf("程序结束\n");//销毁无名信号量sem_destroy(&sem);return 0;
}
ubuntu@ubu
3> 将条件变量的代码实现
#include <myhead.h>//1、定义
pthread_cond_t cond;//1、定义互斥锁
pthread_mutex_t mutex;//线程1
void *pth1(void *arg)
{int num=5;while(num--){sleep(1);printf("%#lx:我是特斯拉顶级销售\n",pthread_self());//3、唤醒一个消费者pthread_cond_signal(&cond);}pthread_exit(NULL);
}//线程2
void *pth2(void *arg)
{//3、上锁pthread_mutex_lock(&mutex);//4、等待条件变量pthread_cond_wait(&cond,&mutex);printf("我是%#lx:我要来买车\n",pthread_self());//4、解锁pthread_mutex_unlock(&mutex);pthread_exit(NULL);
}int main(int argc, const char *argv[])
{//2、初始化条件变量pthread_cond_init(&cond,NULL);//2、初始化互斥锁pthread_mutex_init(&mutex,NULL);//定义线程号pthread_t tid1,tid2,tid3,tid4,tid5,tid6;//创建分支线程1 if(pthread_create(&tid1,NULL,pth1,NULL)!=0){printf("ERROR");return -1;}//创建分支线程2if(pthread_create(&tid2,NULL,pth2,NULL)!=0){printf("ERROR");return -1;}//创建分支线程3if(pthread_create(&tid3,NULL,pth2,NULL)!=0){printf("ERROR");return -1;}//创建分支线程4if(pthread_create(&tid4,NULL,pth2,NULL)!=0){printf("ERROR");return -1;}//创建分支线程5if(pthread_create(&tid5,NULL,pth2,NULL)!=0){printf("ERROR");return -1;}//创建分支线程6if(pthread_create(&tid6,NULL,pth2,NULL)!=0){printf("ERROR");return -1;}pthread_join(tid1,NULL);pthread_join(tid2,NULL);pthread_join(tid3,NULL);pthread_join(tid4,NULL);pthread_join(tid5,NULL);pthread_join(tid6,NULL);printf("程序结束\n");//释放互斥锁pthread_mutex_destroy(&mutex);//释放条件变量pthread_cond_destroy(&cond);return 0;
}
4> 将无名管道的代码实现
#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);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){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("error");return -1;}return 0;
}
5> 将有名管道的代码实现
#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;
}
6> 使用有名管道完成两个进程的相互通信(提示:可以使用多进程或多线程完成)
#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;
}
#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;
}
#include <myhead.h>
int main(int argc, const char *argv[])
{if((mkfifo("./MYFIFO1",0664))==-1){perror("error");return -1;}//阻塞getchar();system("rm MYFIFO1");return 0;
}
#include <myhead.h>
int main(int argc, const char *argv[])
{if((mkfifo("./MYFIFO2",0664))==-1){perror("error");return -1;}//阻塞getchar();system("rm MYFIFO2");return 0;
}