实验六、消息队列数组排序

结合lab5,使用消息队列实现一个数组排序系统。client端负责提交排序数组(int),server端输出排序结果。

>示例

>client端:

```
Enter the length of input array:
5
Enter the array need to be sorted:
3 2 8 4 16
```

>server端:

```
2 3 4 8 16
```

思路:

client首先输入字符串len,然后逐位逐位转成整形,传给server,告知数组长度,
使用getchar,一位一位输入,控制输入格式,忽略所有非数字输入,不传送非数字的字符,数字之间空格隔开,传给server

server一位一位转换为相应个数的数字,然后使用选择排序,排序输出
使用flag,判断数字中是否有0,有0,则退出循环

运行结果:

client端

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<linux/msg.h>
 
#define MAXMSG 512
#define MSGKEY 1314
struct my_msg   //消息队列结构体
{
	long int msg_type;
	int i;
	char text[MAXMSG];
}msg;

int main(int argc, char const *argv[]){
	    char buffer[MAXMSG];
		int msgid = msgget(MSGKEY, 0666|IPC_CREAT);

		printf("msgid = %d!\n",msgid);

		msg.msg_type = 3;

        int flag = 1;
        while(flag){
            memset(buffer,'\0',MAXMSG);
	    	printf("\nEnter the length of input array:");
	    	scanf("%s", buffer);
	    	strcpy(msg.text, buffer);
	    	msgsnd(msgid, &msg, sizeof(msg.text), IPC_NOWAIT);
	    	
	    	int len= 0;
	    	for(auto i = 0; i<strlen(buffer);i++){
	    	    len = len*10 +(buffer[i]-'0');
	    	}
	    	
            memset(buffer,'\0',MAXMSG);
	    	printf("Enter the array need to be sorted:\n");
	    	char c;
	    	for(int i = 0,j=0;i<=len;){ 	
	    	    c = getchar();
	    	    
	    	    if(i!=0&&c <'0'|| c > '9'){
	    	      	i++;
	    	      	if(i==len+1)break;	   	    
	    	        buffer[j++] = ' ';
	    	    }   
	    	   
	    	    
	    	    //is num 0 - 9
	    	    while(c <'0'|| c > '9'){
	    	        c = getchar();    
	    	    }	
	    	    
	    	    //0 exit
	    	    if(i == 0){
	    	        i++;
	    	        if(c == '0') flag = 0;
	    	    }
	    	    else if(buffer[j-1] == ' '&&c == '0') {
	    	        flag = 0;
	    	    }
	    	            
	    	    buffer[j++] = c; 	    
	    	}
	    	
	    	strcpy(msg.text, buffer);	
	    	msgsnd(msgid, &msg, sizeof(msg.text), IPC_NOWAIT);
	    	/*
	    	paixu 
	    	int a[MAXMSG]={0};
	    	for(int i = 0,j=0;i<len;){
		        while(msg.text[j]>='0'&&msg.text[j]<='9'){
		                a[i] = a[i]*10 + msg.text[j++]-'0';
		        }
		        i++;
		        j++;
		    }
		        
		        
		    for(int i = 1; i<len;i++){
		        int min = i-1;
		        for(int j = i;j<len;j++){
		           if(a[j]<a[min]) min = j;
		        } 
		        int temp = a[min];
		        a[min] = a[i-1];
		        a[i-1] = temp;
		    }

		    for(int i = 0; i<len;i++){
		        printf("%d\t",a[i]);
		    }  
		    */  
        }
       // TO-DO
	    msgctl(msgid, IPC_RMID, 0);
	  exit(0);
}

server端

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<linux/msg.h>
 
#define MAXMSG 512
#define MSGKEY 1314

struct my_msg   //消息队列结构体
{
	long int msg_type;
	int i;
	char text[MAXMSG];
}msg;

