Exam 2021-6-2 DIT635 Development of Embedded and Real-Time Systems Peiran Wei: 19990922-2490 Question 1 - 9 Points Circuit Overview: 0~10 degrees : 10~20 degrees : 20~30 degrees: 30~40 degrees: 40-50 degrees: abnormal temperature: Source code: // sources code of Question 1 // date : 6/2/2021 #include
// timer headerfile // sensor pin declaration const int SENSOR_PIN = A0; const int SENSOR_VAL_MIN = 20; const int SENSOR_VAL_MAX = 358; const int SENSOR_TEMP_MIN = -40; const int SENSOR_TEMP_MAX = 125; // LED PINS definitions const int LED_PIN_MIN = 2; const int LED_PIN_MAX = 6; const int LED_COUNT = LED_PIN_MAX - LED_PIN_MIN + 1; const int lOOP_DELAY = 300; // definitions of pins` values int LED_LEVELS[LED_COUNT] = {0,0,0,0,0}; // declaration of temp judge functions void temp_detect(); // initiate the program void setup() { MsTimer2::set(500, temp_detect); // set the interrupts MsTimer2::start(); // start the timer and counting // initiate the led pins for(int ledPin = LED_PIN_MIN; ledPin <= LED_PIN_MAX; ledPin++) { pinMode(ledPin, OUTPUT); digitalWrite(ledPin, LOW); } } // change the pins` level void temp_detect() { int sensorVal = analogRead(SENSOR_PIN); float temp = map(sensorVal, SENSOR_VAL_MIN, SENSOR_VAL_MAX, SENSOR_TEMP_MIN, SENSOR_TEMP_MAX); if(temp < 10 && temp >=0) // if the degree is between 0 ~ 10 { LED_LEVELS[0] = 1; LED_LEVELS[1] = 0; LED_LEVELS[2] = 0; LED_LEVELS[3] = 0; LED_LEVELS[4] = 0; } else if (temp < 20 && temp >=10) // if the degree is between 10 ~ 20 { LED_LEVELS[0] = 1; LED_LEVELS[1] = 1; LED_LEVELS[2] = 0; LED_LEVELS[3] = 0; LED_LEVELS[4] = 0; } else if (temp < 30 && temp >=20) // if the degree is between 20 ~ 30 { LED_LEVELS[0] = 1; LED_LEVELS[1] = 1; LED_LEVELS[2] = 1; LED_LEVELS[3] = 0; LED_LEVELS[4] = 0; } else if (temp < 40 && temp >=30) // if the degree is between 30 ~ 40 { LED_LEVELS[0] = 1; LED_LEVELS[1] = 1; LED_LEVELS[2] = 1; LED_LEVELS[3] = 1; LED_LEVELS[4] = 0; } else if (temp < 50 && temp >=40) // if the degree is between 40 ~ 50 { LED_LEVELS[0] = 1; LED_LEVELS[1] = 1; LED_LEVELS[2] = 1; LED_LEVELS[3] = 1; LED_LEVELS[4] = 1; } else // otherwise all the leds shut down { LED_LEVELS[0] = 0; LED_LEVELS[1] = 0; LED_LEVELS[2] = 0; LED_LEVELS[3] = 0; LED_LEVELS[4] = 0; } } // the main loop void loop() { for(int i = 0; i < LED_COUNT; i++) { digitalWrite(LED_PIN_MIN+i, LED_LEVELS[i]); } delay(lOOP_DELAY); } Question 2 - 7 Points Source code: // sources code of Question 2 // date : 6/2/2021 #include // header file declaration #include #define Status int // data type declaration const int MAX = 100; //upper bound const int MIN = 0; //lower bound int randnums = 0; // random num generated by server int randnumc = 0; // random num generated by client int connected = 0; // whether s and c are connected int initc = 0; int inits = 0; //initial flag // the beginning of server Status server(int guess) { if(!connected) return ; //not connected , failed if(inits == 0) //generate a random num { randnums = rand() % MAX; // 0 ~ 100 inits = 1 ; } if(guess!=0) { printf("server: received num from client : %d \n", guess); if(guess < randnums) return -1; else if(guess > randnums) return 1; else if(guess == randnums) return 0; } } int client(Status server_flag,int range_l, int range_h) // set up a client { if(initc == 0) // start guessing { randnumc = rand() % range_h; initc = 1; connected = 1; // connected prepared printf("client: connected the server and the first number is %d \n",randnumc); return randnumc; } else { if(server_flag == -1) // too low { printf("client: the number is too low, new number is %d \n",randnumc + 1); randnumc++;// increase the number return randnumc; } else if(server_flag == 1) //too high { printf("client: the num if too high, new number is %d\n", randnumc -1); randnumc--; // decrease the number return randnumc; } else // correct { printf("client: correct, the number is %d",randnumc); return -1; // game over, succeeded } } } // the beginning of main() int main() { printf("connecting....\n"); // prompt user the actual process int counter = 0; int response = -2; // response from the server int guess = 0; // current number for(counter=0 ; counter< MAX ; counter ++) // guess process { guess = client(response,MIN,MAX); //guess once if(guess == -1) break; // if succeed, game over response = server(guess); // else continue, send to server } return 0; } Question 3 - 7 Points Source code: // sources code of Question 3 // date : 6/2/2021 #include // header file declarations #include #include /*Task 1*/ int prime_check(int num) // check the num is prime or not { int k , i= 0 ; k=(int)sqrt(num ); for(i=2;i<=k;i++) { if(num%i==0) break; } if(i>k) { printf("%d is prime\n",num); // print out results to user return 1; } else { printf("%d is not prime\n",num); return -1; } return 0; } // the beginning of main() int main() { prime_check(2); } Question 4 - 8 Points Source code: // sources code of Question 4 // date : 6/2/2021 /* *This program demonstrates the data structure of a binary tree*Your task is specified in the exam*/ #include // header file declaration #include // This is one node of the tree typedef struct { int data; // data which is stored in the node void* left; // pointer to the left sub-tree void* right; // pointer to the right sub-tree } Node; /* *This function creates an empty nde*/ /* Task A : print the node */ // print the binary tree type: 0- root , 1- left ,2- eight, level- layer of tree void printTree(Node *n, int type, int level) { int i; if (NULL == n) // NUll node , failed return; printTree((Node *)n->right, 2, level+1); switch (type) // Recursive printing // type: 0- root , 1- left ,2- eight, level- layer of tree { case 0: //root printf("%2d\n", n->data); break; case 1: // left for (i = 0; i < level; i++) printf("\t"); printf("\\\n"); for (i = 0; i < level; i++) printf("\t"); printf(" %2d\n", n->data); break; case 2: //right for (i = 0; i < level; i++) printf("\t"); printf(" %2d\n", n->data); for (i = 0; i < level; i++) printf("\t"); printf("/\n"); break; } printTree((Node *)n->left, 1, level+1); } /* Task B : delete the node */ void DeletNode(Node *p, Node *cur, int DelData) { Node *subNode = NULL; // sub-nodes Node *pNode = NULL; // parent nodes if (cur == NULL) { printf("not exist\n"); exit(0); } else if (DelData > cur->data) { DeletNode((Node *)cur, (Node *)cur->right, DelData); } else if (DelData < cur->data) { DeletNode((Node *)cur, (Node *)cur->left, DelData); } else if(DelData == cur->data) { if ((Node *)cur->left == NULL && (Node *)cur->right == NULL) //the delete nodes are subnodes { if (p->left == cur) //left { p->left = NULL; } else if (p->right == cur) //right {p->right = NULL; } } } } /* Task C : problems */ /* 1.function main didn`t check whether the fromer creation succeeded or not 2.function CreateNode didn`t check whether the current creation succeeded or not 3.the memories are not freed at the end of the program 4.the type of child should not be void*, Node * is better */ Node * CreateNode(int val) { Node *p; // pointer to the new node // create the node p = (Node *) malloc(sizeof(Node)); // store the value p->data = val; // Left and right child for node // will be initialized to null p->left = NULL; p->right = NULL; // return the new node return p; } /* Task D : delete the tree */ void freeTree(Node *n) { if (n == NULL) return; // free left if ((Node *)n->left != NULL) { freeTree((Node *)n->left); n->left = NULL; } // free right if ((Node *)n->right != NULL) { freeTree((Node *)n->right); n->right = NULL; } // free root if (n != NULL) { free(n); n = NULL; } } /* *Main - create a simple tree*/ int main() { /*create root*/ Node* root = CreateNode((int)'B'); // create two sub-trees //Task C: make sure the former creation is correct if(root == NULL) { printf("Failed exit!"); // failed return -1; } printTree(root,0,0); printf("\n"); root->left = CreateNode('9'); //Task C: make sure the former creation is correct if(root->left == NULL) { printf("Failed exit!"); // failed return -1; } printTree(root,0,0); root->right = CreateNode(0x20); //Task C: make sure the former creation is correct if(root->right == NULL) { printf("Failed exit!"); // failed return -1; } printTree(root,0,0); // print the tree ((Node *)root->left)->left = CreateNode(68); //Task C: make sure the former creation is correct if(((Node *)root->left)->left== NULL) { printf("Failed exit!"); // failed return -1; } printTree(root,0,0); printf("\n"); ((Node *)root->right)->left = CreateNode(1); //Task C: make sure the former creation is correct if(((Node *)root->right)->left == NULL) { printf("Failed exit!"); // failed return -1; } printTree(root,0,0); printf("\n"); //Task C - free all memory freeTree(root); printTree(root,0,0); } Question 5 - 4 Points 1. For multi-core processors, each core is essentially running on its own. Multithreading does not mean parallel operations. Programs are executed sequentially in a single core. The system optimizes resources and automatically schedules threads. All in all, multithreading is not a prerequisite for fully exploiting CPU performance. For example, in deep learning, the CPU cannot implement parallel operations, so GPUs are needed. 2. For a multi-core processor, it is essentially just a collection of multiple single-core sequential processors. There is no parallel computing unit like GPU on the circuit, so even if multithreading is used, it only improves efficiency from the level of resource utilization. In short, multi-threading and multi-core processors do not necessarily improve each other. In order to greatly speed up the operation of the computer, it is still necessary to consider parallel chips (e.g., GPU, FPGA) or design special computing units for algorithms. 欢迎咨询51作业君