#define SSLEEP 1 /* sleeping on high priority */ #define SWAIT 2 /* sleeping on low priority */ #define SRUN 3 /* running */ #define SIDL 4 /* process/thread being created */ #define SSTOP 6 /* thread being traced */
/* flag codes */
#define SLOAD 01 /* in core */ #define SSYS 02 /* system thread/process */ #define SLOCK 04 /* process cannot be swapped */ #define SSWAP 010 /* process is being swapped out */ #define STRC 020 /* thread is being traced */ #define SWTED 040 /* another tracing flag */ #define STERM 100 //线程要被终止
typedefint(*ThreadProc)(int*pParam);
/*
* 线程处理结构
*/ struct Thread { char t_stat; char t_flag; char t_pri;/* priority, negative is high */ char t_time;/* resident time for scheduling */ char t_cpu;/* cpu usage for scheduling */ char t_nice;/* nice for scheduling */ int t_tid;/* unique thread id */ int t_ttyp;/* controlling tty */ int t_index;/* internal index from 0 to NTHREAD_PROC-1 */ int t_pid;/* process id of parent */ int t_ustackaddr;/* address of stack in userspace */ int t_ustacksize;/* size of userstack (*64 bytes) */ int t_wchan;/* event thread is awaiting */ char t_name[12];
ThreadProc t_entry;/* pointer to user entry */ int*t_param;/* param passed to thread proc */ struct proc *t_proc;/*parent process pointer */ }Threads[NTHREAD];
proc结构改为: struct proc { char p_stat; char p_flag; char p_nice;/* nice for scheduling */ char p_time;/* resendial time for scheduler */ char p_sig;/* signal number sent to this process */ char p_uid;/* user id, used to direct tty signals */ int p_ttyp;/* controlling tty */ int p_pid;/* unique process id */ int p_ppid;/* process id of parent */ int p_addr;/* address of swappable image */ int p_size;/* size of swappable image (*64 bytes) */ int*p_textp;/* pointer to text structure */ struct Thread *p_threads[NTHREAD_PROC]; int p_threadNum;//总线程数
int p_actThreadNum;//运行着的线程数
int*p_thdsysentry;/* pointer to sys entry ThreadSysEntry*/ } proc[NPROC];
--------------选自光盘文件/usr/personal/kernel/include/Thread_U.h------------ #define USIZE (4096/64) #define THREAD_SSIZE 1024 #define SSIZE 4096 /*
* 线程上下文
*/ struct U_Thread { int u_rsav[2];/* save r5,r6 when exchanging stacks */ int u_fsav[25];/* save fp registers */ /* rsav and fsav must be first in structure */ char u_segflg;/* flag for IO; user or kernel space */ char u_error;/* return error code */ char u_uid;/* effective user id */ char u_gid;/* effective group id */ char u_ruid;/* real user id */ char u_rgid;/* real group id */ int u_threadp;/* pointer to Thread structure */ char*u_base;/* base address for IO */ char*u_count;/* bytes remaining for IO *///read/write to/from u_base
char*u_offset[2];/* offset in file for IO */ int*u_cdir;/* pointer to inode for current directory */ char u_dbuf[DIRSIZ];/* current pathname component */ char*u_dirp;/* current pointer to inode */ struct{/* current directory entry */ int u_ino;//the directory u_name's inode number in the disk.
char u_name[DIRSIZ]; } u_dent; int*u_pdir;/* inode of parent directory of dirp */ int u_ofile[NOFILE];/* pointers to file structures of open files */ int u_arg[5];/* arguments to current system call */
int u_qsav[2];/* label variable for quits & interrupts */ int u_ssav[2];/* label variable for swapping */ int u_utime;/* this process user time */ int u_stime;/* this process system time */ int u_cutime[2];/* sum of childs’ utimes */ int u_cstime[2];/* sum of childs’ stimes */ int*u_ar0;/* address of users saved R0 */ int u_prof[4];/* profile arguments */ char u_intflg;/* catch intr from sys */ char u_pad; char u_kernstack[802];// 每个线程的内核栈
char u_uid;/* effective user id */ char u_gid;/* effective group id */ char u_ruid;/* real user id */ char u_rgid;/* real group id */
int u_uisa[16];/* prototype segmentation addresses */ int u_uisd[16];/* prototype segmentation descriptors */ int u_tsize;/* text size (*64) */ int u_dsize;/* data size (*64) */ int u_ssize;/* stack size (*64) */
//线程栈分配记录,为了提高效率,可采用链表实现。这里简单起见,采用数组。
structmap u_stackmap[NTHREAD_PROC]; int u_sep;/* flag for I and D separation */ struct proc *u_procp;
int u_curthd;//Index for current thread structure in u_threads