易截截图软件、单文件、免安装、纯绿色、仅160KB

Linux 惊群现象

        在linux中,惊群现象已经消失了的,我们可以看   http://simohayha.javaeye.com/blog/561424 ,但是当我们在开发服务器时候,需要使用epoll,发现一个问题,就是当一个请求过来的时候,发现有的时候被唤起的进程不止一个,看下面的程序:#include <sys/socket.h>
#include <sys/epoll.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include <errno.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <errno.h>
#define KEY 1234
#define SIZE 1024
#define PORT 9999
#define MAXFDS 5000
#define EVENTSIZE 100
void process();
int fd, cfd,opt=1;
int shmid;
char *shmaddr;
struct shmid_ds buf;
int num = 0 ;
int main(int argc, char *argv[])
{
shmid = shmget(KEY,SIZE,IPC_CREAT|0600); /* 建立共享内存 */
if(shmid == -1){
printf("create share memory failed\n");
}
shmaddr = (char *)shmat(shmid,NULL,0);
if(shmaddr == (void *)-1){
printf("connect to the share memory failed: %s",strerror(errno));
return 0;
}
strcpy(shmaddr,"1\n");

struct sockaddr_in sin, cin;
socklen_t sin_len = sizeof(struct sockaddr_in);
if ((fd = 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(fd, (struct sockaddr *)&sin, sizeof(sin)) != 0)
{
fprintf(stderr, "bind failed\n");
return -1;
}
if (listen(fd, 32) != 0)
{
fprintf(stderr, "listen failed\n");
return -1;
}
int i ;
for(i = 0; i < 2; i++)
{
int pid = fork();
if(pid == 0)
{


相关文档:

Linux iptable文档

总览
用iptables -ADC 来指定链的规

,-A添加 -D删除 -C 修改
iptables - [RI] chain rule num rule-specification[option]
用iptables - RI 通过规则的顺序指定
iptables -D chain rule num[option]
删除指定规则
iptables -[LFZ] [chain][option]
用iptables -LFZ 链名 [选项]
iptables -[NX] chain
用 -NX ......

Linux 的多线程编程的高效开发经验

2009 年 4 月 23 日
本文中我们针对 Linux 上多线程编程的主要特性总结出 5 条经验,用以改善 Linux 多线程编程的习惯和避免其中的开发陷阱。在本文中,我们穿插一些 Windows 的编程用例用以对比 Linux 特性,以加深读者印象。
背景
Linux 平台上的多线程程序开发相对应其他平台(比如 Windows)的多线程 API 有一些细微 ......

linux socket 详解

头文件:sys/socket.h
相关结构体:
struct sockaddr
{
    unsigned short sa_family;   //地址族
    char sa_data[14];   //14字节协议地址
};
struct sockaddr_in
{
    short int sin_family;   //地址族
    u ......

linux shell种类

Linux中的shell有多种类型,其中最常用的几种是Bourne   shell(sh)、C   shell(csh)和Korn   shell(ksh)。三种shell各有优缺点。Bourne   shell是UNIX最初使用的shell,并且在每种UNIX上都可以使用。Bourne   shell在shell编程方面相当优秀,但在处理与用户的交互 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号