1.管道实现俩不同进程随时通信
第一个用户
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <pthread.h>
#include <signal.h>
void* my_read(void* arg)
{
int* p=(int*)arg;
char buf[255]={0};
while (1)
{
memset(buf,0,sizeof(buf));
int x=read(*p,buf,sizeof(buf));
if(x==0||strcmp(buf,"quit")==0)
{
printf("对方退出");
kill(getpid(),2);
}
printf("对方说:%s\n",buf);
printf("请输入你说的话:\n");
}
}
void* my_write(void* arg)
{
int* p=(int*)arg;
char buf[255]={0};
while (1)
{
memset(buf,0,sizeof(buf));
printf("请输入你说的话:\n");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0';
write(*p,buf,strlen(buf));
if(strcmp(buf,"quit")==0)
{
kill(getpid(),2);
}
}
}
int main(char argc,char* argv[])
{
int rq=mkfifo("fifo1",0777);//1给2
int re=mkfifo("fifo2",0777);//1收2
if(rq<0||re<0)
{
if(errno!=17)
{
perror("mkfifo");
return -1;
}
}
int res1=open("fifo1",O_WRONLY);
int res2=open("fifo2",O_RDONLY);
if(res1<0||res2<0)
{
perror("open");
return -1;
}
printf("打开了\n");
//char buf[255];
pthread_t tid1,tid2;
int x=pthread_create(&tid1,NULL,my_write,(void*)&res1);
int y=pthread_create(&tid2,NULL,my_read,(void*)&res2);
pthread_join(tid1,NULL);
pthread_detach(tid2);
return 0;
}
第二个用户
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <pthread.h>
#include <signal.h>
void* my_read(void* arg)
{
int* p=(int*)arg;
char buf[255]={0};
while (1)
{
memset(buf,0,sizeof(buf));
int x=read(*p,buf,sizeof(buf));
if(x==0||strcmp(buf,"quit")==0)
{
printf("对方退出\n");
kill(getpid(),2);
}
printf("对方说:%s\n",buf);
printf("请输入你说的话:\n");
}
}
void* my_write(void* arg)
{
int* p=(int*)arg;
char buf[255]={0};
while (1)
{
memset(buf,0,sizeof(buf));
printf("请输入你说的话:\n");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0';
write(*p,buf,strlen(buf));
if(strcmp(buf,"quit")==0)
{
kill(getpid(),2);
}
}
}
int main(char argc,char* argv[])
{
int rq=mkfifo("fifo1",0777);//2收1
int re=mkfifo("fifo2",0777);//2给1
if(rq<0||re<0)
{
if(errno!=17)
{
perror("mkfifo");
return -1;
}
}
int res1=open("fifo1",O_RDONLY);
int res2=open("fifo2",O_WRONLY);
if(res1<0||res2<0)
{
perror("open");
return -1;
}
printf("打开了\n");
//char buf[255];
pthread_t tid1,tid2;
int x=pthread_create(&tid1,NULL,my_write,(void*)&res2);
int y=pthread_create(&tid2,NULL,my_read,(void*)&res1);
pthread_detach(tid2);
pthread_join(tid1,NULL);
return 0;
}
2.信号队列实现随时通信
用户1
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <pthread.h>
#include <signal.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/ipc.h>
//这是一个发送接受
struct msgbuf//需要的结构体
{
long mtype;
char mtext[128];
};
void* send(void* arg)//把消息发给对方,参数是队列号的地址
{
struct msgbuf buf;
int* p=(int*)arg;//使用指针存储队列号地址
while (1)
{
printf("请输入数据\n");
buf.mtype=1;//给对方发类型为1点数据包
fgets(buf.mtext,sizeof(buf.mtext),stdin);//定义数据消息准备发出去
buf.mtext[strlen(buf.mtext)-1]='\0';//因为最后一位总是'\0',所以也可以代替字符串清空操作
msgsnd(*p,(void*)&buf,sizeof(buf.mtext),0);//把定义好的结构体包发送,这个包有类型,有字符串数据
if(strcmp(buf.mtext,"quit")==0)//如果消息是quit,先发送给对面,然后自己杀死自己进程
{
kill(getpid(),2);
}
}
}
void* recive(void* arg)
{
struct msgbuf buf;
int* p=(int*)arg;//队列号地址指针
while (1)
{
memset(buf.mtext,0,sizeof(buf.mtext));//读取钱清空字符串
msgrcv(*p,(void*)&buf,sizeof(buf.mtext),2,0);//只接受类型为2的数据包,这个类型的数据包是对方给的,按顺序取出正确类型的数据包
if(strcmp(buf.mtext,"quit")==0)//如果接收到quit,说明对面离开了
{
printf("对方退出");
kill(getpid(),2);//杀死自己,收到对面quit,只剩下自己,只能也退出了
}
printf("对方的消息: %s\n",buf.mtext);
printf("请输入你的消息\n");
}
}
int main(char argc,char* argv[])
{
key_t key=ftok("./",89);
if(key<0)
{
perror("ftok");
return -1;
}
int m=msgget(key,IPC_CREAT|0777);
if(m<0)
{
perror("msgget");
return -1;
}
pthread_t tid1,tid2;
int res1=pthread_create(&tid1,NULL,send,(void*)&m);
int res2=pthread_create(&tid2,NULL,recive,(void*)&m);
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
return 0;
}
用户2
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <pthread.h>
#include <signal.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/ipc.h>
struct msgbuf
{
long mtype;
char mtext[128];
};
void* send(void* arg)
{
struct msgbuf buf;
int* p=(int*)arg;
while (1)
{
printf("请输入数据\n");
buf.mtype=2;
fgets(buf.mtext,sizeof(buf.mtext),stdin);
buf.mtext[strlen(buf.mtext)-1]='\0';
msgsnd(*p,(void*)&buf,sizeof(buf.mtext),0);
if(strcmp(buf.mtext,"quit")==0)
{
kill(getpid(),2);
}
}
}
void* recive(void* arg)
{
struct msgbuf buf;
int* p=(int*)arg;
while (1)
{
memset(buf.mtext,0,sizeof(buf.mtext));
msgrcv(*p,(void*)&buf,sizeof(buf.mtext),1,0);
if(strcmp(buf.mtext,"quit")==0)
{
printf("对方退出");
kill(getpid(),2);
}
printf("对方的消息: %s\n",buf.mtext);
printf("请输入你的消息\n");
}
}
int main(char argc,char* argv[])
{
key_t key=ftok("./",89);
if(key<0)
{
perror("ftok");
return -1;
}
int m=msgget(key,IPC_CREAT|0777);
if(m<0)
{
perror("msgget");
return -1;
}
pthread_t tid1,tid2;
int res1=pthread_create(&tid1,NULL,send,(void*)&m);
int res2=pthread_create(&tid2,NULL,recive,(void*)&m);
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
return 0;
}
互相通信运行