Two reports on the basic experiment of information security system design

Source: Internet
Author: User

The basic experiment of information security system design two

Lab report cover:

First, the experimental process 1. Configure the development environment with experiment one
    • Set the XP system, the Redhat virtual machine, the ARM machine IP in the same network segment.
    • Install the arm compiler.
    • Enter the virtual machine, enter the./install.sh on the command line, and the setup script will automatically establish the directory and configure the compilation environment.
    • Configuring Environment variables

2. Copy the Experiment Code 02_ pthread and 03_tty to the shared folder BC.

3. Read the source code

Go to the Exp/basic/02_pthread directory and read the source code using the VI editor or other editor.

4. Compile the application 5. Download and debug

Run the executable file Pthread in HyperTerminal, the result

Second, the experimental Code Analysis 1. PTHREAD.C Code
#include <stdio.h> #include <stdlib.h> #include <time.h> #include "pthread.h" #define Buffer_size 16/* Sets the round buffer for an integer */struct prodcons {int buffer[buffer_size];/* buffer array */pthread_mutex_t lock;/* Mutex */int readpos, Writepos; /* Read and write location */pthread_cond_t notempty; /* Buffer non-empty signal */pthread_cond_t notfull; /* Buffer non-full signal */};/*--------------------------------------------------------*//* Initialize buffer */void init (struct prodcons * b) {  Pthread_mutex_init (&b->lock, NULL);p thread_cond_init (&b->notempty, NULL); Conditional variable initialization pthread_cond_init (&b->notfull, NULL); b->readpos = 0;b->writepos = 0;} /*--------------------------------------------------------*//* producer writes a shared loop buffer function put, writes an integer to the buffer */void put (struct   Prodcons * b, int data) {Pthread_mutex_lock (&b->lock); Get Mutex/* Wait buffer not full */while ((B->writepos + 1)% Buffer_size = = B->readpos) {printf ("Wait for not full\n");p Thread_cond _wait (&b->notfull, &b->lock); Wait for the state variable to b->notfull, the dissatisfaction jumps out of the block}/* write the data and the pointer moves forward */b->buffeR[b->writepos] = data; Write Data b->writepos++;if (b->writepos >= buffer_size) b->writepos = 0;/* Set buffer non-empty signal */pthread_cond_signal (  &b->notempty);  Set the state variable Pthread_mutex_unlock (&b->lock); Release the mutex}/*--------------------------------------------------------*//* The consumer reads the shared loop buffer function get, reads an integer from the buffer */int get (  struct Prodcons * b) {int data;  Pthread_mutex_lock (&b->lock); Gets the mutex/* Wait buffer non-empty */while (B->writepos = = B->readpos) {//If the read-write location is the same as printf ("Wait for not empty\n");p thread_cond_wait  (&b->notempty, &b->lock); Wait for the state variable, not empty to jump out of the block. Otherwise countless data can be read.  }/* read the data and move the pointer forward */data = b->buffer[b->readpos];  Reading data b->readpos++;  if (B->readpos >= buffer_size) b->readpos = 0;/* Set buffer non-full signal */pthread_cond_signal (&b->notfull);  Set the state variable Pthread_mutex_unlock (&b->lock); Release the mutex return data;} /*--------------------------------------------------------*/#define OVER ( -1) struct prodcons buffer;/*------------ --------------------------------------------*/void * Producer (void * data) {int n;for (n = 0; n <; n++) {printf ("put-->%d\n", N);p ut (&buffer, n);} Put (&buffer, over);p rintf ("producer stopped!\n"); return NULL;} /*--------------------------------------------------------*/void * Consumer (void * data) {int D;while (1) {d = Get (& Buffer), if (d = = over) break;printf ("%d-->get\n", d);} printf ("Consumer stopped!\n"); return NULL;} /*--------------------------------------------------------*/int Main (void) {pthread_t th_a, th_b;void * Retval;init (  &buffer);p thread_create (&th_a, NULL, producer, 0); Thread creation function Pthread_create (&th_b, NULL, consumer, 0);  /* Wait for producers and consumers to end */pthread_join (Th_a, &retval); Waits for the specified thread to end Pthread_join (Th_b, &retval); return 0;}
2. Production and consumption flowchart

