20175203-20175206 实验三《实时系统》

20175203-20175206 实验三《实时系统》

本小组成员2017520320175206

任务一

1.学习使用Linux命令wc(1)

2.基于Linux Socket程序设计实现wc(1)服务器(端口号是你学号的后6位)和客户端

3.客户端传一个文本文件给服务器

4.服务器返加文本文件中的单词数

Linux系统中的wc(Word Count)命令的功能为统计指定文件中的字节数、字数、行数,并将统计结果显示输出

命令格式:wc [选项]文件

命令功能:统计指定文件中的字节数、字数、行数,并将统计结果显示输出。该命令统计指定文件中的字节数、字数、行数。如果没有给出文件名,则从标准输入读取。wc同时也给出所指定文件的总统计数。

命令参数:

-c 统计字节数。
-l统计行数。
-m统计字符数。这个标志不能与 -c 标志一起使用。
-w统计字数。一个字被定义为由空白、跳格或换行字符分隔的字符串。
-L打印最长行的长度。
-help显示帮助信息

服务端

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#define MYPORT 165318
void main(){
    int serverfd, clientfd;
    struct sockaddr_in my_addr;
    struct sockaddr_in remote_addr;
    char buffer[BUFSIZ];
    memset(&my_addr, 0, sizeof(my_addr));
    my_addr.sin_family=AF_INET;
    my_addr.sin_addr.s_addr=INADDR_ANY;
    my_addr.sin_port=htons(MYPORT);

    if((serverfd=socket(PF_INET, SOCK_STREAM, 0))==-1){
        perror("socket");
    }
    
    if(bind(serverfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))<0){
        perror("bind");
    }
    listen(serverfd, 5);
    int addrlen=sizeof(struct sockaddr_in);
    while(1){
        if((clientfd=accept(serverfd, (struct sockaddr *)&remote_addr, &addrlen))<0){
            perror("accept");
    }
    printf("accept client %s\n", inet_ntoa(remote_addr.sin_addr));
    int len, i;
    long wordscount=0;
    int flag=1;
    while(1){
        if((len=recv(clientfd, buffer, 1024, 0))>0){
            for(i=0; i<len; i++){
                if(flag==0){
                    switch(buffer[i]){
                        case ' ':
                            wordscount++;
                            break;
                        case '\n':
                            wordscount++;
                            break;
                        case '\r':
                            wordscount++;
                            break;
                        default:
                            break;
                    }
                }
            if(buffer[i]== ' ' || buffer[i]=='\n' || buffer[i]=='\r') flag=1;
            else flag=0;
            }
        }
        if(len<1024) break;
    }
    send(clientfd, &wordscount, sizeof(long), 0);
    close(clientfd);
    }
    close(serverfd);
}

客户端


#include <stdio.h>  
#include <sys/types.h>  
#include <sys/socket.h>  
#include <netinet/in.h>  
#include <arpa/inet.h>  
#include <string.h>
#define MYPORT 165318
void main(){
    int clientfd;
    struct sockaddr_in remote_addr;
    char buffer[BUFSIZ];
    memset(&remote_addr, 0 , sizeof(remote_addr));
    remote_addr.sin_family=AF_INET;
    remote_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
    remote_addr.sin_port=htons(MYPORT);
    if((clientfd=socket(PF_INET,SOCK_STREAM,0))<0){  
        perror("socket");  
    }
    if(connect(clientfd, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr))<0){
        perror("connect");
    }
    int len;
    FILE *fp;
    char path[20];
    gets(path);
    fp=fopen(path, "r");
    char readch;
    int i=0;
    while((readch=fgetc(fp))!=EOF){
        if(i<1024){
            buffer[i]=readch;
            i++;
        }
        else{
            i=0;
            int n=send(clientfd, buffer, 1024, 0);
        }
    }
    fclose(fp);
    if(i!=0) 
        send(clientfd, buffer, i, 0);
    long wordscount;
    recv(clientfd, &wordscount, sizeof(long), 0);
    printf("%ld\n", wordscount);
    close(clientfd);
}

实验截图


任务二

1.使用多线程实现wc服务器并使用同步互斥机制保证计数正确

2.对比单线程版本的性能,并分析原因

服务端

