Larbin 搜索引擎源码赏析——(一)搜索引擎的main函数

本文介绍了一个使用C++编写的网络爬虫程序的核心部分,包括信号处理、多线程启动及网络服务器的搭建等关键技术点。通过忽略SIGPIPE信号避免了向未读端的管道写入数据时程序异常终止的问题,并启动多个线程负责不同的任务,如URL检查、获取资源等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. // Larbin
  2. //fileName: main.cc
  3. // Sebastien Ailleret
  4. // 09-11-99 -> 08-03-00
  5. //comment:anzijin
  6. //2008.12.25
  7. #include <unistd.h>
  8. #include <iostream.h>
  9. #include <signal.h>
  10. #include <time.h>
  11. #include <netdb.h>
  12. #include <sys/socket.h>
  13. #include <sys/types.h>
  14. #include <sys/stat.h>
  15. #include <fcntl.h>
  16. #include "global.h"
  17. #include "xutils/GenericFifo.h"
  18. #include "xutils/url.h"
  19. #include "xutils/text.h"
  20. #include "xutils/threads.h"
  21. #include "xfetcher/checker.h"
  22. #include "xsequencer/sequencer.h"
  23. #include "xfetcher/fetchOpen.h"
  24. #include "xfetcher/fetchPipe.h"
  25. #include "xinterf/input.h"
  26. #include "xutils/debug.h"
  27. #include "xutils/webserver.h"
  28. // If this thread terminates, the whole program exits
  29. int main (int argc, char *argv[]) 
  30. {
  31.     //可以不包含参数,直接写调用
  32.   // create all the structures
  33.   global glob(argc, argv);  //大部分操作在构造函数中完成,除了getProxyFds函数的调用
  34.   // let's ignore SIGPIPE
  35.   static struct sigaction sn, so;
  36.   sigemptyset(&sn.sa_mask);//初始化由set指定的信号集,信号集里面的所有信号被清空
  37.   sn.sa_flags=SA_RESTART;  //系统调用就会重启
  38.   sn.sa_handler = SIG_IGN;  //目前SIG_IGN的作用还不清楚,似乎不是一个函数
  39.   
  40.   /*
  41.   int sigaction(int signum, const struct sigaction * act, struct sigaction * oldact);
  42.   函数说明:sigaction()会依照参数signum制定的信号编号来设置该信号的处理函数。参数signum
  43.   可以指定为SIGKILL和SIGSTOP以外的所有信号
  44.   参数结构sigaction定义如下:
  45.   struct sigaction
  46.   {
  47.     void (*sa_handler)(int);
  48.     sigset_t sa_mask;
  49.     int sa_flags;
  50.     void (*sa_restorer)(void);
  51.   }
  52.   sahandler: 此参数和signal()的参数handler相同,代表新的信号处理函数,其他意义请参考signal()
  53.   sa_mask: 用来设置在处理该信号时暂时将sa_mask指定的信号搁置。
  54.   sa_restorer 此参数没有使用
  55.   sa_flags 用来设置信号处理的其他相关操作,下面的值可用
  56.     SA_RESETHAND/SA_ONESHOT    将信号的处理函数重置为缺省值
  57.     SA_RESTART 被信号中断的系统调用会自行重启。
  58.     SA_NOMASK/SA_NODEFER 在处理此信号未结束前不理会此信号的再次到来,那么在该信号处理函数运行时,内核将不会阻塞该信号
  59.     
  60.   关于返回值:执行成功则返回0,如果有错误则返回-1
  61.     错误代码:
  62.         EINVAL 参数signum不合法,或是企图拦截SIGKILL/SIGSTOPSIGKILL信号
  63.         EFAULT 参数act,oudact的指针地址无法存取
  64.         EINTR 此调用被中断
  65.   
  66.   SIGPIPE 信号表示:当向一个没有读中端的管道写数据时产生该信号。
  67.   */
  68.   
  69.   if (sigaction(SIGPIPE, &sn, &so)) //用来检测和改变信号行为且功能强大的一个函数 
  70.   {
  71.     cerr << "Unable to disable SIGPIPE/n";
  72.   }
  73.   // Start all threads
  74.   crash("Launch all threads");
  75.   if (startThread(startSequencer, NULL)   //startSequencer函数位于sequencer文件中
  76.       || startThread(startFetchBlock, NULL) //startFetchBlock函数位于fetchOpen文件中
  77.       || startThread(startFetchNonBlock, NULL) //startFetchNonBlock函数位于fetchOpen文件中
  78.       || startThread(startFetchPipe, NULL) //startFetchPipe函数位于fetchPipe文件中
  79.       || startThread(startInput, NULL)      //startInput函数位于input文件中
  80.       || startThread(startOutput, NULL)) //函数定义在output.h 文件中
  81.     {
  82.             cerr << "Unable to launch a thread/n";
  83.             exit(1);
  84.   }
  85.   // Start the search
  86.   cerr << global::userAgent;
  87.   cerr << " is starting its search/n";
  88.   check( new url(newString(global::firstUrl), global::depthInSite) );  //check函数在checker文件中,check函数只有一个参数
  89.   // Launch the web server
  90.   crash("Launch the webserver");
  91.   webserver();  //webserver函数位于webserver文件中
  92. }
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值