深信服 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 person{
  char name[40];
  int age;
} person_a, person_a_copy;

int main ()
{
  int n;
  char myname[] = "Pierre de Fermat";

  /* using memcpy to copy string: */
  memcpy ( person_a.name, myname, strlen(myname)+1 );
  person_a.age = 46;

  /* using memcpy to copy structure: */
  memcpy ( &person_a_copy, &person_a, sizeof(person) );

  printf ("person_copy: %s, %d \n", person_a_copy.name, person_a_copy.age );

n = memcmp(&person_a_copy, &person_a, sizeof(struct person) );
if ( n > 0 )
printf("greater\n");
else if ( n < 0 )
printf("smaller\n");
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);
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、说说你知道的经典排序算法名称

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

callinglove

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值