Fundamentals of information Security system Design experiment two: Firmware design (20135229,20135234)

Source: Internet
Author: User
Tags terminates

Beijing Institute of Electronic Technology (BESTI)

Real inspection report

Course: Information Security system Design Basic class: 1352 Name: Lusson

Study number: 20135229 20135234 score: Instructor: Lou Jia Peng Experimental Date:: 2015.11.10

Experiment level: Preview degree: Experiment time: 15:30-17:30

Instrument Group: Compulsory/Elective: compulsory test number: 02

First, the contents of the experiment

1. Configuration of the development environment with experimental one.

2. Copy the experiment code to the shared folder.

3. Compile the code in the virtual machine.

For multithreaded-related code, you need to add a-lpthread library at compile time.

4. Download and debug

Run the executable file Pthread in HyperTerminal, run the executable file term.

Second, code parsing

(1) Analysis

    • This code is the implementation of the producer-consumer problem model.
    • The main program starts the producer thread and the consumer thread, respectively. The producer thread sequentially writes 0 to 1000 of digits to the shared loop buffer, while the consumer thread continuously reads the data from the shared loop buffer.
    • The producer first obtains the mutex, and determines whether the write pointer is equal to the read pointer after +1, if the equality goes into the wait state, waits for the condition variable notfull, if not equal writes an integer to the buffer, and sets the condition variable to Notempty, and finally releases the mutex.

(2) Source code comment

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "Pthread.h"
#define BUFFER_SIZE 16
 
/* Set a round buffer of integers */
struct prodcons {
INT/  * Buffer Array */
/* Mutex lock */
int  /* Read-write location */
/* Buffer non-empty signal */
/* Buffer not full signal */
};
 
/* Initialize buffer: Initialize cache pointer information (semaphore) */
void init (struct prodcons * b)
{
    pthread_mutex_init(&b->lock, NULL);
    pthread_cond_init(&b->notempty, NULL);
    pthread_cond_init(&b->notfull, NULL);
0;
0;
}
 
/* Write an integer to the buffer */
void put (struct prodcons * b, int data)
{
    pthread_mutex_lock(&b->lock);Get Mutex Lock
 
/* Wait buffer is not full */
While  1  //If the read-write location is the same
     {
printf ( "Wait for not full\n");
         pthread_cond_wait(&b->notfull, &b->lock);Wait for the state variable b->notfull, the dissatisfaction jumps out of the block
    }
 
/* Write the data and move the pointer forward */
    b->buffer[b->writepos] = data;Write Data
    b->writepos++;
If  0;
 
/* Set buffer non-empty signal */
    pthread_cond_signal(&b->notempty);Set state variables
    pthread_mutex_unlock(&b->lock);Release Mutex lock
}
 
/* Read an integer from the buffer */
int get (struct prodcons * b)
{
Int data;
    pthread_mutex_lock(&b->lock);Get Mutex Lock
 
/* Wait buffer not empty */
While (b->writepos == b->readpos)
     {
printf ( "Wait for not empty\n");
         pthread_cond_wait(&b->notempty, &b->lock);Wait for the state variable to b->notempty, 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  0;
 
/* Set buffer not full signal */
    pthread_cond_signal(&b->notfull);Set state variables
    pthread_mutex_unlock(&b->lock);Release Mutex lock
Return data;
}
 
#define OVER (-1)
struct prodcons buffer;
 
/* Implement a producer program: the producer thread sequentially writes 0 to 1000 digits to the shared loop buffer, and when production-1, the program terminates. */
void  * producer( void * data)
{
Int n;
For  0  1000; n++) {
printf ( "put-->%d\n", n);
         put(&buffer, n);
    }
    put(&buffer, OVER);
printf ( "producer Stopped!\n");
Return NULL;
}
 
/* Consume the data produced in the cache: the consumer thread continuously reads the data from the shared loop buffer, and when 1 is consumed, the program terminates/
void  * consumer( void * data)
{
Int d;
While  ( 1
     {
         d = get(&buffer);
If 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);
Create a producer thread
0);
Create a consumer thread
0);
/* Wait for producers and consumers to end */
    pthread_join(th_a, &retval);
    pthread_join(th_b, &retval);
Return  0;
}
 
三、遇到的问题及解决

1. File not Found

    • folder is compressed and copied to the shared folder after BC, enter the virtual machine and enter the./install.sh in the command line. config failed to configure the compilation environment. Displays install.sh not found.
    • The command line we opened is located in the default location of the system and should be entered into BC before entering the command.

2. Execution./term Error

The method can be used to establish a connection to resolve. First, enter the/dev folder in HyperTerminal.

Enter the command "ln–sf/dev/tts/0/dev/ttys0" note the space and the letter L, number 0

Fundamentals of information Security system Design experiment two: Firmware design (20135229,20135234)

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.