⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uthread.h

📁 可模拟实现Linux用户级线程库的静态调用,规避了多线程库的竞态条件和复杂的同步问题.除源代码外还有详细的设计说明文档.
💻 H
字号:
/*学号:200620110268
  姓名:李海波*/

#define USECOND 1 
#define SECOND USECOND*1000000
#define MAX_THREAD_NUM 1024		
#define V_QUANTUM 20	
#define TIME_SLICE 3		


#include <stdio.h>
#include <signal.h>
#include <setjmp.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/times.h>

struct		thread_descr;	
enum		  rstatus;		
struct		thread_status ;	
struct 		queue;	
struct		node;	

					
void	cpu_dispatch();						
void	queue_insert( struct queue *q, struct node *n );	
long  uthreads_wait( struct thread_status  *ss );
int	  uthreads_init( void );					
int	  uthreads_spawn( int stacksize, void (* proc)(int), int param );
int	  uthreads_start( void );					
int	  uthreads_yield( void );				
void	uthreads_exit( void );				
void	uthreads_perror( char *user_info );		
	

static struct thread_descr 	thread[MAX_THREAD_NUM];	// 全局线程描叙信息
static struct queue 	*q_ready, *q_blocked, *q_zomby;	// 状态队列
static int			total_thread_num;			// 当前活跃的线程总数(凡是不处于僵死状态的线程就认为是活跃线程)
static int			running_tid;			// 正在运行的线程号
static struct node		*running_thread;			// 正在运行的线程节点地址
static int			last_errno;			// 上一次出错的错误号
static int			uthread_inits = 0;			// 线程库是否已经初始化,
							// 0代表否,1代表是
static int			uthread_starts = 0;			// uthreads_start函数是否已经运行,
							// 0表示未运行,1表示已运行
static sigjmp_buf		g_jbuf;				// 全局环境保存
static sigjmp_buf		jbuf[MAX_THREAD_NUM];		// 线程环境保存

//一些辅助变量
int m=0;
int x=0;
int add=0;
int test[MAX_THREAD_NUM];
struct node *temp;



// 线程运行状态信息
enum rstatus
{
	READY,		// 此时线程可调度
	RUNNING,	// 线程正在运行
	BLOCKED,	// 线程由于各种原因阻塞
	ZOMBY,		// 线程已结束
};

// 线程运行统计信息
struct thread_status  
{
	long total_time;		// 总用时(微秒)
	long run_time;		// 运行时间(微秒)
	long bursts;		// 获调度次数
};

// 线程描述信息
struct thread_descr
{
	int	   t_id;			// TID(0 - MAX_THREAD_NUM)
	enum   rstatus	r_status;			// 线程运行状态信息
	struct thread_status 	s_status;			// 线程运行统计信息
	void	 (*start_func)(int);		// 线程对应执行的函数
	int		 parameter;		// 函数的参数
	long	 quantum;			// 线程量子值
	char	 *stack;			// 栈地址
	int		 stack_size;		// 栈大小
};

// 队列
struct queue
{
	struct node	*head;		// 队列头
	int		num;		// 队列中节点个数
};

// 队列节点
struct node
{
	long		tid;		// 节点所代表线程的TID
	struct node	*next;		// 指向下一个节点的指针
};




// 错误信息
char *errinfo[] = {
	"No.0 无错误信息",
	"No.1 线程库已经初始化,无需再次初始化",
	"No.2 uthread_start已经运行",
	"No.3 在开始运行线程之前,应该创建至少一个线程",
	"No.4 系统调用函数出错:signal函数出错,无法设置对信号的响应",
	"No.5 无足够的可分配内存空间",
	"No.6 栈空间大小应为正值",
	"No.7 线程所执行程序指针应为非空",
	"No.8 线程库尚未初始化",
	"No.9 无线程存在",
	"No.10 无线程出于僵死状态"
};





	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -