|
发表于 2010-8-26 10:42:31
|
显示全部楼层
回复【19楼】clingos
ls能给些代码看看吗?关于进程的?
-----------------------------------------------------------------------
我设计的多进程OS部分代码:
void* LoadProcess(char *path) => 丛disk加载进程文件到物理内存,path为文件路径
{
HANDLE hfile;
U32 fsize;
U32 *buf;
////
hfile =OpenFile(path,"rw");
if(hfile==0) return 0;
fsize =GetFileSize(hfile);
if(fsize==0) return 0;
buf =ProcessAlloc(fsize);
if(buf==0) return 0;
fsize =ReadFile(hfile,buf,fsize);
CloseFile(hfile);
if(fsize==0)
{
ProcessFree(buf);
return 0;
}
if(buf[1]!=0x01234567||buf[2]!=0x76543210)
{
DebugPrintf("Process File Err!\n");
ProcessFree(buf);
return 0;
}
buf[4] =GetAPIEntry();
//((U32*)proc)[4]
DebugPrintf("\n");
DebugPrintf("******** Process Info ********\n");
DebugPrintf("Path: %s\n",path);
DebugPrintf("Size: %dByte\n",fsize);
DebugPrintf("PID: %08XH\n",buf);
DebugPrintf("RO: %08XH - %08XH\n" ,buf[8],buf[9]);
DebugPrintf("RW: %08XH - %08XH\n" ,buf[10],buf[11]);
DebugPrintf("ZI: %08XH - %08XH\n" ,buf[12],buf[13]);
DebugPrintf("HeapSize: %dByte\n" ,ProcHeapSize(buf));
DebugPrintf("StackSize: %dByte\n" ,ProcStackSize(buf));
DebugPrintf("ProcSize: %dByte\n" ,ProcSize(buf));
DebugPrintf("Priority: %d\n" ,ProcPriority(buf));
DebugPrintf("Name: %s\n" ,(U8*)buf+buf[17]);
DebugPrintf("Icon: %08XH\n" ,buf[18]);
DebugPrintf("Version: %s\n" ,(U8*)buf+buf[20]);
DebugPrintf("Date: %s\n" ,(U8*)buf+buf[21]);
DebugPrintf("Time: %s\n" ,(U8*)buf+buf[22]);
DebugPrintf("Desc: %s\n" ,(U8*)buf+buf[23]);
DebugPrintf("********************************\n");
return buf;
}
HANDLE CreateProcess (char *path, void *p_arg,char *cur_dir ) =>创建进程
{
STK *stk_cur,*stk_base;
TCB *ptcb;
void *proc;
EnterCritical();
if (CriticalNesting > 0)
{ //不能在中断中创建进程.
ExitCritical();
return 0;
}
ExitCritical();
ptcb =(TCB*)MemAlloc(sizeof(TCB));
if(ptcb==(TCB*)0)
{
return 0;
}
ptcb->OSTCBType =TCB_TYPE_PROCESS;
////加载进程
proc =(void*)LoadProcess(path);
if(proc==0)
{
MemFree(ptcb);
return 0;
}
////登记进程绝对路径
ptcb->Path =(char*)MemAlloc(StrLen(path));
StrCopy(ptcb->Path,path);
////登记进程当前路径
if(cur_dir)
{
StrCopy(ptcb->CurrentDir,cur_dir);
}
else
{
int i;
i=StrLen(path);
while(i)
{
if(path=='\\')
{
path='\0';
StrCopy(ptcb->CurrentDir,path);
break;
}
i--;
}
}
stk_base =(STK*)ProcStackStart(proc);
if(stk_base==(STK*)0)
{
MemFree(ptcb->Path);
MemFree(ptcb);
return 0;
}
stk_cur = StkInit((void(*)(void*))ProcROBase(proc), p_arg, stk_base, 0);
err = TCBInit(ptcb,ProcROBase(proc),ProcPriority(proc), stk_cur, stk_base, ProcStackSize(proc),OS_STAT_RDY,proc,ProcSize(proc));
if (err != OS_NO_ERR)
{
MemFree(ptcb->Path);
MemFree(ptcb);
return 0;
}
if (OSRunning == TRUE)
{
OS_ThreadSched();
}
return (HANDLE)ptcb;
}
void MMU_Sched(void) => 页面切换
{
if(ReadyTCB->PAStart)
{
if(ReadyTCB->PAStart!=CurrentTCB->PAStart)
{
MMU_Remap(ReadyTCB->VAStart,ReadyTCB->PAStart,ReadyTCB->MemSize);
}
}
}
示例:
(原文件名:未命名.JPG) |
|