#include<stdlib.h>
#include<pthread.h>
#include<sys/socket.h>
#include<sys/types.h>      
#include<stdio.h>
#include<netinet/in.h>     
#include<arpa/inet.h>      
#include<assert.h>         
#include<string.h>         
#include<unistd.h>         
#define SOCK_PORT 9988
#define BUFFER_LENGTH 1024
#define MAX_CONN_LIMIT 512     
static void Data_handle(void * sock_fd); 
int CountWordsOfEuropeanTxtFile(char *szFileName);
int CountWordsInOneLine(const char *szLine);
int main()
{
    int sockfd_server;
    int sockfd;
    int fd_temp;
    struct sockaddr_in s_addr_in;
    struct sockaddr_in s_addr_client;
    int client_length;
    sockfd_server = socket(AF_INET,SOCK_STREAM,0); 
    assert(sockfd_server != -1);
    memset(&s_addr_in,0,sizeof(s_addr_in));
    s_addr_in.sin_family = AF_INET;
    s_addr_in.sin_addr.s_addr = htonl(INADDR_ANY); 
    s_addr_in.sin_port = htons(SOCK_PORT);         
    fd_temp = bind(sockfd_server,(const struct sockaddr *)(&s_addr_in),sizeof(s_addr_in));
    if(fd_temp == -1)
    {
        fprintf(stderr,"bind error!\n");
        exit(1);
    }
    fd_temp = listen(sockfd_server,MAX_CONN_LIMIT);
    if(fd_temp == -1)
    {
        fprintf(stderr,"listen error!\n");
        exit(1);
    }
    while(1)
    {
        printf("waiting for new connection...\n");
        pthread_t thread_id;
        client_length = sizeof(s_addr_client);
        sockfd = accept(sockfd_server,(struct sockaddr * restrict)(&s_addr_client),(socklen_t *)(&client_length));
        if(sockfd == -1)
        {
            fprintf(stderr,"Accept error!\n");
            continue;                            
        }
        printf("A new connection occurs!\n");
        if(pthread_create(&thread_id,NULL,(void *)(&Data_handle),(void *)(&sockfd)) == -1)
        {
            fprintf(stderr,"pthread_create error!\n");
            break;                                 
        }
    }
    int ret = shutdown(sockfd_server,SHUT_WR);
    assert(ret != -1);
    printf("Server shuts down\n");
    return 0;
}
static void Data_handle(void * sock_fd)
{
    int fd = *((int *)sock_fd);
    int i_recvBytes;
    char data_recv[BUFFER_LENGTH];
    const char * data_send = "Server has received your request!\n";
    while(1)
    {
        printf("waiting for file_name...\n");
        memset(data_recv,0,BUFFER_LENGTH);
        i_recvBytes = read(fd,data_recv,BUFFER_LENGTH);
        if(i_recvBytes == 0)
        {
            printf("Maybe the client has closed\n");
            break;
        }
        if(i_recvBytes == -1)
        {
            fprintf(stderr,"read error!\n");
            break;
        }
        if(strcmp(data_recv,"quit")==0)
        {
            printf("Quit command!\n");
            break;                         
        }
        printf("read from client : %s\n",data_recv);
    char buffer[BUFFER_LENGTH];
    int count=0;
    bzero(buffer, BUFFER_LENGTH);
    count = CountWordsOfEuropeanTxtFile(data_recv);
    sprintf(buffer,"%d", count);
        send(fd, buffer, sizeof(buffer), 0);
        if(write(fd,data_send,strlen(data_send)) == -1)
        {
            break;
        }
    }
    printf("terminating current client_connection...\n");
    close(fd);           
    pthread_exit(NULL);  
}
int CountWordsOfEuropeanTxtFile(char *szFileName)
{
    int nWords = 0;
    FILE *fp; //文件指针
    char carrBuffer[1024]
    if ((fp = fopen(szFileName,  "r")) == NULL)
    {
        return -1;
    }
    while (!feof(fp))
    {
        if (fgets(carrBuffer, sizeof(carrBuffer),fp) != NULL)
            nWords += CountWordsInOneLine(carrBuffer);
    }
    fclose(fp);
    return nWords;
}
int CountWordsInOneLine(const char *szLine)
{
    int nWords = 0;
    int i=0;
    for (;i<strlen(szLine);i++)
    {
        if (*(szLine+i)!=' ')
        {
            nWords++;
            while ((*(szLine+i)!=' ')&&(*(szLine+i)!='\0'))
            {
                i++;
            }
        }

    } 
    return nWords;
}

