Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Тестовые примеры / ch13 / msg2 / msg_cls2
.C/* client program: low-level RPC APIs */
#include <thread.h>
#include <signal.h>
#include "msg2.h"
#include "RPC.h"
extern "C" int thr_sigsetmask(int, const sigset_t *, sigset_t *);
#define MAX_THREAD 200
typedef struct {
char *host;
char *msg;
} MSGREC;
thread_t thread_list[MAX_THREAD];
/* function executed by a thread to send a message */
void* send_msg( void* ptr )
{
int res;
MSGREC *pRec = (MSGREC*)ptr;
/* set thread's signal mask to everything except SIGHUP */
sigset_t setv;
sigfillset(&setv);
sigdelset(&setv, SIGHUP);
if (thr_sigsetmask(SIG_SETMASK,&setv,0)) perror("thr_setsigmask");
/* create a client handle to communicate with a host */
RPC_cls cl( pRec->host, MSGPROG, MSGVER, "netpath");
if (!cl.good()) thr_exit( &res );
/* call the remote host to print the message to system console */
(void)cl.call( PRINTMSG, (xdrproc_t)xdr_string, (caddr_t)&(pRec->msg),
(xdrproc_t)xdr_int, (caddr_t)&res);
/* delete dynamic memory */
delete pRec->msg;
delete pRec->host;
delete pRec;
/* check RPC function execution status */
if (res!=0) cerr << "clnt: call printmsg fails\n";
int *rcp = new int(res);
thr_exit( rcp );
return 0;
}
/* Function to create a thread for a user message */
int add_thread( int& num_thread )
{
char host[60], msg[256];
thread_t tid;
int res;
/* get remote host name and message from a user */
cin >> host >> msg;
if (cin.eof()) return RPC_FAILED; /* normal return */
if (!cin.good()) { /* I/O error detected */
perror("cin");
return RPC_FAILED;
}
/* create dynamic memory for a message text and a host name */
MSGREC *pRec = new MSGREC;
pRec->host = new char[strlen(host)+1];
pRec->msg = new char[strlen(msg)+1];
strcpy(pRec->host,host);
strcpy(pRec->msg,msg);
/* create a suspended thread to process the message */
if (thr_create( 0, 0, send_msg, pRec, THR_SUSPENDED, &tid ))
{
perror("thr_create");
return RPC_FAILED;
}
else if (num_thread>= MAX_THREAD)
{
cerr << "Too many threads created!\n";
return RPC_FAILED;
}
else { /* create a thread successfully */
thread_list[num_thread++] = tid;
cout << "Thread: " << (int)tid << " created for msg: '"
<< msg << " [" << host << "]\n";
}
return RPC_SUCCESS;
}
/* client main function */
int main(int argc, char* argv[])
{
int num_thread=0;
thread_t tid;
int *res;
/* Set concurrency level */
if (thr_setconcurrency(5)) perror("thr_setoncurrency");
cout << "No. LPWs in process: " << getpid() << " is: "
<< thr_getconcurrency() << endl;
/* create a thread to send each mesg input by a user */
while (add_thread(num_thread)==RPC_SUCCESS) ;
/* set each thread's priority and launch it */
for (int i=0; i < num_thread; i++)
{
thr_setprio(thread_list[i],i);
thr_continue(thread_list[i]);
}
/* wait for every thread to terminate */
while (!thr_join(0,&tid,(void**)&res))
{
cerr << "thread: " << (int)tid
<< ", exited. rc=" << (*res) << endl;
delete res;
}
/* terminate the main tread */
thr_exit(0);
return 0;
}