CS代写 MEC302 Embedded Computer Systems

MEC302 Embedded Computer Systems
Parallelism
Dr. Sanghyuk Lee
Email: Dept. Mechatronics and Robotics

Copyright By PowCoder代写 加微信 powcoder

Parallelism
• Most processors today provide various forms of parallelism
• These mechanisms strongly affect the timing of the execution of a program, so embedded system designers have to understand them.
How to support
• Thread library; it provide API(Application Programming Interface) to generate and manage thread
• Provide library only in user space without Kennel support
• POSIX Pthread/Windows/Java

Parallelism vs Concurrency
• Concurrent
A computer program is concurrent if different parts of the program conceptually execute simultaneously
• Parallel
A program is parallel if different parts of the program physically execute simultaneously on distinct hardware.

To program and return. Every thread can access
Access only to counter thread

What is thread
• Unit of program(process)
• One process construct multiple thread
• Threads share memory and resources allocated in process
• Each thread has its own stack and register
Difference between process and thread
• Process is operating program; it consume time and resource
• Thread is considered as operation unit; it share address space or
• In this case, thread has its own stack and register due to its
independent operation

Why stack is allocated independently to each thread
• It is memory space to the specific thread, t makes possible independent operation
• It is requirement for the independent operation
Why register is allocated independently to each thread
• It need to keep the running status because it could be interrupted by scheduler
• Advantage: faster than processor/mutual communication is possible due to share memory and resource

Multithread synchronization
• OS scheduling operate with alternatively
• It can extend to multi thread program
• In the same process, multi thread share memory. Then synchronization problem can happen.

Pointers – Point to Point
• The computer accesses its own memory not by using variable names but by using a memory map with each location of memory uniquely defined by a number, called the address of that memory location.
• A pointer is a variable that stores this location of memory. In more fundamental terms, a pointer stores the address of a variable. In more picturesque terms, a pointer points to a variable.
• A pointer must be declared just like any other variable – remember a pointer is just a variable that stores an address. For example,
int *p; int *p, q; p=&q; p=q;

Pointers – Point to Point
• If you place * in front of a pointer variable, then the result is the value stored in the variable pointed at. That is, p stores the address, or pointer, to another variable and *p is the value stored in the variable that p points at.
• The * operator is called the de-referencing operator and it helps not to confuse it with multiplication or with its use in declaring a pointer.
• This multiple use of an operator is called operator overload.

Pointers – Point to Point
There are three basic ideas:
• To declare a pointer, add an * in front of its name.
• To obtain the address of a variable use & in front of its name.
• To obtain the value of a variable use * in front of a pointer’s name.

Pointers – Swap shop
• Write a function which swaps the contents of two variables
• In principle this should be easy
Try this – does not work – reason is all parameters are passed by value

Pointers – Swap shop
• The solution is to pass not the values stored in the variables, but the addresses of the variables.
function swap(int *a , int *b); {
temp = *a;
*b = temp; }

Advanced Data Types
• What we would really like to do is to use a name like a[i]
where i is a variable which specifies which value, we are working with.
• In the case of C you must declare an array before you use it – in the same
way you must declare any sort of variable. For example,
int a[5]; a[0], a[1], a[2], a[3], a[4];
• In C you must remember that
type array[size] − declares an array of the specified type and with size elements. The first array element is array[0] and the last is array[size-1].

Advanced Data Types
Finding the largest number in an array

Multidimensional Arrays
• In C programming, you can create an array of arrays. These arrays are known as multidimensional arrays. For example,
float x[3][4]
Here, x is a two-dimensional array. The array can hold 12 elements

Pointers and Arrays
• In C there is very close connection between pointers and arrays int a[10];
• To be more precise, a[i] is exactly equivalent to *(a+i) i.e., the value pointed at by a+i.
• Notice that you can even use ++ and — with a pointer, but not with an array name because this is a constant pointer and cannot be changed.
• An array’s name is a constant pointer to the first element in the array that is a==&a[0] and *a==a[0].
• Array indexing is equivalent to pointer arithmetic – that is a+i=&a[i] and *(a+i)==a[i].

Pause and talk; synchronization
Mutex(Mutual exclusion);
For the single toilet, there is que and only one person can use /not allow simultaneous approach; use ‘lock’
Semaphore;
For three toilets, three persons use simultaneously, it is related with access order (Que)
• For the three toilets, if it is used by Mutex then everyone should stand only on toilet, cannot move to the other even it is vacancy/three toilets are considered as Mutex, only one person use it regardless number of toilets
• Toilets are synchronization objective/person is thread

// the data shared by the threads int sum;
// thread call this function void * runner(void* param); int main(int argc, char* argv[]) {
pthread_t tid; // thread identifier pthread_attr_t attr; // thread attributes if(argc!=2)
fprintf(stderr, “usage: a.out \n”);
return -1; }
if(atoi(argv[1])<0) { fprintf(stderr,"%d must be >= 0\n”,atoi(argv[1]));
return -1; }
Two thread generated.
• main() function
• Runner operating thread.
• main() function generate the
second thread to operate
• Two thread share sum:
global variable
C programming generation 2 thread; Example Pthread API

thread identifier
C programming generation 2 thread; Pthread API: Continue
pthread_attr_init(&attr); // attr initialization
pthread_create(&tid, &attr, runner, argv[1]); // thread generation pthread_join(tid, NULL); // tid thread wait
printf(“sum = %d\n”, sum); }
void *runner(void* param) {
int i, upper = atoi(param);
sum = 0; for(i=0; i<=upper; i++) { sum += i; } pthread_exit(0); } •pthread_t tid : thread identifier •pthread_attr_t attr : thread attributes •pthread_attr_init(&attr) : assign at attr as attributes; default •pthread_create(&tid, &attr, runner, argv[1]) : thread generation The End of the Lecture 程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com