LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-26-2004, 03:45 AM   #1
Bostonian
LQ Newbie
 
Registered: Dec 2004
Distribution: SuSE 9.0
Posts: 4

Rep: Reputation: 0
conceptual server design question


Hi Everyone,

I'm currently working on a server software and it's still on a design stage.

In short, here's the server's model: server process starts, then it forks a child processes to listen on each unique iport pair configured. Forked processes use create threads to respond to requests. Parent and child processes and threads, all have to share the same in-merory database. The database should be updated sometimes on the fly. Only the parent process can initiate an update. I've got to pause every activity going on in the server while updating. Should it be done with semaphores for each thread and forked child process? WHat is the correct way of doing this?

I'm just starting with Linux devevlopment, so, sorry for questions like there. Try to make better soon.

I would appreciate any help.

Thanks a lot!
 
Old 12-27-2004, 12:09 AM   #2
cppkid
Member
 
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185

Rep: Reputation: 30
Hi there!
Yup i don't know why but there is no function provided in pthread library to pause a thread, as we have Suspend/Resume system in windows. Solaris also support suspending a thread, but in linux you have to suspend a thread using semaphores. I have written a sort of library for it, you can use it i am sending you the source code for these functions.

Code:
#include "SysThread.h"
#include<iostream.h>
#include<unistd.h>
//_____________________________________________________________________________________________
//_____________________________________________________________________________________________
//_________________________________CONSTRUCTER_________________________________________________
//_____________________________________________________________________________________________
CSysThread::CSysThread()
{
int nRet;

	ThreadFunction = (char (*)(void*))Dummy;
	m_pObjThdFn = NULL;
	m_nState = QUITTED;
	m_nSuspend = 0;
	m_ThdID = 0;
	m_nErr = 0;

	pthread_mutex_init(&m_SuspendMutex,NULL);		// Return Check
	pthread_cond_init(&m_ResumeCond,NULL);		// Return Check

	nRet = pthread_create(&m_ThdID, NULL, ThdFn,this);
	if(nRet != 0)
	{
		m_nErr = 1;
		return;
	}
	m_nState = RESUMED;
	pthread_mutex_lock(&m_SuspendMutex);		// Return Check
	Suspend();
}
//_____________________________________________________________________________________________
//_____________________________________________________________________________________________
//_________________________________DESTRUCTER__________________________________________________
//_____________________________________________________________________________________________
CSysThread::~CSysThread()
{
	if(m_ThdID == 0) return;
	pthread_exit(0);
	m_nState = EXITED;
}

//_____________________________________________________________________________________________
//_____________________________________________________________________________________________
//_____________________________________SUSPEND_________________________________________________
//_____________________________________________________________________________________________
int CSysThread::Suspend()
{
	cout<<"Suspending........\n";

	if(IsThdSuspended()) return 0;	// Alredy Suspended

	m_nSuspend = 1;
	return 1;
}
//_____________________________________________________________________________________________
//_____________________________________________________________________________________________
//______________________________________RESUME_________________________________________________
//_____________________________________________________________________________________________
int CSysThread::Resume(char (*func)(void*), void *pObj)
{
int nCondRet;

	if(IsThdSuspended() == false) return -1;

	m_pObjThdFn = pObj;
	ThreadFunction = (char (*)(void*))func;
	nCondRet = pthread_cond_signal(&m_ResumeCond);
	if(nCondRet != 0) return -1;
	return 1;
}
//_____________________________________________________________________________________________
//_____________________________________________________________________________________________
//______________________________________IsSuspended____________________________________________
//_____________________________________________________________________________________________

int CSysThread::IsSuspended()
{
	return m_nState;
}

bool CSysThread::IsThdSuspended()
{
int nRet;

	nRet = pthread_mutex_trylock(&m_SuspendMutex);
	if(nRet == 0)	// Thread was suspended
	{
		nRet = pthread_mutex_unlock(&m_SuspendMutex);
		return true;
	}
	return false;
}
//_____________________________________________________________________________________________
//_____________________________________________________________________________________________
//_________________________________________GetErr______________________________________________
//_____________________________________________________________________________________________
int CSysThread::GetErr()
{
	return m_nErr;
}
//_____________________________________________________________________________________________
//_____________________________________________________________________________________________
//_________________________________________GetThreadID______________________________________________
//_____________________________________________________________________________________________
unsigned long CSysThread::GetThreadID()
{
	return m_ThdID;
}
//_____________________________________________________________________________________________
//_____________________________________________________________________________________________
//_________________________________________TerminateSysThread__________________________________
//_____________________________________________________________________________________________
int CSysThread::TerminateSysThread()
{
	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
	pthread_cancel(m_ThdID);
	m_nState = EXITED;
	return 1;
}
//_____________________________________________________________________________________________
//_____________________________________________________________________________________________
//_________________________________________Quit________________________________________________
//_____________________________________________________________________________________________
int CSysThread::Quit()
{
int nRet;

	if(m_ThdID == 0) return -1;
	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
	nRet = pthread_cancel(m_ThdID);
	m_nState = EXITED;
	return nRet;
}

