cgi的写法 (C/C++学习)
发布于 2007-12-05 19:57 阅读:59806 评论:2 标签: C C++ Linux

      CGI虽然差不多已经被淘汰,但在大公司有时还是需要用到的。编写、编译和升级的确是件很麻烦的事情。

      个人感觉写CGI主要是两个方面,一个是头信息;一个是让服务器知道它是个CGI。头信息用“Content-type”指定;而要让服务器知道,则把文件放进服务器专门为CGI而准备的目录,否则会被服务器认为的个二进制文件。当然这个目录是可以自己指定的,对apache而言就是httpd.conf文件中“cgi-bin”的位置。

      下面是一个简单的C++例子:

以下是代码片段:

#include <iostream>

using namespace std;

main()
{
 cout << "Content-type: text/html; charset=gb2312\n\n";
 cout << "www.yayu.org" << flush;
 exit(0);
}

展开全文  
收起全文  
memcached英文文档学习 (C/C++学习)
发布于 2007-12-04 17:58 阅读:65387 评论:1 标签: Linux memcached 学习 英文

      今天看了memcached wiki的FAQ,觉得有几句话对了解memcached很有作用,与大家共享一下。

      服务器的内存如何分配问题:

以下是引用片段:
Because a 32-bit process can only address 4GB of virtual memory (usually significantly less, depending on your operating system), if you have a 32-bit server with 4-64GB of memory using PAE you can just run multiple processes on the machine, each using 2 or 3GB of memory.

     memcached之get过程,看点在于client/server的hash key处理:

以下是引用片段:
When doing a memcached lookup, first the client hashes the key against the whole list of servers. Once it has chosen a server, the client then sends its request, and the server does an internal hash key lookup for the actual item data.

      client/server的hash key算法差异:

以下是引用片段:
Different client implementations store data into memcached differently (perl Storable, php serialize, java hibernate, JSON, etc). Some clients also implement the hashing algorithm differently. The server is always the same however.

      当一个memcached down后的情况:

以下是引用片段:
 Remove dead nodes from your list. Be very careful! With default clients adding or removing servers will invalidate all of your cache! Since the list of servers to hash against has changed, most of your keys will likely hash to different servers. It's like restarting all of your nodes at the same time.

      key/value大小的说明:

以下是引用片段:
 Keys are restricted to 250 characters. Stored data cannot exceed 1 megabyte in size, since that is the largest typical slab size.

      数据存储的处理方式:

以下是引用片段:
 At first memcached did just use malloc/free for everything. However this does not play very well with OS memory managers. You get fragmentation, and your OS ends up spending more time trying to find contiguous blocks of memory to feed malloc() than it does running the memcached process.

      如何处理数据过期的问题:

以下是引用片段:
 When do expired cached items get deleted from the cache?

 memcached uses a lazy expiration, which means it uses no extra cpu
 expiring items. When an item is requested (a get request) it checks
 the expiration time to see if the item is still valid before returning
 it to the client.

 Similarly when adding a new item to the cache, if the cache is full,
 it will look at for expired items to replace before replacing the
 least used items in the cache.

      尤其是对数据过期的处理让偶意外,虽然省CPU,但很浪费空间。

       下面是几个常用的链接:

以下是引用片段:
 memcached'home
http://danga.com/memcached/

memcached'wiki
http://www.socialtext.net/memcached/index.cgi

memcached'faq
http://www.socialtext.net/memcached/index.cgi?faq

memcached'Protocol
http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt

memcached'consistent hashing
http://www.last.fm/user/RJ/journal/2007/04/10/392555/

展开全文  
收起全文  
segmentation fault原因 (C/C++学习)
发布于 2007-10-31 19:18 阅读:37230 评论:0 标签: fault segmentation 方法

  PHP总是和linux/unix、C/C++分不开的,郁闷。继续C/C++的学习吧。

  今天起,要向学习PHP和MySQL一样,把遇见的问题都记录下来,以提醒自己。

  “segmentation fault”是今天遇见的错误。查找资料,说是一些细节错误:
    1。 针没有赋值;
    2。 量赋值类型有错误。
    3。 最主要的错误就是声明了指针,但是没有初始化 ,结果再后来的时候进行间接引用 ,就出现问题了。

  同一个错误的提示总是有多个造成错误的情况的可能。printf输出时,类型有错误便会有这个问题。

       比如:

以下是代码片段:
char *val;
val = (char *)malloc(valsize + 1);
memset(val, 69, valsize);
val[valsize] = '\0';
printf("After memset : %s \n", *val);

  这时便会提示“segmentation fault”了。原因呢,就是多写了一“*”。改成下面的即可。

以下是代码片段:
printf("After memset : %s \n", val);

      参考的资料请看: 程序运行总是提示 segmentation fault (core dumped)是什么错啊?

  一篇更好的文章: 可恶的"Segmentation faults"之初级总结篇

展开全文  
收起全文  
指针的地址、定义时连等赋值、指针的连等赋值 (C/C++学习)
发布于 2006-02-06 12:13 阅读:23172 评论:2 标签: 指针

  1:指针的地址可以由以下方法得到:

    int a = 5;
    int *pA = &a;
    cout<<"The &pA is :"<<&pA<<endl;

  2:在定义的时候赋值是不能连等赋值的:

  例如下面

    int x = y = z = 5;

  此时,编译器会对y 、z报错。同样,对于指针也是如此,例如:

    int *m = *n = *w = 0;

  此时,编译器会对n 、w报错。

  3:对于指针的连等赋值建议使用地址连等赋值:

  对于以下程序,编译器不会报错:

    int *m =0 , *n = 0 , *w = 0;
    *m = *n = *w = 8;//把8改为x(x已定义赋值)也一样

  但是运行程序的时候会出现错误(至少对偶的机子是这样)。Windows XP提示:

    “0x004013bd”指令引用的“0x00000000”内存。该内存不能为“written”。

  所以建议使用:

       m = n = w = &x;           

展开全文  
收起全文  
[debug]*pAge=&howOld是错的 (C/C++学习)
发布于 2006-01-25 11:58 阅读:39972 评论:2 标签: C++

    书上142页有一个语句:

    unsign short int howOld = 50;
          unsign short int *pAge   = 0;
          *pAge = &howOld;

     初看一眼,还以为这又是C与C++的区别。今天急忙编译了一下,发现这又是书上的错误。编译器提示:invalid conversion from `int*' to `int'

附程序:

/*********************************************
*Developer:                 yayu;            *
*My email:                  xieyayu@163.com  *
*Development environment:   Dev-C++ 4.9.9.0; *
*********************************************/

#include <iostream>
#include <stdlib.h>

using namespace std;

int main(int argc, char *argv[])
{
 
  int *page=0;
  int age=100;
 
  *page=&age;
  //page=&age;
  //*page=age;
 
  cout<<"*page is : "<<*page<<endl;
  cout<<"page is : "<<page<<endl;
    
  return 0;
}

展开全文  
收起全文  
Dev-C++ 4.9.9.0在for中的定义为局部变量 (C/C++学习)
发布于 2006-01-23 12:24 阅读:42646 评论:2 标签: C++

  程序将提示:

     D:\yayu\C++Test\forDeZuoYongYu.cpp In function `int main(int, char**)':

    21 D:\yayu\C++Test\forDeZuoYongYu.cpp name lookup of `i' changed for new ISO `for'  scoping

    16 D:\yayu\C++Test\forDeZuoYongYu.cpp   using obsolete binding at `i'

     D:\yayu\C++Test\Makefile.win [Build Error]  [forDeZuoYongYu.o] Error 1

附程序:

/*********************************************
*Developer:                 yayu;            *
*My email:                  xieyayu@163.com  *
*Development environment:   Dev-C++ 4.9.9.0; *
*********************************************/

#include <iostream>
#include <stdlib.h>

using namespace std;

int main(int argc, char *argv[])
{
  //int i; 
   
  for(int i=0 ; i<5 ; i++)
  {
    cout<<"i : "<<i <<endl;
  }
   
  i=7;
 
  return 0;
}

展开全文  
收起全文  
一个工程只能有一个main函数 (C/C++学习)
发布于 2006-01-23 12:18 阅读:26863 评论:1 标签: C++

  很奇怪,偶开始在工程test.dev下建立了一个forDeZuoYongYu.cpp文件,出现了exe文件,偶再在这个工程下建立了test1.cpp文件,结果exe文件没了,还出了下面的提示:

  test1.cpp D:\yayu\C++Test\test1.o(.text+0x6) multiple definition of `main'

  forDeZuoYongYu.cpp D:\yayu\C++Test\forDeZuoYongYu.o(.text+0x6) first defined here

   D:\yayu\C++Test\Makefile.win [Build Error]  [Test.exe] Error 1 

  百思不得其解,咨询了一下李马才明白"你可以有多个c/cpp文件来组成这个工程,这些文件中可以包括多个函数或者类的实现,但是main只能有一个"  

