You are on page 1of 14

7-1

(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 second(void far *arg)


{
while(1){
//tick
sec << Tau.GetTime()/20.0;

//

}
}
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

print

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

------------------------------------------------------------------------------------------------------class far mbox : _list{


public:
~mbox();
int is_empty()
{ return _list :: is_empty(); }
void far put(MSGPack *mpack);
MSGPack far *get();
};
7.8
class mbox(COMM.H)
------------------------------------------------------------------------------------------------------void mbox :: put(MSGPack *mpack)

{
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);
}

void COMMGR.RegisterPort(PORT *p)


{
MaskDispatcher;
PortList + (_qItem*)p;
//PortList
ReleaseDispatcher;
}
7.10 PORTPORT()COMMGR::RegisterPort()
(COMM.CPP)
PORTPORTCOMMGR
DeletePort
TaskTask.KillTaskPORT()
------------------------------------------------------------------------------------------------------PORT :: ~PORT()
{
COMMGR.DeletePort();
//
}
void COMMGR :: DeletePort()
{ unsigned tid = Tau.CurrentThreadID(); //
MaskDispatcher;
PortList.toHead();
do{
if( (PortList.focus())->priority() == tid){
//PortList
PortList.remove_focus();
break;
}
}while(PortList>>1);
ReleaseDispatcher;
}

7.11 PORT ~PORT()COMMGR :: DeletePort()


(COMM.CPP)
MSGPackCOMMGR ::operator <<(MSGPack
*)MSGPackReceiver
PortListPORTPORT
PORT ::PutMessage(MSGPack *)PORTmbox
(inWait)PORT ::PutMessage
------------------------------------------------------------------------------------------------------int PORT ::PutMessage(MSGPack *mpack)
{
mail_list.put(mpack);
//MSGPack
if(inWait) msg.post(); //
return ERR_NOERROR;
}
int COMMGR ::operator <<(MSGPack *mpack)
{
if( PortList.is_empty())
return ERR_BADID;
PORT *p = (PORT *)PortList.first();
PORT *h = p;
while(1){
if( p->priority() == mpack->Receiver){
//
p->PutMessage(mpack);
break;
}
p = (PORT *)p->next();
//
if( p==h) return ERR_BADID;
}

//

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;

}
}

TASK Doc(ARG arg)


{ unsigned tid=*(unsigned)arg;
char*string[2]={
"This is a demo of communication manager\n",
"Message is passed from Task:%u to Task:%u\n"
};
StringMsg *str;
TauTimeDelay(25);
for(int i=0; i<2; i++ ){
MaskDispatcher;
str = new StringMsg;
//
ReleaseDispatcher;
str->Receiver = tid;
//
str->MSGID =1;

//ID

str->String(string[i]);

//

COMM << str;

//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

This is a demo of communication manager


Message is passed from Task:61443 to Task:61442
Message ID is 1
Press any key to quit...
Tau-OS Shuts Down!
Thread Count : 1

7-4
(IPC)""(
)""()
""
hand shaking(global variables)

()Ebenchmark

You might also like