int main(int argc, char const *argv[]){
		int msgid = msgget(MSGKEY, 0666|IPC_CREAT);
		
		printf("msgid = %d!\n",msgid);
		
		msg.msg_type=3;
        int flag = 1;
        int a[MAXMSG]={0};
        int len = 0; 
        while(flag){
            memset(msg.text,'\0',MAXMSG);
            while(1){
                if(msgrcv(msgid, &msg, sizeof(msg.text), msg.msg_type, IPC_NOWAIT)>0){
                    len = 0;
                    printf("```\nlength of array:\t%s", msg.text);
                    for(int i = 0; msg.text[i]!='\0';i++){
                        len = len*10 + (msg.text[i]-'0');
                    }
                    break;
                }
            }
              
            printf("\nrecv message:");
                
            memset(msg.text,'\0',MAXMSG);
            while(1){         
		        if(msgrcv(msgid, &msg, sizeof(msg.text), msg.msg_type, IPC_NOWAIT)> 0){
		    	    printf("\t%s", msg.text);
		            for(int i = 0,j=0;i<len;){
		                a[i]=0;
		                while(msg.text[j]>='0'&&msg.text[j]<='9'){
		                    //0 exit
	    	                if(a[i] == 0 &&msg.text[j] == '0') {
	    	                    flag = 0;
	    	                }
		                    a[i] = a[i]*10 + msg.text[j++]-'0';
		                }
		                //printf("%d\t",a[i]);
		                i++;
		                j++;//skip space

		            }

		            //select sort
		            for(int i = 1; i<len;i++){
		                int min = i-1;
		                for(int j = i;j<len;j++){
		                    if(a[j]<a[min]) min = j;
		                } 
		                int temp = a[min];
		                a[min] = a[i-1];
		                a[i-1] = temp;
		            }
                
                    //print sort array
                    printf("\nsort array:\t");
		            for(int i = 0; i<len;i++){
		                printf("%d  ",a[i]);
		            }
		            printf("\n```\n\n");
		            break;
		        }	        
            }       
        }

        msgctl(msgid, IPC_RMID, 0);
        
		exit(0);
}

client2

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<linux/msg.h>
 
#define MAXMSG 512
#define MSGKEY 10
struct my_msg   //消息队列结构体
{
	long int msg_type;
	int i;
	char text[MAXMSG];
}msg;

int main(int argc, char const *argv[]){
		int msgid = msgget(MSGKEY, 0666|IPC_CREAT);
		msg.msg_type = 3;
        int flag = 1;
        while(flag){
            memset(msg.text,'\0',MAXMSG);
	    	printf("\nEnter the length of input array:\n");
	    	scanf("%s", msg.text);
	    	msgsnd(msgid, &msg, sizeof(msg.text), IPC_NOWAIT);
	    	
	    	int len= 0;
	    	for(auto i = 0; i<strlen(msg.text);i++){
	    	    len = len*10 +(msg.text[i]-'0');
	    	}
	    	
            memset(msg.text,'\0',MAXMSG);
	    	printf("Enter the array need to be sorted:\n");
	    	char c[MAXMSG];
	    	for(int i = 0,k=0,j=0;i<len;i++){ 	
	    	    scanf("%s",c);
	    	    if(c[0]=='0'){//num is 0 
	    	        flag = 0;
	    	    }
	    	    
	    	    //copy
	    	    for(int j = 0;j<strlen(c);){ 	
                    msg.text[k++] = c[j++];	  
                }
                msg.text[k++] = ' ';//add space
	    	}	
	    	msgsnd(msgid, &msg, sizeof(msg.text), IPC_NOWAIT);
        }
	    msgctl(msgid, IPC_RMID, 0);
	    exit(0);
}

server2

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<linux/msg.h>
 
#define MAXMSG 512
#define MSGKEY 10

struct my_msg   //消息队列结构体
{
	long int msg_type;
	int i;
	char text[MAXMSG];
}msg;

int main(int argc, char const *argv[]){
		int msgid = msgget(MSGKEY, 0666|IPC_CREAT);	
		msg.msg_type=3;
        int flag = 1;
        int a[MAXMSG]={0};
        int len = 0; 
        while(flag){
            memset(msg.text,'\0',MAXMSG);
            while(1){
                if(msgrcv(msgid, &msg, sizeof(msg.text), msg.msg_type, IPC_NOWAIT)>0){
                    len = 0;
                    for(int i = 0; msg.text[i]!='\0';i++){
                        len = len*10 + (msg.text[i]-'0');
                    }
                    break;
                }
            }
                
            memset(msg.text,'\0',MAXMSG);
            while(1){         
		        if(msgrcv(msgid, &msg, sizeof(msg.text), msg.msg_type, IPC_NOWAIT)> 0){
		            //printf("%s\n",msg.text);
		            for(int i = 0,j=0;i<len;){
		                a[i]=0;
		                while(msg.text[j]>='0'&&msg.text[j]<='9'){
	    	                if(a[i] == 0 &&msg.text[j] == '0') {
	    	                    flag = 0;
	    	                }
		                    a[i] = a[i]*10 + msg.text[j++]-'0';
		                }
		                //printf("%d\t",a[i]);
		                i++;
		                j++;//skip space

		            }
		            
		            //bubble sort
		            for(int i = 1; i<len;i++){
		                for(int j = 0;j<len-i;j++){
		                    if(a[j]>a[j+1]){
		                    	int temp = a[j];
		                        a[j] = a[j+1];
		                        a[j+1] = temp;
		                    }
		                }
		            }
                    printf("\n```\n");
                    //print sort array
		            for(int i = 0; i<len;i++){
		                printf("%d  ",a[i]);
		            }
		            printf("\n```\n");
		            break;
		        }	        
            }       
        }

        msgctl(msgid, IPC_RMID, 0);
        
		exit(0);
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值