Professional Documents
Culture Documents
(IPC)""()
""()
7-2
headtail
()headtail
"
"(template) - OS "(TPIPE)"
7.1TPIPE
------------------------------------------------------------------------------------------------------template<class DATA> class TPIPE: public Message{
private:
DATA
*pool;
int
pool_size;
int
head,tail;
unsigned
count;
public:
TPIPE(int psize=100);
~TPIPE();
virtual int far operator <<(DATA input);
virtual int far operator >>(DATA &output);
};
7.1 template<DATA>class TPIPE(TPIPE.H)
TPIPEpooltemplate
class:DATATPIPE<int>TPIPE<double>
<
>
TPIPEDATA
pool100
------------------------------------------------------------------------------------------------------/*TPIPE */
template<class DATA> TPIPE<DATA>::TPIPE(int psize=100)
{
Tau.DispatchMask=1;
/**/
pool=(DATA far *)farmalloc(sizeof(DATA)*size);
Tau.DispatchMask=0;
if( pool==NULL){
//pool_size0
pool_size=0;
return;
}
pool_size=psize;
count=0;
head=0;
tail=0;
}
/*TPIPE*/
template<class DATA> TPIPE<DATA>::TPIPE()
{
Tau.DispatchMask=1;
farfree(pool);
count = pool_size=0;
Tau.DispatchMask=0;
//
}
7.2 TPIPE(TPIPE.H)
TPIPE
TPIPE
headtail
(overwrite)
------------------------------------------------------------------------------------------------------template<class DATA>int far TPIPE<DATA>::operator << (DATA input)
{
if( count == pool_size)
return ERR_PIPEFULL;
pool[tail] = input;
//
tail++;
count++;
if( tail == pool_size)
return ERR_NOERROR;
//
tail=0;
}
7.3 TPIPE :: operator << (DATA input)
------------------------------------------------------------------------------------------------------template<class DATA>int far TPIPE<DATA>::operator >> (DATA &output)
{
if(!count)
return ERR_PIPEEMPTY;
output = pool[head];
//pool
head++;
//
count--;
if( head == pool_size) head=0;
return ERR_NOERROR;
}
7.4 TPIPE :: operator >>(DATA &output)
TPIPE=/C++
=(copy)
DATATPIPE
7.5TPIPE
------------------------------------------------------------------------------------------------------#include "tauos.h"
#define Tick_Rate 20
#include "conio.h"
TPIPE<double> sec;
//
//
}
}
void far print(void far *arg)
{double s=0.0;
while(s<=20.0){
sec >>s;
//sec
gotoxy(1,12);
cprintf("Execution Time : %lf seconds",s);
}
Tau.Shutdown =1;
//20
}
void TauMain(int,char **)
{
unsigned id;
Task.CreateSliceTask(id,second,NULL,4096,15);
Task.CreateSliceTask(id,print,NULL,4096,15);
clrscr();
/* Tick_Rate*/
Tau.Start(Tick_Rate);
}
7.5 (demo3.cpp)
(DEMO3.CPP)(5.28)void far
second(void *)double
secsecond
secprintprint
7-3
(port)()
(MSG Pack)
"" - OS
""
""MSGPack
------------------------------------------------------------------------------------------------------class far MSGPack : _node,public Message{
private:
unsigned far sign;
//
public:
MSGPack(){
sign=Tau.CurrentThreadID();}
unsigned far MSGID;
//ID
unsigned far Receiver;
//
unsigned Sender(){
return sign;
}
friend class far mbox;
};
7.6 class far MSGPack(COMM.H)
MSGPack""sign
Receiver
""MSGID
MSGPack
MSGPack
MSGPack
MSGPack
------------------------------------------------------------------------------------------------------#include <stdio.h>
#include "tauos.h"
#include <conio.h>
class far PORT: _qItem,public Message{
private:
mbox
far mail_list;
//
EVENT
msg;
int far
PutMessage(MSGPack *mpack);
unsigned inWait:1;
public:
PORT();
~PORT();
int far
hasMSG();
MSGPack *
GetMessage(int time_out=0);
friend class far COMMGR;
};
class far COMMGR : public Manager,public Message{
private:
_queue
PortList; //
void far
RegisterPort(PORT *p);
void far
DeletePort();
public:
int far
operator << (MSGPack * mpack);
friend classPORT;
};
7.7 class PORT class COMMGR(COMM.H)
PORTmbox_listputget
{
Tau.DispatchMask =1;
_list :: insert((_node*)mpack);
Tau.DispatchMask =0;
//
}
MSGPack far * mbox :: get()
{
MSGPack far *mpack=NULL;
if( !_list :: is_empty()){
Tau.DispatchMask =1;
// MSGPack
mpack = (MSGPack far *)_list :: erase();
Tau.DispatchMask =0;
}
// NULL
return mpack;
}
7.9 mbox :: putmbox :: get(COMM.CPP)
PORT
PORTRegisterPortCOMMGR
------------------------------------------------------------------------------------------------------PORT :: PORT()
{
//ID_qItemprio
_qItem::priority(Tau.CurrentThreadID());
//
COMMGR.RegisterPort(this);
}
//
return ERR_NOERROR;
}
7.12 MSGPack,COMMGR ::operator <<(MSGPack *)
PORT ::PutMessage(MSGPack *)(COMM.CPP)
COMMGR ::operator <<(MSGPack *)_queue_focus
PORT_focus
(race condition)
COMMGR ::operator
<<(MSGPack *)PortList_queue
PORT
PORT ::GetMessage()mboxMSGPackmail_list
PORT ::GetMessage()time out
------------------------------------------------------------------------------------------------------MSGPack *PORT :: GetMessage(int time_out)
{ MSGPack *p;
if(mail_list.isEmpty()){
//mail_list
inWait =1;
msg.pend(time_out); //
inWait =0;
}
p=mail_list.get();
//MSGPack
return p;
}
7.13 MSGPack PORT::GetMessage()(COMM.CPP)
Doc()
StringMsgShowDoc
-------------------------------------------------------------------------------------------------------
#include <stdio.h>
#include <conio.h>
#include "tauos.h"
class StringMsg : public MSGPack{
private:
char *string;
public:
void String(char *s)
{
char *String()
{
};
//MSGPack
string = s;
return string;
}
}
//ID
str->String(string[i]);
//
//COMM
}
}
TASK ShowDoc(ARG)
{
PORT
p;
StringMsg *str;
str = (StringMsg *)p.GetMessage(100); //
if( str == NULL) printf("Time out!!\n");
else{
printf(str->String());
MaskDispatcher;
delete str;
ReleaseDispatcher;
}
str = (StringMsg *)p.GetMessage(100);
if(str == NULL) printf("Time out!!\n");
else{
printf(str->String(),str->Sender(),PTID);
printf("Message ID is %u\n",str->MSGID);
MaskDispatcher;
delete str;
ReleaseDispatcher;
}
TauShutDown;
}
void TauMain(int, char**)
{
unsigned id1,id2;
TauCreateSliceTask(id1,ShowDoc,NULL,4096);
TauCreateSliceTask(id2,Doc,(ARG)&id1,4096);
TauStart(50);
printf("Press any key to quit...");
getch();
}
7.14
7-4
(IPC)""(
)""()
""
hand shaking(global variables)
()Ebenchmark