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;
}
运行结果: