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)
{
相关文档:
2009 年 4 月 23 日
本文中我们针对 Linux 上多线程编程的主要特性总结出 5 条经验,用以改善 Linux 多线程编程的习惯和避免其中的开发陷阱。在本文中,我们穿插一些 Windows 的编程用例用以对比 Linux 特性,以加深读者印象。
背景
Linux 平台上的多线程程序开发相对应其他平台(比如 Windows)的多线程 API 有一些细微 ......
Linux下Nutch分布式配置和使用
目 录
介绍. 2
0 集群网络环境介绍. 2
1 /etc/hosts文件配置. 2
2 SSH无密码验证配置. 2
2.1配置所有节点之间SSH无密码验证. 2
3 JDK安装和Java环境变量配置. 3
3.1 安装 JDK 1.6 3
3.2 Java环境变量配置. 4
4 Hadoop集群配置. 4
5 Hadoop集群启动. 6
6 Nutc ......
版权声明:原文地址及作者不详,如有侵权,请联系;
本文给出了一个通用的线程池框架,该框架将与线程执行相关的任务进行了高层次的抽象,使之与具体的执行任务无关。另外该线程池具有动态伸缩性,它能根据执行任务的轻重自动调整线程池中线程的数量。文章的最后,我们给出一个简单示例程序,通过该示例程序,我们会发现, ......
Linux 查看所有环境变量命令:expoert 和 env 。
指定环境变量:echo $XXX,(XXX代表环境变量名)
更改环境变量值方法:
1.修改/etc/profile文件
例:在profile文件末尾加入:
JAVA_HOME=/usr/share/jdk1.5.0_05
P ......
Linux中的shell有多种类型,其中最常用的几种是Bourne shell(sh)、C shell(csh)和Korn shell(ksh)。三种shell各有优缺点。Bourne shell是UNIX最初使用的shell,并且在每种UNIX上都可以使用。Bourne shell在shell编程方面相当优秀,但在处理与用户的交互 ......