展开全文  
收起全文  
哈,main函数也可以自己调用自己 (C/C++学习)
发布于 2006-01-23 11:29 阅读:47033 评论:1 标签: C

  有意思!

/*********************************************
*Developer:                 yayu;            *
*My email:                  xieyayu@163.com  *
*Development environment:   Dev-C++ 4.9.9.0; *
*********************************************/

#include
#include

using namespace std;

int main(int x, int y)
{
  if(x||y)
  {
    cout <<"Get x:";
    cin >> x;
    cout <<"Get y:";
    cin >> y;
  }
     
  if(x    main(x,y);   
  else   
    cout<<"You are good!";
   
  return 0; 
}

展开全文  
收起全文  
false和true的值 (C/C++学习)
发布于 2006-01-22 17:56 阅读:32795 评论:0 标签:

  想不到不同的编译器对false和true的输出还不一样。我用Dev-C++输出没有内容。但alexu告诉偶用VC可以输出1和0。晕!

 偶测试用的程序如下:

/*********************************************
*Developer:                 yayu;            *
*My email:                  xieyayu@163.com  *
*Development environment:   Dev-C++ 4.9.9.0; *
*********************************************/

#include <iostream>
#include <stdlib.h>

using namespace std;

int main(int argc, char *argv[])
{
 
  cout<<"\nfalse is : "<<false;
  cout<<"\ntrue is : "<< true;  
  return 0;
}

展开全文  
收起全文  
enum和#define、const (C/C++学习)
发布于 2006-01-22 17:02 阅读:13750 评论:1 标签:

  在enum中定义的量和#define、const定义的量都是常量。定义后都不能用"++"的形式自增和自减。

  一:在定义下面后,Monday就是常量“1”:

      enum Days
         {
           Sunday,Monday,tuesday,Wedneday,Tursday,Friday,Saturday
         };

  如果执行程序:

    cout<<"\n ++Monday is : "<< ++Monday;

  则编译器会提示:

     no match for 'operator++' in '++Monday'

  在定义下面: 

    #define BDefine 10

  如果程序执行下面语句:

    cout<<"\n ++BDefine is : "<< ++BDefine;

  编译器会提示:

    non-lvalue in increment

  定义如下const后:

    const int CConst = 20;

  如果程序执行下面语句:

    cout<<"\n ++CConst is : "<< ++CConst;

  编译器会提示:

    increment of read-only variable `CConst'

附测试程序:(源于书中34页清单3.7)

/*********************************************
*Developer:                 yayu;            *
*My email:                  xieyayu@163.com  *
*Development environment:   Dev-C++ 4.9.9.0; *
*********************************************/

#include
#include
#define BDefine 10
const int CConst = 20;

using namespace std;

int main(int argc, char *argv[])
{
 
  enum Days
       {
         Sunday,Monday,tuesday,Wedneday,Tursday,Friday,Saturday
       };
      
  Days today;
  today = Monday;
 
  int AEnum;
  AEnum = Monday;
   
  if(today == Sunday || today == Saturday)
  {
    cout<<"\n today is : "<< today;
    cout<<"\n Yayu love the weekends! \n";
  } 
  else
  {
    cout<<"\n today is : "<< today;
    cout<<"\n Back to work!. \n";
  }
 
  cout<<"\n AEnum is : "<< AEnum;
  cout<<"\n Monday is : "<< Monday;
 
  cout<<"\n ++AEnum is : "<< ++AEnum;
  //cout<<"\n ++Monday is : "<< ++Monday;
  //cout<<"\n ++BDefine is : "<< ++BDefine;
  //cout<<"\n ++CConst is : "<< ++CConst;
 
  return 0;
}

展开全文  
收起全文