taoqian's profiledifferent life, differen...PhotosBlogListsMore Tools Help

taoqian taoqian

There are no music lists on this space.

different life, different dream

June 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
  
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 linux

I 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
 
 
June 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 switch

Latency 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

May 09

我喜欢健身

体检出来有脂肪肝之后,我觉得我应该减减肥了。另外,有时候感觉自己的颈椎偶尔有些难受。
以前也经常跑步,不过第一感觉自己很难坚持,第二距离太短,只有3km,热身有余,燃烧脂肪又不足。
所以决定找个健身房,让运动的强度更大一些,恰好公司附件有个健身房,价格合理,办了卡,每天下班之后去跑步,然后做做简单的器械。我每次跑步是5km,在跑步机上加一点坡度,一次下来能消耗400大卡的热量.现在坚持做了两三个月,效果不错,体能上升非常明显,也减了一些肥。
健身房的锻炼还是有好处的,第一是锻炼气氛好,有群体效应;第二是可以加大单位时间的强度;第三是器械锻炼可以更全面,腰,颈都可以得到锻炼。
因为我的带动,公司很多同事也慢慢开始有规律的锻炼了。
 
不过健身房的锻炼也有些缺点,第一是无法作平衡性练习,第二是其实练的更多的是心肺和肌肉,对软组织无法有针对性的做更多的练习。如果有机会,我会去尝试一下瑜伽,它能解决这些问题,还可以调理呼吸和心神。
 
 
May 05

管理中最可怕的事情是什么

就是一个项目,项目负责人告诉老板,这个项目难度可控,可以搞定,其实这个项目难度非常大;或者是快做完了,其实还有一半的工作没做完;或者是明明需要投入1000万,花40人X年的任务,老板却被告知一半的资源也可以把这个项目拿下来。
 
这样下去,最后项目做成烂尾,浪费了时间,客户的期望,士气也非常低落,后果非常严重。
 
为什么会出现这种问题呢?一般而言,老板喜欢听话的人,这些人也喜欢说一些让老板开心的话,他们往往被放到项目负责人的位置。这种情况下,项目负责人经常不敢对老板讲真话,另外具体干活的人往往资历短,眼光也短浅,不愿意多事。慢慢的就酿成了悲剧。
 
如何克服这种问题呢:
1.老板的心胸要开阔,公司内部一定要有唱反调的人,他们可以时时提醒。
或者
2.分权:一定要有一个可以检测项目进度和质量的人,他是监督这个项目和项目负责人的,这个人一定不能是老板。
或者
3.具体干活的人中有对公司较为忠诚的人,这些人资历不能太浅,也就是说让他们的荣辱跟公司的前途进行绑定。
 
 
 
 
Photo 1 of 6
More albums (1)