個人檔案different life, differen...相片部落格清單更多 ![]() | 說明 |
|
6月21日 Some basic but interesting questions about C language.1.in C language, what key word register means?
if register number is not enough, what happpen?
Asnwer:The keyword register tell compiler store the variable in regster, not in memory.
If the register number is not enough, the variable have to be stored in stack.
Below is the C code and the assembled code:
void main()
{ register int a=1; register int b=2; register int c=3; register int d=4; register int a1=1; register int b1=2; register int c1=3; register int d1=4; printf("a:%d,b:%d,c:%d,d:%d",a,b,c,d);
printf("a1:%d,b1:%d,c1:%d,d1:%d",a1,b1,c1,d1); } movl $1, %edi movl $2, %edx movl $3, %ecx movl $4, %eax movl $1, -24(%ebp) movl $2, -20(%ebp) movl $3, %ebx movl $4, %esi 2.what's the below code's output?
struct A
{ int a; char c; }; struct B { int a; char c; int b; }; printf("size of struct A %d,struct B %d",sizeof(struct A),sizeof(struct B)); Answer:size of struct A 8,struct B 12
If code is modifed as:
struct A
{ int a; char c; } __attribute__ ((packed)); struct B { int a; char c; int b; }__attribute__ ((packed)); printf("size of struct A %d,struct B %d",sizeof(struct A),sizeof(struct B)); The output is: size of struct A 5,struct B 9
http://tigcc.ticalc.org/doc/gnuexts.html#SEC91_packed explain how to use the keyword.
3.for (i=0; i < 10; i++) {
printf("i:%d",i); if (i == 5)
continue; printf("i:%d",i);
} Will this loop infinite or finite? Answer:
the loop is finite.output is
i:0 i:0 i:1 i:1 i:2 i:2 i:3 i:3 i:4 i:4 i:5 i:6 i:6 i:7 i:7 i:8 i:8 i:9 i:9
The assembled code of the C code piece is as below:
movl $0, -8(%ebp)
movl $0, -8(%ebp) jmp .L2 .L4: movl -8(%ebp), %eax movl %eax, 4(%esp) movl $.LC0, (%esp) call printf cmpl $5, -8(%ebp) je .L3 movl -8(%ebp), %eax movl %eax, 4(%esp) movl $.LC0, (%esp) call printf .L3: addl $1, -8(%ebp) .L2: cmpl $9, -8(%ebp) jle .L4 .L4 is the loop, .L3 is "i++", .L2 is "i<10"
Alough "continue" bypass the remaning code in the loop, but it still excute the "i++",as http://msdn.microsoft.com/en-us/library/6e3dc2z3(VS.80).aspx said
"In a for loop (using the syntax for(init-expr; cond-expr; loop-expr)), continue causes loop-expr to be executed. Then cond-expr is reevaluated and, depending on the result, the loop either terminates or another iteration occurs." 4. int main()
{ int i=1; do { printf("%d\n",i); i++; if(i < 15) continue; }while(false); return 0; } What would be output? Answer:
1
As http://tigcc.ticalc.org/doc/keywords.html#do says:"
do statement while (expression) statement, which is usually a compound statement, is executed repeatedly as long as the value of expression remains non-zero. The test takes place after each execution of the statement."
So even continue skip the remaning statements,it still need test "expression". 5 int main()
{ char *p="Network\n"; p[0]='n'; printf("%s", p); return 0; } What is the output? Answer: the output is "Segmentation fault",because the string is in readonly data section.The below is the assembled code:
.section .rodata
.LC0: .string "Network\n" .LC1: .string "%s" But if the code "char *p=Network\n";" is changed to
"char p[30]="Network\n";",the program would not generate segement fault because the string is in stack.
6.which one takes precedence when comparing signed int and unsigned int?
Answer:
unsigned int take precedence,I think it is just a rule, not for any special reseaon.
The below code testify it:
unsigned int a=0xfffffff0;
int b=0x10; if(a<b)
printf("b is bigger\n"); else printf("a is bigger\n"); The output is "a is bigger". why we say linux 2.6 scheduler is premptive? why we say linux 2.4 schueduler is not premptive?According to Understanding linux kernel edition3, chapter 5.1.1, Kernel Preemption is defined as:"a preemptive kernel differs from a nonpreemptive kernel on the way a process running in Kernel Mode reacts to asynchronous events that could induce a process switchfor instance, an interrupt handler that awakes a higher priority process. We will call this kind of process switch a forced process switch." "in nonpreemptive kernels, the current process cannot be replaced unless it is about to switch to User Mode " I still remember that I wrote a driver in linux 2.4.* about a few years ago,one error--an infinite loop exist in the driver, and once one process call the drvier in the kernel, the whole operating system become halted.
Read arch/i386/kernel/entry.S of linux 2.6,please pay attention to ret_from_exception and ret_from_intr, in these two functions,it will check the current process's _TIF_NEED_RESCHED bit, if the bit is set, will do
call preempt_schedule_irq().(Refer to understanding linux kernel edition3 chapter4.9) So even a infinite loop run in kernel, the process's time slice would be used up after a few time intterupts, at last, after one time intterrupt returns, the process in kernel mode would be prempted.
But in arch/i386/kernel/entry.S of linux 2.4,please pay attention to ret_from_intr and ret_from_exception, only in the conditon that return to user Mode(Not kernel Mode!!!), it will call schedule(). So an infinite loo in linux 2.4 kernal has no chance to be prempted. process/pthread's scheduling on linuxI want to learn more about process/pthread's scheduling on linux, but can't find systematic information after searching on web.So I try step by step, by writing code to verify and reading code to research how the process/pthread's scheduling work.
CPU:virtual-box on i686,uni-core OS kernel:2.6.25-14.fc9.i686
library:libc.so.6, libgpgme-pthread.so.11.6.4
step1:two different process has the same priority and nice
while.c:
void main()
{ do{}while(1); } [root@localhost tmp]#gcc while.c -o while [root@localhost tmp]# ./while &
[1] 27533 [root@localhost tmp]# ./while & [2] 27534 [root@localhost tmp]# top
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
27533 root 20 0 1668 256 204 R 44.7 0.1 1:37.16 while 27534 root 20 0 1668 256 204 R 44.7 0.1 1:35.77 while Concusion: The CPU time is divided envenly between the two processes,because they have the same nice and priority. step2: based on step1, change nice(static priority)
change one process's nice to 5, the other process's nice keep 0 at default.
[root@localhost tmp]# nice --adjustment=5 ./while &
[1] 27614 [root@localhost tmp]# ./while & [2] 27615 [root@localhost tmp]# top
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
27615 root 20 0 1668 252 204 R 67.9 0.1 2:23.95 while 27614 root 25 5 1668 252 204 R 22.3 0.1 0:52.66 while According nice's manual:
Nicenesses range from -20 (most favorable scheduling) to 19 (least favorable).
Because these processes are not real-time,they are traditionla time-sharing processes,their nice(priority) just tell scheduler how much quantum time is allocated. step3:different processes, different threads
pthread_while.c
#include <pthread.h>
pthread_t ntid;
void *
thr_fn(void *arg) { do{}while(1);
return((void *)0); } void main()
{ int err; err = pthread_create(&ntid, NULL, thr_fn, NULL); if (err != 0) { printf("create thread fail\n"); exit(1); } do{}while(1);
exit(0); } [root@localhost tmp]# top -H PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
2322 root 20 0 12148 476 396 R 23.8 0.2 7:31.35 pthread_while 2323 root 20 0 12148 476 396 R 23.8 0.2 7:31.37 pthread_while 2325 root 20 0 1668 256 204 R 23.8 0.1 7:23.46 while It shows that the two threads in one process are scheduled with main thread in the other process.Reference 1 says:"
The nptl implementation only uses a 1:1 thread model. The scheduler handles every thread as if it were a process. Therefor only the supported scope is PTHREAD_SCOPE_SYSTEM. "
4.real time process scheduling
SCHED_FIFO and SCHED_RR policy can be used to tell thread(process) as realtime thread.If the high-priority thread not blocked,the low-priority thread has no chance to run. I have no idea how to verify these scheduling rules.
References:
1.Thread Scheduling with pthreads under Linux and FreeBSD:
2.glibc
3.understanding linux kernel,3rd edition. Chapter 7
6月16日 Very interesting! fulcrum's FM4000 can used a fabric elment.The 24-port density of the chip - also an industry best - enables it to be the interconnect foundation for extremely large clustered systems. When used in fat tree (or Clos) network architectures, the density and low latency mean the network can scale to 3,456 non-blocking nodes in three tiers of switching. Cut-Through and Store-and-Forward in switchLatency is critcal in data center switches. And Cut-Through architecture can reduce latency.
According to my testing expierence, the latency in Store-and-Forward switch chip is at least a few tens of micro-second,and HPC need the latency is less than 10 micro-second.
Fulcrum boast that its chip has the lowest latency in the industry--300 nano-second.
Fulcrum Launches World's First Low-Latency IP Router Chips; 300ns Latency Extends Role of Ethernet in the Datacenter
Also the below article explains the difference between Cut-Through and Store-and-Forward architecture.
Cut-Through and Store-and-Forward Ethernet Switching for Low-Latency Environments |
|
|