【原创】TCP Socket 简单练习 --- 线程池实现并发服务器

本文提供了一次使用线程池实现TCP并发服务器的简单练习,详细介绍了服务器函数执行流程,包括初始化、线程池创建、任务处理等,并给出了Makefile文件、服务器及客户端代码,展示了运行结果。
摘要由CSDN通过智能技术生成


【原创】TCP Socket 简单练习 --- 线程池实现并发服务器


服务器函数执行流程

main

init_system

creat_pthread_pool

child_work


thread_manager


task_manager

process_client


monitor


sys_clean


Makefile文件

CC = gcc
TARGET = pthread_pool
SRC = pthread_pool.c base.c
OBJECT = pthread_pool.o  base.o
INCLUDES = -I./
LDFLAGS = -lpthread

all:$(TARGET)

$(OBJECT):$(SRC)
	$(CC) -c $(INCLUDES) ${SRC}

$(TARGET):$(OBJECT)
	$(CC) -o $@ $(OBJECT) $(LDFLAGS)

.PHONY:clean

clean:
	@rm -rf $(OBJECT) $(TARGET) *~

服务器代码

头文件

#ifndef __PTHREAD_POOL_H__

#define __PTHREAD_POOL_H__

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#include <assert.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <errno.h>

#define THREAD_MAX_NUM	100     /* max number of thread. */
#define THREAD_DEF_NUM	20      /* by default ,number of thread. */
#define THREAD_MIN_NUM	5       /* min number of thread pool. */
#define LISNUM<span style="white-space:pre">	</span>5
#define PORT	9001
#define MAXBUF	1024


/*
 * *ds of the every task. make all task in a single link
 */
//任务结构节点,用于描述每个任务的具体属性
typedef struct task_node
{
	void *arg;                              /* fun arg. */
	void *(*fun)(void *);                   /* the real work of the task. */
	pthread_t		tid;            /* which thread exec this task. */
	int			work_id;        /* task id. */
	int			flag;           /* 1: assigned, 0: unassigned. */
	struct task_node	*next;
	pthread_mutex_t		mutex;          /* when modify this ds and exec the work,lock the task ds. */
} TASK_NODE;


/*
 * *the ds  of the task_queue
 */
//任务队列结构,用于控制整个任务队列
typedef struct task_queue
{
	pthread_mutex_t		mutex;
	pthread_cond_t		cond;   /* when no task, the manager thread wait for ;when a new task come, signal. */
	struct task_node	*head;  /* point to the task_link. */
	int			number; /* current number of task, include unassinged and assigned but no finished. */
} TASK_QUEUE_T;


/*
 * *the ds of every thread, make all thread in a double link queue.
 */
//线程结构节点,用于描述每个线程的具体属性
typedef struct pthread_node
{
	pthread_t		tid;    /* the pid of this thread in kernel,the value is  syscall return . */
	int			flag;   /*  1:busy, 0:free. */
	struct task_node	*work;  /*  if exec a work, which work. */
	struct pthread_node	*next;
	struct pthread_node	*prev;
	pthread_cond_t		cond;   /* when assigned a task, signal this child thread by manager. */
	pthread_mutex_t		mutex;
} THREAD_NODE;


/*
 * *the ds of the thread queue
 */
//线程队列结
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值