This blog is only to remind myself of what I've learned (from others or by myself) and only for knowledge sharing. External sources will be clearly stated in [Reference] of each article. I hope this blog won't infringe any copyrights and that it can be useful for interested blog readers.

2008年3月11日 星期二

Some C and OS related tests

(1) What might be run-time problem of the following code?

How should we modify it?

int foo(int *pi) {
*pi = 1;
return *pi;
}

ps: anybody knows the answer??

(2) Please write a function that returns the maximum number of 3 numbers.

Ans: Take "integer" for example

=>

int getMaxNumFromThree(int a, int b, int c) {

int tmp;

tmp = (a > b) ? a : b;

tmp = (tmp > c) ? tmp : c;

return tmp;

}

(3) Please re-write the following code.

void (*(*pfunc[3])) (char *test);

===>

typedef ____fp_______ ;

fp *pfunc[3];


Ans:

typedef void (*fp) (char *test);
fp *pfp[3];

(4) recursive -- what's the output of the following code?

void e(int num) {

if (num > 0) {
e(num - 1);
printf("%d ", num);
e(num - 1);
}
return;
}

Ans: e(3) => 1 2 1 3 1 2 1

(5) pointer -- what's the output of the above code?

int ref[] = {8, 4, 0, 2};
int *ptr;
int index = 0;

for (index = 0; index < 4; index++; ptr++) {
printf("%d %d\n", ref[index], *ptr);
}

Ans:

8 8

4 4

0 0

2 2

(6) In OS scheduling, what is "priority inversion"?
and in what situation it may happen?

Ans:

這個情況會發生在 當 low priority task 佔用了 high priority task 的資源時...
high priority task 此時必須等到 low priority task 釋放該資源才可以繼續做事...
在 RTOS 的環境中若是 interrupt 沒處理好就有可能發生這種事情...

(7) what's the difference between process and thread?

process 是獨立的執行單位,擁有自己的位址空間還有狀態資訊,
process 與 process 之間的溝通透過某些 IPC 機制,ex: socket, shared memory 等
thread 是活在 process 內的執行程式,所有的 thread 可享有 process 內部的資源,
如 memory space..而 process 的 threads 之間可直接溝通,因為他們享有同樣的
process 內的變數...保護共享的方法有 semaphore or mutex 等..

(8) The comparisons between different RTOSes, such as

WinCE vs Windows Mobile 5/6

MicroC

NuCleus

Linux

沒有留言: