Question Collecting:
1. CICT Ch 02 - Linked List
2. Ch 05 - Bit Manipulation
1
01 - Insertion
2
02 - Binary to String
3
03 - Flip Bit to Win
4
04 - Next Number
5
05 - Debugger
6
06 - Conversion
7
07 - Pairwise Swap
8
08 - Draw Line
Copied!
3. Please explain the difference between the following two functions in standard C library. Explain the meaning of the qualifiers const and restrict. What can happen if the object pointed to by s1 and the object pointed to by s2 overlap?
#include <string.h> void *memcpy(void restrict si, const void* restrict s2, size_t n); void *memmove(void *s1, const void *s2, size_t n);
4. add two matrix within O(N^2)
6. What problem will the code cause? (Hint: Multi-threading issues.)
1
static int count =1;
2
​
3
void* incr_count(void*p) {
4
++count;
5
return 0;
6
}
7
​
8
static pthread_mutex_t m1 = PTHREAD_MUTEX_INITIALIZER;
9
static pthread_mutex_t m2 = PTHREAD_MUTEX_INITIALIZER;
10
​
11
void* lock_m1_then_m2(void*p){
12
pthread_mutex_lock(&m1);
13
pthread_mutex_lock(&m2);
14
pthread_mutex_unlock(&m2);
15
pthread_mutex_unlock(&m1);
16
return 0;
17
}
18
​
19
void* lock_m2_then_m1(void*p){
20
pthread_mutex_lock(&m2);
21
pthread_mutex_lock(&m1);
22
pthread_mutex_unlock(&m1);
23
pthread_mutex_unlock(&m2);
24
return 0;
25
}
26
​
27
int main(void){
28
pthread_t ti, t2, t3, t4;
29
pthread_create(&t1, NULL, incr_count, NULL);
30
pthread_create(&t2, NULL, incr_count, NULL);
31
pthread_create(&t3, NULL, lock_m1_then_m2, NULL);
32
pthread_create(&t4, NULL, lock_m2_then_m1, NULL);
33
pthread_join(t4, NULL);
34
pthread_join(t3, NULL);
35
pthread_join(t2, NULL);
36
pthread_join(t1,NULL);
37
return count;
38
}
Copied!
7. Why does the program not exit ?
1
#include <stdio.h>
2
#include <unistd.h>
3
#include <pthread.h>
4
​
5
pthread mutex_t mutex1;
6
pthread_mutext mutex2;
7
static int count = 0;
8
​
9
void func1(void arg){
10
while((*(int*)(arg))<200){
11
pthread_mutex_lock(&mutex1);
12
pthread mutex_lock(&mutex2);
13
*(int*)arg = *(int*)(arg) + 1;
14
​
15
printf("thread a\n", *(int*)arg);
16
pthread_mutex_unlock(&mutex2);
17
pthread_mutex_unlock(&mutex1);
18
​
19
}
20
}
21
​
22
​
23
void* func2(void* arg){
24
while((*(int*)(arg))<200){
25
pthread_nutex_lock(&mutex2);
26
pthread mutex_lock(&mutex1);
27
*(int*)arg = *(int*)(arg) + 1;
28
printf("thread %d\n", *(int*)arg);
29
pthread_mutex_unlock(&mutex1);
30
pthread_mutex_unlock(&mutex2);
31
}
32
}
33
​
34
int main(int argc, char argv[]){
35
pthread_mutex_init(&mutex1, NULL);
36
pthread mutex_init(&mutex2, NULL);
37
​
38
pthread_t tid1;
39
pthread_t tid2;
40
​
41
pthread_create(&tid1, NULL, func1, (void*) (&count));
42
pthread_create(&tid2, NULL, func2, (void") (&count));
43
​
44
while(1){
45
if(count>=200){
46
break;
47
}else{
48
sleep(1);
49
}
50
}
51
return 0;
52
}
Copied!
​
9. What's the difference between heap and stack ?
10. how does violate be implemented ?
11. Common sense of compiler and OS For me, the params of makefile is a little bit important.
12. sorting, binary tree
13. How can we avoid the issue of synchronize problem ? mutex ? https://stackoverflow.com/questions/5515276/synchronization-problems-in-c-using-pthreads-mutexes​
14. 判斷 big endian and little endian
OS: interrupt
OS: 怎麼 sync 多個 threads
Copy link