深信服 linux軟體開發面試題整理__linux

來源:互聯網
上載者:User
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.

#include <stdio.h>#include <string.h>struct suo{  char name[40];  int age;} person, person_copy;int main (){  int n;  char myname[] = "Pierre de Fermat";  /* using memcpy to copy string: */  memcpy ( person.name, myname, strlen(myname)+1 );  person.age = 46;  /* using memcpy to copy structure: */  memcpy ( &person_copy, &person, sizeof(person) );  printf ("person_copy: %s, %d \n", person_copy.name, person_copy.age );n = memcmp(&person_copy, &person, sizeof(struct suo) );if ( n > 0 )printf("greater");else if ( n < 0 )printf("smaller");elseprintf("=\n");  return 0;}

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、大端小端
大端位元組(Big-endian):較高的有效位元組存放在較低的儲存空間地址,較低的有效位元組存放在較高的儲存空間地址。
小端位元組(Big-endian):字資料的高位元組儲存在高地址中,而字資料的低位元組則存放在低地址中。
/********大端返回0;小端返回1*******/
int checkCPU()
{
union w
{
int x ;
char y ;
}c ;
c.x = 1;


return (c.y==1);
}


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、說說你知道的經典排序演算法名稱








聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.