客户端

#include<stdlib.h>
#include<sys/socket.h>
#include<sys/types.h>      
#include<stdio.h>
#include<netinet/in.h>      
#include<arpa/inet.h>       
#include<assert.h>         
#include<string.h>         
#include<unistd.h>          
#define SOCK_PORT 9988
#define BUFFER_LENGTH 1024
int main()
{
    int sockfd;
    int tempfd;
    struct sockaddr_in s_addr_in;
    char data_send[BUFFER_LENGTH];
    char data_recv[BUFFER_LENGTH];
    memset(data_send,0,BUFFER_LENGTH);
    memset(data_recv,0,BUFFER_LENGTH);
    sockfd = socket(AF_INET,SOCK_STREAM,0);       
    if(sockfd == -1)
    {
        fprintf(stderr,"socket error!\n");
        exit(1);
    }
    //before func connect, set the attr of structure sockaddr.![](https://img2018.cnblogs.com/blog/1290756/201811/1290756-20181115200439006-896968805.png)


    memset(&s_addr_in,0,sizeof(s_addr_in));
    s_addr_in.sin_addr.s_addr = inet_addr("127.0.0.1");     
    s_addr_in.sin_family = AF_INET;
    s_addr_in.sin_port = htons(SOCK_PORT);
    tempfd = connect(sockfd,(struct sockaddr *)(&s_addr_in),sizeof(s_addr_in));
    if(tempfd == -1)
    {
        fprintf(stderr,"Connect error! \n");
        exit(1);
    }
    while(1)
    {
        printf("Please Input File Name On Server(input \"quit\" to quit):\t");
    scanf("%s", data_send);        
        tempfd = write(sockfd,data_send,BUFFER_LENGTH);
        if(tempfd == -1)
        {
            fprintf(stderr,"write error\n");
            exit(0);
        }
        if(strcmp(data_send,"quit") == 0)  
        {
            break;
        }
        else
        {
            tempfd = read(sockfd,data_recv,BUFFER_LENGTH);
            assert(tempfd != -1);
            printf("%s\n",data_recv);
            memset(data_send,0,BUFFER_LENGTH);
            memset(data_recv,0,BUFFER_LENGTH);
        }
    char buffer[BUFFER_LENGTH];
    int length=0;
    bzero(buffer, BUFFER_LENGTH);
        length = recv(sockfd, buffer, BUFFER_LENGTH, 0);
        buffer[length] = '\0';
        printf("count=%s\n", buffer);
        bzero(buffer, BUFFER_LENGTH);
    }
    int ret = shutdown(sockfd,SHUT_WR);       
    assert(ret != -1);
    return 0;
}

实验截图


分析单线程和多线程

单线程一次只允许一个客户端连接,易于实现,稳定安全但是无法实现多线程服务
多线程一次可以允许多个客户端连接,工作效率更高,但是可能不太稳定

任务三

交叉编译多线程版本服务器并部署到实验箱中

PC机作客户端测试wc服务器

提交测试截图

因为是交叉编译,即使用armgcc进行编译,与第一个实验的环境一致,所以首先ping通虚拟机和超级终端:

ping命令使目标机和宿主机能互相ping通,ping通的截图如下:

虚拟机ping超级终端:

超级终端ping虚拟机:

实验截图

arm-none-linux-gnuenbi-gcc编译的命名为armserver: /usr/local/toolchain/toolchain-4.3.2/bin/arm-none-linux-gnueabi-gcc server.c -o armserver

对需要共享的文件进行赋予权限:

打开Ubuntu命令行终端,配置/etc/exports:

sudo vim /etc/exports

在文件/etc/exports中进行了允许挂载权限的定义,在/etc/exports文件末添加如下一行:

/home/linux/linux_20175203_20175206 *(rw,sync,no_root_squash,no_subtree_check)

然后使用权限更新命令sudo /etc/init.d/nfs-kernel-server restart来重启服务

在超级终端上无法运行映射过去的程序,无论是修改代码还是更换试验箱都没有达到想要的效果,所以第三个实验的最后部分没有完成,将在之后弄清问题所在后后加以解决

posted @ 2019-11-18 15:14  20175203马羽达  阅读(230)  评论(0编辑  收藏  举报