日期:2014-05-16  浏览次数:20619 次

深信服 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.

#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");
else
printf("=\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);