8月6日 IO进程线程作业

1)要求AB进程做通信

  1. A进程发送一句话,B进程接收打印

  2. 然后B进程发送给A进程一句话,A进程接收打印

  3. 重复1,2步骤,直到A进程或者B进程收到quit,退出AB进程;

  4. 2)在第一题的基础上实现,AB进程能够随时收发数据(附加题)

A进程 

#include <stdio.h>                                                                                          
#include <sys/types.h>                                                                                      
#include <sys/stat.h>                                                                                       
#include <sys/ipc.h>                                                                                        
#include <sys/msg.h>                                                                                        
#include <stdlib.h>                                                                                         
#include <string.h>                                                                                         
#include <pthread.h>                                                                                        
#define N 128                                                                                               
struct node                                                                                                 
{                                                                                                           
    long mtype;   //消息类型                                                                                
    char arr[N];                                                                                            
                                                                                                            
};                                                                                                          
key_t key1;                                                                                                 
key_t key2;                                                                                                 
int msqid1;                                                                                                 
int msqid2;                                                                                                 
struct node *p = NULL;                                                                                      
struct node *q = NULL;                                                                                      
void *callback(void *arg)                                                                                   
{                                                                                                           
    while(1)                                                                                                
    {                                                                                                       
        //printf("请输入消息包类型>>");                                                                     
        //scanf("%ld",&q->mtype);                                                                           
        //getchar();    //吸收回车                                                                          
        q->mtype = 1;                                                                                       
        //printf("请输入消息包内容>>");                                                                     
        fgets(q->arr, sizeof(q->arr), stdin);                                                               
        q->arr[strlen(q->arr)-1] = '\0';                                                                    
        msgsnd(msqid2, q, sizeof(q->arr), 0);  //以阻塞方式发送消息包至第二个消息队列中                     
        if(strcasecmp(q->arr,"quit") == 0)                                                                  
            exit(0);                                                                                        
    }                                                                                                       
}                                                                                                           
int main(int argc, const char *argv[])                                                                      
{                                                                                                           
    key1 = ftok("./", 1);        //计算第一个消息队列键值                                                   
    key2 = ftok("./", 2);        //计算第二个消息队列键值                                                   
                                                                                                            
    msqid1 = msgget(key1, IPC_CREAT|0664);  //创建第一个消息队列                                            
    msqid2 = msgget(key2, IPC_CREAT|0664);  //创建第二个消息队列                                            
                                                                                                            
    p = (struct node *)malloc(sizeof(struct node));                                                         
    q = (struct node *)malloc(sizeof(struct node));                                                         
                                                                                                            
    pthread_t tid;                                                                                          
    pthread_create(&tid, NULL, callback, NULL);     //创建一个线程用来向第二个消息队列发送消息              
    while(1)                                                                                                
    {                                                                                                       
        //printf("请输入你要读取的消息包类型>>");                                                           
        //scanf("%ld",&p->mtype);                                                                           
        msgrcv(msqid1, p, sizeof(p->arr), 1, 0);  //以阻塞的方式读取第一个消息队列里的内容                  
        if(strcasecmp(p->arr,"quit") == 0)                                                                  
        {   msgctl(msqid1,IPC_RMID,NULL);         //删除第一个消息队列                                      
            msgctl(msqid2,IPC_RMID,NULL);         //删除第二个消息队列  
            free(p);    //释放指针
            p = NULL;
            free(q);
            q = NULL;                                    
            exit(0);                                                                                        
        }                                                                                                   
        printf("A进程说:%s\n",p->arr);                                                                      
                                                                                                            
    }                                                                                                       
                                                                                                            
                                                                                                            
    return 0;                                                                                               
}                                                                                                           
                                                                                                            

 B进程 

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#define N 128
struct node
{
    long mtype;   //消息类型
    char arr[N];

};
key_t key1;
key_t key2;
int msqid1;
int msqid2;
struct node *p = NULL;
struct node *q = NULL;
void *callback(void *arg)
{
    while(1)
    {
        //printf("请输入消息包类型>>");                                                             
        //scanf("%ld",&q->mtype);
        //getchar();    //吸收回车
        q->mtype = 1;
        //printf("请输入消息包内容>>");
        fgets(q->arr, sizeof(q->arr), stdin);
        q->arr[strlen(q->arr)-1] = '\0';
        msgsnd(msqid1, q, sizeof(q->arr), 0);  //以阻塞方式发送消息包至第一个消息队列中
        if(strcasecmp(q->arr,"quit") == 0)
            exit(0);
    }
}
int main(int argc, const char *argv[])
{
    key1 = ftok("./", 1);        //计算第一个消息队列键值
    key2 = ftok("./", 2);        //计算第二个消息队列键值

    msqid1 = msgget(key1, IPC_CREAT|0664);  //创建第一个消息队列
    msqid2 = msgget(key2, IPC_CREAT|0664);  //创建第二个消息队列

    p = (struct node *)malloc(sizeof(struct node));
    q = (struct node *)malloc(sizeof(struct node));

    pthread_t tid;
    pthread_create(&tid, NULL, callback, NULL);     //创建一个线程用来向第一个队列发送消息
    while(1)
    {
        //printf("请输入你要读取的消息包类型>>");
        //scanf("%ld",&p->mtype);
        msgrcv(msqid2, p, sizeof(p->arr), 1, 0);  //以阻塞的方式读取第二个消息队列里的内容
        if(strcasecmp(p->arr,"quit") == 0)
        {
            msgctl(msqid1,IPC_RMID,NULL);         //删除第一个消息队列
            msgctl(msqid2,IPC_RMID,NULL);         //删除第二个消息队列
            free(p);    //释放指针
            p = NULL;
            free(q);
            q = NULL;                  
            exit(0);
        }
        printf("B进程说:%s\n",p->arr);

    }


    return 0;
}

  1. A进程写入一个整型,在该整型后,写入一个字符串

  2. B进程将共享内存中的整型以及字符串读取出来;

A进程  

#include <stdio.h>                                                        
#include <sys/types.h>                                                    
#include <sys/ipc.h>                                                      
#include <sys/shm.h>                                                      
#include <string.h>                                                       
#define N 32                //宏定义要创建的共享内存的大小                
int main(int argc, const char *argv[])                                    
{                                                                         
    key_t key = ftok("./",1);     //计算键值                              
                                                                          
    int shmid = shmget(key, N, IPC_CREAT|0664); //创建共享内存            
    if(shmid < 0)                                                         
    {                                                                     
        perror("shmid");                                                  
        return -1;                                                        
    }                                                                     
    void *p = shmat(shmid, NULL, 0);  //将共享内存映射到进程的用户空间中  
    int *q = (int *)p;                                                    
    *q = 520;                                                             
    q++;                                                                  
    strcpy((char *)q, "hello world  hello hqyj");                         
                                                                          
    return 0;                                                             
}                                                                         
                                                                          
                                                                          

 B进程 

#include <stdio.h>                                                               
#include <sys/types.h>                                   
#include <sys/ipc.h>                                                             
#include <sys/shm.h>                                                             
#include <string.h>             
#define N 32                //宏定义要创建的共享内存的大小
int main(int argc, const char *argv[])
{
    key_t key = ftok("./",1);     //计算键值

    int shmid = shmget(key, N, IPC_CREAT|0664); //创建共享内存
    if(shmid < 0)
    {
        perror("shmid");
        return -1;
    }
    void *p = shmat(shmid, NULL, 0);  //将共享内存映射到进程的用户空间中
    int *q = (int *)p;
    printf("%d ",*q);
    q++;
    printf("%s\n",(char *)q);
                                                                                 
    return 0;
}
                                                                                 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值