3. TERM.C Code
#include <termios.h>/*ppsix Terminal control definition */#include <stdio.h>/* standard input/output definition */#include <unistd.h>/*linux Standard function definition */#include <fcntl.h>/* File control definition */#include <sys/signal.h> #include <pthread.h>/* Line Libraries definition */#define BaudRate b115200#define COM1 "/DEV/TTYS0" #define COM2 "/dev/ttys1" #define ENDMINITERM1/* ESC to quit Miniterm */#defin E ENDMINITERM2 3/*ctl +c to quit Miniterm */#define FALSE 0#define TRUE 1volatile int stop=false;volatile int fd;void c Hild_handler (int s) {printf ("Stop!!!   \ n "); Stop=true;}    /*--------------------------------------------------------*/void* keyboard (void * data) {int C; for (;;) {C=getchar ();//printf ("GetChar is:%d", c); if ((c== ENDMINITERM1) | (C==ENDMINITERM2))    {stop=true;    break; }}return NULL;}     /*--------------------------------------------------------*//* Modem Input handler */void* receive (void * data) {int C;    printf ("Read modem\n"); while (Stop==false) {read (fd,&c,1);/* COM port */wRite (1,&c,1);    /* stdout */} printf ("Exit from Reading modem\n"); return NULL;    }/*--------------------------------------------------------*/void* Send (void * data) {int c= ' 0 ';    printf ("Send data\n");    while (Stop==false) {C + +;    C%= 255; Write (fd,&c,1);    /* stdout */usleep (100000); } return NULL; /* Wait for child-die or it'll become a zombie */}/*--------------------------------------------------------*/int mai    n (int argc,char** argv) {struct Termios oldtio,newtio,oldstdtio,newstdtio;    struct Sigaction sa;    int OK;    pthread_t th_a, Th_b, Th_c;    void * RETVAL;  if (argc > 1) FD = open (COM2, O_RDWR); /* Open the serial port in read/write mode */Else FD = open (COM1, O_RDWR); //| O_noctty |    O_nonblock);    if (fd <0) {/* Cannot open serial port one */perror (COM1);    Exit (-1);    } tcgetattr (0,&oldstdtio); Tcgetattr (Fd,&oldtio); /* Save current modem settings */tcgetattr (fd,&newstdtio); /* Get working stdtio */NEWTIO.C_CFLAG = BaudRate | crtscts | CS8 | clocal | Cread;  /* Control Mode flag */Newtio.c_iflag = Ignpar;     /* Input Mode flag */newtio.c_oflag = 0;   /* Output Mode flag */newtio.c_lflag = 0;    /* Local Mode flags */newtio.c_cc[vmin]=1; newtio.c_cc[vtime]=0;    /* Now clean the modem line and activate the settings for modem */Tcflush (FD, Tciflush);    Tcsetattr (fd,tcsanow,&newtio);/*set attrib */sa.sa_handler = Child_handler;    sa.sa_flags = 0; Sigaction (Sigchld,&sa,null);    /* Handle Dying Child */Pthread_create (&th_a, NULL, keyboard, 0);    Pthread_create (&th_b, NULL, receive, 0);    Pthread_create (&th_c, NULL, send, 0);    Pthread_join (Th_a, &retval);    Pthread_join (Th_b, &retval);    Pthread_join (Th_c, &retval); Tcsetattr (Fd,tcsanow,&oldtio); /* Restore Old modem setings */tcsetattr (0,tcsanow,&oldstdtio);    /* Restore old TTY setings */close (FD); Exit (0); }
4. TTY.C Code
#include <stdio.h> /*标准输入输出定义*/#include <unistd.h> /*linux 标准函数定义*/#include <fcntl.h> /*文件控制定义*/#include <errno.h> /*错误号定义*/#include <termios.h> /*PPSIX 终端控制定义*/int main(){int fd,n;    char buf[256];fd=open("/dev/ttyS1",O_NOCTTY|O_RDWR|O_NONBLOCK); /*以读写方式打开串口*/if( fd < 0)  /* 不能打开串口一*/{perror("Unable open /dev/ttyS0\r ");        return 1;}n = write( fd, "hello\r", 6);if ( n < 0 )        puts( "write() of 6 bytes failed!\r");puts( "write() of 6 bytes ok!\r");while(1){        read(fd,buf,256);        puts(buf);        if(strncmp(buf,"quit",4)==0)break;  }return 0;}int set_port(int fd){    struct termios opt;    tcgetattr(fd,&opt);/*get current option setup*/    show_option(&opt);//  opt.c_cflags &=     tcsetattr(fd,&opt);/*get current option setup*/}
5. Serial Communication Experiment Flowchart

Third, the problems encountered and solutions 1. The ARMV4L-UNKNOWN-LINUX-GCC command displayed when compiling the hello.c file was not found.

Workaround: Check the previous Development Environment configuration section and discover that the environment variable path is set incorrectly, causing the command to be found later when using the compile command. Change the environment variable PATH to path= $PATH: The compilation will be completed successfully $HOME/bin:/opt/host/armv4l/bin/.

2. The ARMV4L-UNKNOWN-LINUX-GCC command was not found when compiling the Pthread.c file.

WORKAROUND: Add the exact path to the file you want to compile before compiling the command to compile successfully.

Iv. references

The basic experiment of information security system design and graphic tutorial

"2410 Classic Experiment Guide 20110331"

"How to build an arm cross-compiler environment under Linux" http://www.cnblogs.com/joeblackzqq/archive/2011/05/17/2048696.html

Two reports on the basic experiment of information security system design

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.