资源简介
操作系统课程项目,在linux下用c语言实现了多线程web服务器。可以选择不同的调度算法,来执行web请求,有FCFS, SJF。采用线程池设计思想实现。
代码片段和文件信息
#include
#include
#include
#include
#include
#include“myhttpd.h“
#include“queue.h“
#include“schedule.h“
#include“process.h“
#include“option.h“
control* initializeControl ( );
void releaseResource ( control* cntl );
void usage ( void );
int main(int argc char* argv[])
{
choose(&argc argv);
int ret i;
gClientSockfd = 0;
if(option.help == 1)
{
usage();
exit(0);
}
if(option.debug == 1)
{
option.log = 0;
}
else
{ /* make the process daemon */
pid_t p;
p = fork();
if ( p < 0 )
printError ( “cannot fork new process“ );
else if ( p != 0 )
exit ( 0 );
setsid ( );
chdir ( option.rootDirectory );
umask ( 0 );
}
if ( option.log == 1)
{
/* the log file should be in the log directory which is the source file directory*/
strcat ( option.logDirectory “/“ );
strcat ( option.logDirectory option.logFilename );
}
/* initialize the global control struct */
control* cntl = initializeControl ();
/* setup the server sock */
gSockfd = socket( AF_INET SOCK_STREAM 0 );
if ( gSockfd < 0 )
printError(“printError opening socket“);
struct sockaddr_in serverAddr;
bzero( (char* ) &serverAddr sizeof(serverAddr) );
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = INADDR_ANY;
serverAddr.sin_port = htons(option.port);
ret = 1;
setsockopt ( gSockfd SOL_SOCKET SO_REUSEADDR &ret sizeof(ret) );
if ( bind( gSockfd (struct sockaddr *) &serverAddr sizeof(serverAddr) ) < 0 )
printError(“cannot bind“);
listen ( gSockfd 10 );
pthread_t exeThread[option.threadNum];
pthread_t queueThread;
pthread_t schedThread;
/* create execution thread */
for ( i = 0 ; i < option.threadNum ; i++ )
{
ret = pthread_create( &exeThread[i] NULL execute cntl );
if ( 0 != ret )
printf(“cannot create execution thread No.%d“ i );
}
/* create queue thread and schedule thread */
cntl->queueLength++;
ret = pthread_create( &queueThread NULL queue cntl );
if ( 0 != ret )
printError(“cannot create queuing thread“);
sleep ( option.queuingTime );
cntl->queueLength--;
ret = pthread_create( &schedThread NULL schedule cntl );
if ( 0 != ret )
printError(“cannot create scheduling thread“);
/* release request queue */
if ( option.debug )
{
char final[20];
scanf ( “%s“ final );
if ( 0 == strncmp ( final “exit“ 4 ) )
{
close ( gSockfd );
releaseResource ( cntl );
return 0;
}
}
else
{
/* wait for the other threads to complete */
pthread_join ( queueThread NULL );
pthread_join ( schedThread NULL );
for ( i = 0 ; i < option.threadNum ; i++ )
pthread_join ( exeThread[i] NULL );
}
}
/*print error message and exit*/
void printError(const char* msg)
{
perror(msg);
exit(1);
}
void getTime ( char* str )
{
char ntime[64];
struct tm* now;
time_t tm;
time ( &tm );
now = gmtime ( &tm );
strftime ( ntime sizeof(ntime) “[ %d/%b/%Y:%H:%M:%S -0400]“ now );
strcpy( str ntim
- 上一篇:基于Qt的2048游戏实现
- 下一篇:椭圆曲线密码ECC算法实现源码C++
相关资源
- QtWebApp
- 简易web服务器的设计与实现
- 基于STM32F407的W5500 tcpserver(官网例程
- 高性能服务器代码(50_06th_server_thre
- C++ sql2008 WebServer通讯.docx
- VC操作SQLSERVER数据库
- 一个简单而强大的基于MFC的web server源
- 基于C++和数据库SQL server开发的商品销
- 百度地图二次开发汇总
-
minibl
ink c++ qt 浏览器 - 功能全面的mfc Web浏览器
- 使用C#创建webservice服务,并使用服务
- C++_WebService_Demon
- 简单web浏览器设计
- 基于C++和SQL Server开发的商品销售管理
- 宾馆管理系统(C++MFC)数据库课程设
- c++和SqlServer做的图书管理系统
- Windows下基于ModbusTcp的Server端开发C语言
- DDE server VC++
- 畅捷通T+相关插件CellWeb5
- 网络编程HttpServer c++实现
- SQL server compact 3.5
- 基于C语言和SQL SERVER数据库实现的图书
- 进销存管理系统MFC SqlServer编程
- 服务端和客户端(MFC CSocket)
- IEC104规约Server及Client实现
- VS2015 C++操作WebService
- mfc配合listbox操作数据库
- C/C++使用ODBC操作SQL server数据库
- C/C++使用ODBC连接SQL server数据库完整流
评论
共有 条评论