//_____________________________________________________________________________________________
//_____________________________________________________________________________________________
//_____________________________________________ThdFn___________________________________________
//_____________________________________________________________________________________________

void* CSysThread::ThdFn(void *ptr)
{
char ch;
int nSuspRet;
CSysThread *pCthd = (CSysThread*)ptr;

	pthread_cleanup_push(&CallBack,pCthd);}

	while(1)
	{

		if(pCthd->m_nSuspend==1)
		{
			pCthd->m_nSuspend = 0;
			pCthd->m_nState = SUSPENDED;
			pthread_cond_wait(&pCthd->m_ResumeCond,&pCthd->m_SuspendMutex);
		}
		pCthd->m_nState = RESUMED;
		usleep(2000);
		// check if the funtion is exited, Call the thread cancelation
		ch = (*(pCthd->ThreadFunction))(pCthd->m_pObjThdFn);
		if(ch < 0)
		{
			nSuspRet = pCthd->Suspend();
			if(nSuspRet < 0)
			{
				pthread_exit(0);
				pCthd->m_nState = EXITED;
			}
		}
	}
}

//_____________________________________________________________________________________________
//_____________________________________________________________________________________________
//_____________________________________________CallBack___________________________________________
//_____________________________________________________________________________________________

void CSysThread::CallBack(void *ptr)
{
CSysThread *pCthd = (CSysThread*)ptr;

	pCthd->m_nState = QUITTED;
	pCthd->m_ThdID = 0;
}
//_____________________________________________________________________________________________
//_____________________________________________________________________________________________
//_____________________________________________CallBack___________________________________________
//_____________________________________________________________________________________________
char CSysThread::Dummy(void *arg)
{
	sleep(1);
	return 1;
}
 
Old 12-27-2004, 12:13 AM   #3
cppkid
Member
 
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185

Rep: Reputation: 30
Sorry i forgot to send the header file for SysThread Lib so here it is,

Code:
#include<pthread.h>

#define SUSPENDED	1
#define EXITED		2
#define RESUMED		3
#define QUITTED		4

class CSysThread
{
private:
	pthread_mutex_t	m_SuspendMutex;
	pthread_cond_t	m_ResumeCond; 
	int m_nSuspend;
	int m_nState;
	pthread_t m_ThdID;
	void *m_pObjThdFn;
	char (*ThreadFunction)(void *);
	static char Dummy(void *arg);
	static void CallBack(void *arg);
	bool IsThdSuspended();
	
protected:
	int m_nErr;

	public:
	static void *ThdFn(void *pObj);
	int Suspend(void);
	int Resume(char (*)(void*), void *);
	int IsSuspended();
	int GetErr();
	unsigned long GetThreadID();
	int Quit();
	int TerminateSysThread();
	
	// Constructer / Destructer
	CSysThread();
	virtual ~CSysThread();
};
 
Old 12-27-2004, 02:22 AM   #4
Bostonian
LQ Newbie
 
Registered: Dec 2004
Distribution: SuSE 9.0
Posts: 4

Original Poster
Rep: Reputation: 0
That's exactly what I wanted.

Thanks a lot.

Last edited by Bostonian; 12-27-2004 at 02:27 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Linux for Graphic Design, web design, and publishing maelstrom209 Linux - Software 8 07-17-2011 11:35 AM
Design question blmack44 Linux - Hardware 1 08-22-2004 10:05 AM
FTP Server: Design Concept? Crashed_Again Linux - General 2 02-12-2003 06:02 AM
WebSite Design Advice To All + Question petercool General 1 07-03-2002 10:27 AM
Database Design Question oulevon Programming 4 09-12-2001 04:38 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration