1.使用有名管道完成兩個進程的相互通信
send.c代碼如下:
#include <myhead.h>int main(int argc, const char *argv[])
{pid_t pid=fork();if(pid>0){//父進程//從管道1中讀取數據int fd=-1;if((fd=open("./mkfifo1",O_RDONLY))==-1){perror("open error");return -1;}char rbuf[128]="";while(1){bzero(rbuf,sizeof(rbuf));// printf("請輸入>>>:");// fgets(wbuf,sizeof(wbuf),stdin);// wbuf[strlen(wbuf)-1]=0;read(fd,rbuf,sizeof(rbuf));if(strcmp(rbuf,"quit")==0){break;}printf("從程序A中讀取的數據:%s\n",rbuf);}close(fd);}else if(pid==0){//子進程 //向管道2中寫入數據int fd1=-1;if((fd1=open("./mkfifo2",O_WRONLY))==-1){perror("open error");return -1;}char wbuf[128]="";while(1){bzero(wbuf,sizeof(wbuf));// printf("請輸入>>>:");fgets(wbuf,sizeof(wbuf),stdin);wbuf[strlen(wbuf)-1]=0;write(fd1,wbuf,sizeof(wbuf));if(strcmp(wbuf,"quit")==0){break;}// printf("程序B發送的消息:%s\n",rbuf);}close(fd1);}else{perror("fork error");return -1;}return 0;
}
recv.c代碼如下:
#include <myhead.h>int main(int argc, const char *argv[])
{pid_t pid=fork();if(pid>0){//父進程//向管道1中寫入數據int fd=-1;if((fd=open("./mkfifo1",O_WRONLY))==-1){perror("open error");return -1;}char wbuf[128]="";while(1){bzero(wbuf,sizeof(wbuf));// printf("請輸入>>>:");fgets(wbuf,sizeof(wbuf),stdin);wbuf[strlen(wbuf)-1]=0;write(fd,wbuf,sizeof(wbuf));if(strcmp(wbuf,"quit")==0){break;}}close(fd);}else if(pid==0){//子進程 //從管道2中讀取數據int fd1=-1;if((fd1=open("./mkfifo2",O_RDONLY))==-1){perror("open error");return -1;}char rbuf[128]="";while(1){bzero(rbuf,sizeof(rbuf));read(fd1,rbuf,sizeof(rbuf));if(strcmp(rbuf,"quit")==0){break;}printf("程序B發送的消息:%s\n",rbuf);}close(fd1);}else{perror("fork error");return -1;}return 0;
}
create.c代碼如下:
#include <myhead.h>int main(int argc, const char *argv[])
{if(mkfifo("./mkfifo1",0664)==-1){perror("mkfifo error");return -1;}if(mkfifo("./mkfifo2",0664)==-1){perror("mkfifo error");return -1;}getchar();system("rm mkfifo1");system("rm mkfifo2");return 0;
}
運行結果:
2.關于互斥機制的代碼實現
#include <myhead.h>
int num=200;//定義一個鎖資源
pthread_mutex_t mutex;void *task1(void *arg)
{//對訪問的臨界資源進行上鎖pthread_mutex_lock(&mutex);num=120;printf("task1 num=%d\n",num);//解鎖pthread_mutex_unlock(&mutex);pthread_exit(NULL);
}void *task2(void *arg)
{//對訪問的臨界資源進行上鎖pthread_mutex_lock(&mutex);sleep(1);num++;printf("task2 num=%d\n",num);pthread_mutex_unlock(&mutex);pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{//初始化鎖pthread_mutex_init(&mutex,NULL);pthread_t tid1,tid2;if(pthread_create(&tid1,NULL,task1,NULL)!=0){perror("pthread_create error");return -1;}if(pthread_create(&tid2,NULL,task2,NULL)!=0){perror("pthread_create error");return -1;}pthread_join(tid1,NULL);pthread_join(tid2,NULL);//釋放鎖資源pthread_mutex_destroy(&mutex);return 0;
}
運行結果:
3.無名信號量的代碼實現如下 :
#include <myhead.h>
//實現無名信號量
//定義無名信號量
sem_t sem1,sem2,sem3;void *task1(void *arg)
{int num=5;while(num--){sem_wait(&sem1);printf("A");sem_post(&sem2);}pthread_exit(NULL);
}void *task2(void *arg)
{int num=5;while(num--){sem_wait(&sem2);printf("B");sem_post(&sem3);}pthread_exit(NULL);
}void *task3(void *arg)
{int num=5;while(num--){sem_wait(&sem3);printf("C\n");sem_post(&sem1);}pthread_exit(NULL);
}
/**************主程序******************/
int main(int argc, const char *argv[])
{//初始化無名信號量sem_init(&sem1,0,1);sem_init(&sem2,0,0);sem_init(&sem3,0,0);pthread_t tid1,tid2,tid3;if(pthread_create(&tid1,NULL,task1,NULL)!=0){perror("pthread_create error");return -1;}if(pthread_create(&tid2,NULL,task2,NULL)!=0){perror("pthread_create error");return -1;}if(pthread_create(&tid3,NULL,task3,NULL)!=0){perror("pthread_create error");return -1;}pthread_join(tid1,NULL);pthread_join(tid2,NULL);pthread_join(tid3,NULL);return 0;
}
運行結果:
4.實現條件變量代碼如下:
#include <myhead.h>
//定義一個條件變量
pthread_cond_t cond;
//定義一個互斥鎖
pthread_mutex_t mutex;
void *task1(void *arg)
{int num=5;while(num--){sleep(2);printf("tid1生產了一輛奔馳車\n");//喚醒等待隊列中的線程pthread_cond_signal(&cond);}pthread_exit(NULL);
}void *task2(void *arg)
{//上鎖pthread_mutex_lock(&mutex);pthread_cond_wait(&cond,&mutex);printf("消費了一輛車\n");//解鎖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){perror("pthread_create error");return -1;}if(pthread_create(&tid2,NULL,task2,NULL)!=0){perror("pthread_create error");return -1;}if(pthread_create(&tid3,NULL,task2,NULL)!=0){perror("pthread_create error");return -1;}if(pthread_create(&tid4,NULL,task2,NULL)!=0){perror("pthread_create error");return -1;}if(pthread_create(&tid5,NULL,task2,NULL)!=0){perror("pthread_create error");return -1;}if(pthread_create(&tid6,NULL,task2,NULL)!=0){perror("pthread_create 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);//釋放鎖pthread_mutex_destroy(&mutex);//釋放條件變量pthread_cond_destroy(&cond);return 0;
}
運行結果:
5.無名管道代碼實現如下:
#include <myhead.h>int main(int argc, const char *argv[])
{int pipefd[2]={0};if(pipe(pipefd)==-1){perror("pipe error");return -1;}pid_t pid=fork();if(pid>0){//父進程close(pipefd[0]);char wbuf[128]="";while(1){bzero(wbuf,sizeof(wbuf));// printf("請輸入>>>>");fgets(wbuf,sizeof(wbuf),stdin);wbuf[strlen(wbuf)-1]=0;write(pipefd[1],wbuf,sizeof(wbuf));if(strcmp(wbuf,"quit")==0){break;}}close(pipefd[1]);}else if(pid==0){//子進程close(pipefd[1]);char rbuf[128]="";while(1){bzero(rbuf,sizeof(rbuf));read(pipefd[0],rbuf,sizeof(rbuf));if(strcmp(rbuf,"quit")==0){break;}printf("接收的內容為:%s\n",rbuf);}close(pipefd[0]);}else{perror("fork error");return -1;}return 0;
}
運行結果:
6.有名管道代碼實現如下:
create.c
#include <myhead.h>int main(int argc, const char *argv[])
{if(mkfifo("./mk",0664)==-1){perror("mkfifo error");return -1;}getchar();system("rm mk");return 0;
}
send.c
#include <myhead.h>int main(int argc, const char *argv[])
{if(mkfifo("./mk",0664)==-1){perror("mkfifo error");return -1;}getchar();system("rm mk");return 0;
}
ubuntu@ubuntu:home$ cat mkfifo.c
#include <myhead.h>
//使用有名管道完成不同進程之間的通信
int main(int argc, const char *argv[])
{int wfd=-1;if((wfd=open("./mk",O_WRONLY))==-1){perror("open error");return -1;}char wbuf[128]="";while(1){bzero(wbuf,sizeof(wbuf));fgets(wbuf,sizeof(wbuf),stdin);wbuf[strlen(wbuf)-1]=0;write(wfd,wbuf,sizeof(wbuf));if(strcmp(wbuf,"quit")==0){break;}}close(wfd);return 0;
}
recv.c
#include <myhead.h>int main(int argc, const char *argv[])
{int rfd=-1;if((rfd=open("./mk",O_RDONLY))==-1){perror("open error");return -1;}char rbuf[128]="";while(1){bzero(rbuf,sizeof(rbuf));read(rfd,rbuf,sizeof(rbuf));if(strcmp(rbuf,"quit")==0){break;}printf("收到的消息:%s\n",rbuf);}close(rfd);return 0;
}
運行結果: