Chinaunix首页 | 论坛 | 博客
  • 博客访问: 13988
  • 博文数量: 3
  • 博客积分: 235
  • 博客等级: 二等列兵
  • 技术积分: 40
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-03 15:27
文章分类

全部博文(3)

文章存档

2012年(1)

2009年(2)

我的朋友

分类: C/C++

2012-05-25 09:42:31

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define KEY 1234
#define SIZE 1024
#define PORT 9999
#define MAXFDS 5000
#define EVENTSIZE 100
void process();
int listenfd, cfd,opt=1;
int shmid;
char *shmaddr;
struct shmid_ds buf;
int num = 0 ;
#define LOCKFILE_H
int sev_accept_mutex_held  = 0;
 
 
 
void write_log2 (char *fileName,char *msg)
{
        FILE *fp;
        char filePathName[256];
        memset(filePathName,0,256);
        snprintf(filePathName,256,"./%s.log",fileName);
        fp = fopen(filePathName,"a+" );
        if ( fp )
        {
             char msgs[1024];
             memset(msgs,0,1024);
             snprintf(msgs,1024,"%d %s\n",getpid(),msg);
             fwrite(msgs,sizeof(char),strlen(msgs),fp);
             fclose(fp);
        }
 }
int main(int argc, char *argv[])
{
     int ret = 0;
     int i = 0;
    struct sockaddr_in sin, cin;
    socklen_t sin_len = sizeof(struct sockaddr_in);
    if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) <= 0)
    {
        fprintf(stderr, "socket failed/n");
        return -1;
    }
    memset(&sin, 0, sizeof(struct sockaddr_in));
    sin.sin_family = AF_INET;
    sin.sin_port = htons((short)(PORT));
    sin.sin_addr.s_addr = INADDR_ANY;
    if (bind(listenfd, (struct sockaddr *)&sin, sizeof(sin)) != 0)
    {
         fprintf(stderr, "bind failed/n");
         return -1;
    }
    if (listen(listenfd, 32) != 0)
    {
        fprintf(stderr, "listen failed/n");
        return -1;
    }
 
    for(i = 0; i < atoi(argv[1]); i++)
    {
      int pid = fork();
      if(pid == 0)
      {
          process();     
      }
    } 
   
    while(1)
    {
          ret=waitpid(-1,NULL,WNOHANG|WUNTRACED);
          if ( ret )
          {
 
          }
          usleep(100); 
    };
  
    return 0;
}
int lockfd = 0;
int unlock_file()
{
   struct flock  fl;
    fl.l_start = 0;
    fl.l_len = 0;
    fl.l_pid = 0;
    fl.l_type = F_UNLCK;
    fl.l_whence = SEEK_SET;
    if (fcntl(lockfd, F_SETLK, &fl) == -1)
    {
        printf("unlock error %d\n",getpid());
        return  -1;
    }
    printf("unlock ok %d\n",getpid());
    return 0;
}
int init_lock_file(char *fileName)
{
     lockfd = open(fileName,O_RDWR| O_CREAT,0666);
    if (lockfd == -1)
    {
        perror("open lock error");
        printf("open fd %d\n",lockfd);
        return -1;
    }
 
}
int lock_file()
{
    struct flock  fl;
    fl.l_start = 0;
    fl.l_len = 0;
    fl.l_pid = 0;
    fl.l_type = F_WRLCK;
    fl.l_whence = SEEK_SET;
    if (fcntl(lockfd, F_SETLK, &fl) == -1)
    {
       //printf("lockfd error %d\n",getpid());
        return -1;
    }
    else
    {
        printf("lock process  %d\n",getpid());
    }
    return 0;

}
int sev_enable_accept_events(int epollid,int socketfd,struct epoll_event *pev)
{
    if (epoll_ctl(epollid, EPOLL_CTL_ADD, socketfd, pev) < 0)
    {
        printf("peoll_ctl  add error %d\n",getpid());
        return ;
    }
    else
    {
        printf("监听 socket 加入 epoll 成功!listenfd %d epollfd %d %d\n",socketfd,epollid,   getpid());
    }
 
}
int sev_disable_accept_events(int epollid,int socketfd )
{
    struct epoll_event   ee;
    ee.events = 0;
    ee.data.ptr = NULL;
    ee.data.fd = listenfd;
    if (epoll_ctl(epollid, EPOLL_CTL_DEL, socketfd,&ee ) < 0)
    {
        printf("peoll_ctl  del error %d\n",getpid());
        return ;
    }
    else
    {
        printf(" EPOLL_CTL_DEL %d\n",getpid());
    }
}
int sev_mod_accept_events(int epollid,int socketfd)
{
    struct epoll_event   ee;
    ee.events = EPOLLIN | EPOLLET;
    ee.data.fd = listenfd;
    if (epoll_ctl(epollid, EPOLL_CTL_MOD, socketfd,&ee ) < 0)
    {
        perror("mod epoll error\n");
        printf("eoll_ctl  mod error %d\n",getpid());
        return ;
    }
    else
    {
        printf(" EPOLL_CTL_MODE %d\n",getpid());
    }
 
    return 0;
}

void process()
{
    int nfds =  0;
    int timer = 0;
    int ret = 0;
    int n = 0; 
    struct epoll_event ev;
    struct epoll_event events[1000];
    int    kdpfd = epoll_create(1000);
    int  len = sizeof(struct sockaddr_in);
    ev.events = EPOLLIN | EPOLLET;
    ev.data.fd = listenfd;
    int new_fd;

    if((fcntl(listenfd, F_GETFL, 0)&O_NONBLOCK))
    {
        printf("ok non block\n");
    }
    else
    {
          perror("set non block error\n");
          printf("wrong non block \n");
    }     printf("sub socket is %d \n", listenfd);
   
    struct sockaddr_in my_addr, their_addr;
    init_lock_file("./lockfile");
   
    int accCount = 0;
    while (1)
    {
           /* 等待有事件发生 */
           ret  = lock_file();
           if ( ret == -1 )
           {
                  timer = 50; 
           }
           else
           {
                  timer = -1;
                  sev_accept_mutex_held = 1;
                  sev_enable_accept_events(kdpfd,listenfd,&ev); 
  
           }
       
           nfds = epoll_wait(kdpfd, events, 200, timer);
           if (nfds == -1)
           {
               perror("epoll_wait\n");
               break;
           }
           /* 处理所有事件 */
          for (n = 0; n < nfds; ++n)
         {
             if (events[n].data.fd == listenfd)
             {
                 if ( accCount>=200 ) //
                    break;
                 new_fd = accept(listenfd, (struct sockaddr *) &their_addr,&len);
                 if (new_fd < 0)
                 {
                    printf("accept error %d\n", getpid());
                    continue;
                }
                else
                {
                      accCount++;
                      char buffer[1024];
                      memset(buffer,0,1024);
                      snprintf(buffer,1024,"create new socket: %d %d\n", getpid(),new_fd); 
                      write_log2("loggg",buffer);
                }
            }
        }
        if ( sev_accept_mutex_held == 1 )
        {
                sev_disable_accept_events(kdpfd,listenfd);
                sev_accept_mutex_held = 0;
                unlock_file();
        }
    }

}
 
 
阅读(816) | 评论(0) | 转发(0) |
0

上一篇:lihttpd 安装

下一篇:没有了

给主人留下些什么吧!~~