2024/4/18作业

 信号队列实现随时通信

#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;
}
                                                                                                                                      
                                                                                                                                      
#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;                                              
}

 

 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值