select the limit

/ *
A, SELECT achieve concurrent two limitations concurrent server
1, a file descriptor into the maximum limit that can be opened. Can be modified in two ways
ulimit -n: get the maximum number of file descriptors
ulimit -n 2048: 2048 modified
can be modified by another program, use setrlimit restrictions of the parent process can not be changed, only change the current process and child processes can the maximum open file descriptors limit
  int getrlimit (int resource, struct rlimit * rlim); // get the system resource limits to obtain resources for RLIMIT_NOFILE
  int setrlimit (int resource, const struct rlimit * RLIM); 
  

  of rlimit {struct
    rlim_t The rlim_cur; // Soft limit
    rlim_t rlim_max member; // Hard limit (for ceiling The rlim_cur)
  };
2, the SELECT fd_set restricted set capacity (FD_SETSIZE), to modify the value of the macro, which is to rebuild the kernel
* /

#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<errno.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<signal.h>
#include <sys/time.h>

#include <sys/resource.h>
#define ERR_EXIT(m)\
do\
{\
  perror(m);\
  exit(EXIT_FAILURE);\
}while(0)
int main(void)
{
  struct rlimit rl;
  if(getrlimit(RLIMIT_NOFILE,&rl)<0)
    ERR_EXIT("getrlimit");
  printf("%d\n",(int)rl.rlim_max);

  rl.rlim_cur=2048;
  rl.rlim_max=2048;
  if(setrlimit(RLIMIT_NOFILE,&rl)<0)
    ERR_EXIT("setrlimit");
  if(getrlimit(RLIMIT_NOFILE,&rl)<0)
    ERR_EXIT("getrlimit");
  printf("%d\n",(int)rl.rlim_max);

return 0;
}

 

Guess you like

Origin www.cnblogs.com/wsw-seu/p/11022647.html