一些面试题(更新中)

题目来源: http://student.csdn.net/space.php?uid=116484&do=blog&id=51701

 

1.What is the difference between a mutex and a semaphore?

 

mutex是互斥体,semaphore是信号量。

 

mutex是用来保证对一个资源的互斥访问,一定要由获得mutex的进程来释放。"Mutexes are typically used to serialise access to a section of re-entrant code that cannot be executed concurrently by more than one thread. A mutex object only allows one thread into a controlled section, forcing other threads which attempt to gain access to that section to wait until the first thread has exited from that section.”

 

semaphore是为了使信号从一项任务传至另一项任务,可以由其他进程或任务来增减。 “A semaphore restricts the number of simultaneous users of a shared resource up to a maximum number. Threads can request access to the resource (decrementing the semaphore), and can signal that they have finished using the resource (incrementing the semaphore).”使用方法可参考生产者/消费者模型:

intial:empryCount=N,fullCount=0;

produce:
    P(emptyCount)
    putItemIntoQueue(item)
    V(fullCount)
consume:
    P(fullCount)
    item ← getItemFromQueue()
    V(emptyCount)

另外:semaphores可以被interrupt service routine(ISR)中断服务程序用来向task发送信号.发送一个semaphore是一个非阻塞的RTOS行为,并且ISR安全.因为这种技术排除了在task级别的为了是中断不使能而引起的错误的可能性,从ISR中发出信号是一种使嵌入式软件更加可靠的设计方式.

2.Given a function which produces a random integer in the range 1 to 5, write a function which produces a random integer in the range 1 to 7.

 

解法之一:

function rand7()
{
    $vals
= array(
        array
( 1, 2, 3, 4, 5 ),
        array
( 6, 7, 1, 2, 3 ),
        array
( 4, 5, 6, 7, 1 ),
        array
( 2, 3, 4, 5, 6 ),
        array
( 7, 0, 0, 0, 0 )
   
);

    $result
= 0;
   
while ($result == 0)
   
{
        $i
= rand5();
        $j
= rand5();
        $result
= $vals[$i-1][$j-1];
   
}
   
return $result;
}

 

3.Write a regular expression which matches a email address

 

简化回答的话,可以用/w+@/w+/.[/w]{2,4} 来表示,/w表示可允许的字符,比如A-Za-z0-9等;

具体化的话,可以写作^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+/.(?:[a-z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)$

 

具体可以参考:http://www.regular-expressions.info/email.html

 

需要注意的是*,+,? 在正则表达式中都是贪婪的,换句话说,他们就是尽可能多的匹配。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值