深信服 linux软件开发面试题整理
1、結構體可以進行比較
int memcmp ( const void * ptr1, const void * ptr2, size_t num );
Compare two blocks of memory
Compares the first num bytes of the block of memory pointed by ptr1 to the first num bytes pointed by ptr2, returning zero if they all match or a value different from zero representing which is greater if they do not.
Notice that, unlike strcmp, the function does not stop comparing after finding a null character.
Returns an integral value indicating the relationship between the content of the memory blocks:
A zero value indicates that the contents of both memory blocks are equal.
A value greater than zero indicates that the first byte that does not match in both memory blocks has a greater value in ptr1 than in ptr2 as if evaluated as unsigned char values; And a value less than zero indicates the opposite.
2、最大打開文件數查看與設置
查看系統級最大打開文件數 # cat /proc/sys/fs/file-max
查看當前用戶最大打開文件數 # ulimit -Hn //查看硬限制
# ulimit -Sn //查看軟限制
系統級的設置 # vi /etc/sysctl.conf
增加: fs.file-max = 100000
立即生效: # sysctl -p
用戶級設置 # vi /etc/security/limits.conf
設置如下:
httpd soft nofile 4096
httpd hard nofile 10240
httpd是用戶,可以使用通配符*表示所有用戶。
要使 limits.conf 文件配置生效,必須要確保 pam_limits.so 文件被加入到啟動文件中。
查看 /etc/pam.d/login 文件中有:
session required /lib/security/pam_limits.so
也可以在/etc/profile后面加上ulimit -n 10240
使用如下命令立即生效:
# su - httpd
$ ulimit -Hn 10240
$ ulimit -Sn 4096
硬限制是可以在任何時候任何進程中設置 ?但硬限制只能由超級用戶提起
軟限制是內核實際執行的限制,任何進程都可以將軟限制設置為任意小于等于對進程限制的硬限制的值
C語言文件指針(fopen)與文件描述符(open)之間可以相互轉換:
int fileno(FILE *stream);
FILE *fdopen(int fd, const char *mode);
3、進程間通信方式
linux下進程間通信的幾種主要手段簡介:
管道(Pipe)及有名管道(named pipe):管道可用于具有親緣關系進程間的通信,有名管道克服了管道沒有名字的限制,因此,除具有管道所具有的功能外,它還允許無親緣關系進程間的通信;
信號(Signal):信號是比較復雜的通信方式,用于通知接受進程有某種事件發生,除了用于進程間通信外,進程還可以發送信號給進程本身;linux除了支持Unix早期信號語義函數sigal外,還支持語義符合Posix.1標準的信號函數sigaction(實際上,該函數是基于BSD的,BSD為了實現可靠信號機制,又能夠統一對外接口,用sigaction函數重新實現了signal函數);
報文(Message)隊列(消息隊列):消息隊列是消息的鏈接表,包括Posix消息隊列system V消息隊列。有足夠權限的進程可以向隊列中添加消息,被賦予讀權限的進程則可以讀走隊列中的消息。消息隊列克服了信號承載信息量少,管道只能承載無格式字節流以及緩沖區大小受限等缺點。
共享內存:使得多個進程可以訪問同一塊內存空間,是最快的可用IPC形式。是針對其他通信機制運行效率較低而設計的。往往與其它通信機制,如信號量結合使用,來達到進程間的同步及互斥。
信號量(semaphore):主要作為進程間以及同一進程不同線程之間的同步手段。
套接口(Socket):更為一般的進程間通信機制,可用于不同機器之間的進程間通信。起初是由Unix系統的BSD分支開發出來的,但現在一般可以移植到其它類Unix系統上:Linux和System V的變種都支持套接字。
http://www.ibm.com/developerworks/cn/linux/l-ipc/
一般來說,linux下的進程包含以下幾個關鍵要素:
有一段可執行程序;
有專用的系統堆棧空間;
內核中有它的控制塊(進程控制塊),描述進程所占用的資源,這樣,進程才能接受內核的調度;
具有獨立的存儲空間
4、Linux多線程同步的幾種方式
1)互斥鎖(mutex)
通過鎖機制實現線程間的同步。同一時刻只允許一個線程執行一個關鍵部分的代碼。
int pthread_mutex_init(pthread_mutex_t *mutex,const pthread_mutex_attr_t *mutexattr);
int pthread_mutex_lock(pthread_mutex *mutex);
int pthread_mutex_unlock(pthread_mutex *mutex);
int pthread_mutex_destroy(pthread_mutex *mutex);
互斥鎖靜態賦值pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIER
attr_t有:
PTHREAD_MUTEX_TIMED_NP:其余線程等待隊列
PTHREAD_MUTEX_RECURSIVE_NP:嵌套鎖,允許線程多次加鎖,不同線程,解鎖后重新競爭
PTHREAD_MUTEX_ERRORCHECK_NP:檢錯,與一同,線程請求已用鎖,返回EDEADLK;
PTHREAD_MUTEX_ADAPTIVE_NP:適應鎖,解鎖后重新競爭
2)條件變量(cond)
利用線程間共享的全局變量進行同步的一種機制。條件變量上的基本操作有:觸發條件(當條件變為 true 時);等待條件,掛起線程直到其他線程觸發條件。
int pthread_cond_init(pthread_cond_t *cond,pthread_condattr_t *cond_attr);
int pthread_cond_wait(pthread_cond_t *cond,pthread_mutex_t *mutex);
int pthread_cond_timewait(pthread_cond_t *cond,pthread_mutex *mutex,const timespec *abstime);
int pthread_cond_signal(pthread_cond_t *cond);
int pthread_cond_broadcast(pthread_cond_t *cond); //解除所有線程的阻塞
int pthread_cond_destroy(pthread_cond_t *cond);
pthread_cond_t cond=PTHREAD_COND_INITIALIER
void pthread_cleanup_push(void (*rtn)(void *),void *arg);
void pthread_cleanup_pop(int execute);
pthread_cleanup_push來注冊清理函數rtn,這個函數有一個參數arg。在以下三種情形之一發生時,注冊的清理函數被執行:
? ? 1)調用pthread_exit。
? ? 2)作為對取消線程請求(pthread_cancel)的響應。
? ? 3)以非0參數調用pthread_cleanup_pop。
注意:
? ? 1)如果線程只是由于簡單的返回而終止的,則清除函數不會被調用。
? ? 2)如果pthread_cleanup_pop被傳遞0參數,則清除函數不會被調用,但是會清除處于棧頂的清理函數。
3)信號量(sem)
#include <semaphore.h>
int sem_init (sem_t *sem , int pshared, unsigned int value);
int sem_wait(sem_t *sem); //-1
int sem_post(sem_t *sem); //+1
int sem_destroy(sem_t *sem);
5、大端小端
大端小端模式判斷以及數據轉換
6、結構體中位域對齊
由于位域不允許跨兩個字節,因此位域長度不超過8 。
存儲原則:
整個位域結構體的大小為其最寬基本類型成員大小的整數倍;
如果位域字段之間穿插著非位域字段,則不進行壓縮;
如果相鄰的兩個位域字段的類型不同,則各個編譯器的具體實現有差異,VC6采取不壓縮方式,GCC和Dev-C++都采用壓縮方式;
struct BFA
{
unsigned char a:2;
unsigned int ?b;
};//gcc 8個字節
struct BFB
{
unsigned char a:2;
unsigned char b:3;
unsigned char c:3;
unsigned int ?d:4; ?//多出來這個位域字段;
};//gcc 4個字節
取地址操作符&不能應用在位域字段上;
位域字段不能是類的靜態成員;
位域字段在內存中的位置是按照從低位向高位的順序放置的;
struct BitField
{
? ? unsigned char a:2; ?//最低位;
? ? unsigned char b:3;
? ? unsigned char c:3; ?//最高位;
};
union Union
{
? ? struct BitField bf;
? ? unsigned int n;
};
union Union ubf;
? ubf.n = 0; ? ?//初始化;
? ubf.bf.a = 0; //二進制為: 00
? ubf.bf.b = 0; //二進制為: 000
? ubf.bf.c = 1; //二進制為: 001
? printf("ubf.bf.n = %u\n", ubf.n);
結果:32
本段出自 http://bdxnote.blog.163.com/blog/static/844423520109103132722/
7、setsockopt(),select()函數的應用
8、TCP / UDP C/S框架
參見華清遠見《基于Socket的UDP和TCP編程介紹》博文
http://www.embedu.org/column/column179.htm
connect函數在UDP中的應用
http://www.embedu.org/Column/Column220.htm
http://blog.csdn.net/mycoolx/article/details/6314354
9、說說你知道的經典排序算法名稱
總結
以上是生活随笔為你收集整理的深信服 linux软件开发面试题整理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【TensorFlow】conv2d函数
- 下一篇: sizeof与offsetof有关的结构