'Operations Dev'에 해당되는 글 19건

  1. 2010.12.10 SpinLock
  2. 2010.12.10 SpinLock2
  3. 2010.12.10 SimpleConcurrency
  4. 2010.12.10 HandleBased Operation System
  5. 2010.12.10 map과 make_pair
  6. 2010.12.10 Timer 파일들
  7. 2010.12.10 TestingSTL
  8. 2010.12.10 TestProcessStateModel
  9. 2010.12.10 TwoStatemodel
posted by Kyleslab 2010. 12. 10. 18:22


// Example05_SpinLock.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다.
// 커널모르게 돌리면 유저모드

#include "stdafx.h"

#include<windows.h>
#include<process.h>

#include<string>
#include<iostream>
using namespace std;

unsigned int WINAPI writeCharacter(LPVOID param);

string g_string;
int const MAX_THREAD = 5;
bool g_isLocked = false;

int _tmain(int argc, _TCHAR* argv[])
{
 DWORD threadIDs[MAX_THREAD];
 HANDLE threadHandles[MAX_THREAD];
 char alphas[MAX_THREAD];

 for(int i =0; i<MAX_THREAD; ++i)
 {
  alphas[i] = 'a' + i;

  threadHandles[i] = (HANDLE) ::_beginthreadex( NULL, 0 , writeCharacter,(LPVOID*)(&alphas[i]),0,(unsigned int *)&threadIDs[i]);
 }
 
 DWORD result = ::WaitForMultipleObjects(MAX_THREAD, &threadHandles[0],TRUE,INFINITE);
 if(result == WAIT_OBJECT_0)
 {
  
  cout<<g_string;
  
 }
 for(int i = 0; i< MAX_THREAD; ++i)
 {
  CloseHandle(threadHandles[i]);
 }

 return 0;
}

unsigned int WINAPI writeCharacter(LPVOID param)
{
 char ch = *((char *)param);
 
 while(g_isLocked); 
 g_isLocked = true;   //이곳에서 ContextSwitching이 일어나면 위의 while문에서 뻑날수있다. 원자적연산이 아니기때문에.
 /***************임계영역 *************************/
 for(int i = 0; i< 1000; ++i)
 {
  g_string += ch;
 }
 /**************************************************/
 g_isLocked = false;
 
 
 return 0;
}

'Operations Dev > writech' 카테고리의 다른 글

Mutex  (0) 2010.12.10
Semaphore  (0) 2010.12.10
CriticalSection  (0) 2010.12.10
SpinLock2  (0) 2010.12.10
SimpleConcurrency  (0) 2010.12.10
posted by Kyleslab 2010. 12. 10. 18:10

// Example06_SpinLock2.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다.
//
#include "stdafx.h"

#include<windows.h>
#include<process.h>

#include<string>
#include<iostream>
using namespace std;

unsigned int WINAPI writeCharacter(LPVOID param);

string g_string;
int const MAX_THREAD = 5;
long g_key =1;

int _tmain(int argc, _TCHAR* argv[])
{
 

 DWORD threadIDs[MAX_THREAD];
 HANDLE threadHandles[MAX_THREAD];
 char alphas[MAX_THREAD];

 for(int i =0; i<MAX_THREAD; ++i)
 {
  alphas[i] = 'a' + i;

  threadHandles[i] = (HANDLE) ::_beginthreadex( NULL, 0 , writeCharacter,(LPVOID*)(&alphas[i]),0,(unsigned int *)&threadIDs[i]);
 }
 
 DWORD result = ::WaitForMultipleObjects(MAX_THREAD, &threadHandles[0],TRUE,INFINITE);
 if(result == WAIT_OBJECT_0)
 {
  
  cout<<g_string;
  
 }
 for(int i = 0; i< MAX_THREAD; ++i)
 {
  CloseHandle(threadHandles[i]);
 }

 return 0;
}

unsigned int WINAPI writeCharacter(LPVOID param)
{
 char ch = *((char *)param);
 
 LONG key = g_key;
 while(1 != ::InterlockedCompareExchange(&g_key, 0 ,1));  //( 조건 ? 참 : 거짓)과 같다
 /***************임계영역 *************************/
 for(int i = 0; i< 1000; ++i)
 {
  g_string += ch;
 }
 /**************************************************/
 g_key = 1; 
 
 return 0;
}

 

'Operations Dev > writech' 카테고리의 다른 글

Mutex  (0) 2010.12.10
Semaphore  (0) 2010.12.10
CriticalSection  (0) 2010.12.10
SpinLock  (0) 2010.12.10
SimpleConcurrency  (0) 2010.12.10
posted by Kyleslab 2010. 12. 10. 17:53

// Example01_Simple.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다.
//

#include "stdafx.h"

#include<windows.h>
#include<process.h>

#include<string>
#include<iostream>
using namespace std;

unsigned int WINAPI writeCharacter(LPVOID param);

string g_string;
int const MAX_THREAD = 5;

int _tmain(int argc, _TCHAR* argv[])
{
 g_string.reserve(MAX_THREAD * 1000 +1);  //가용공간 예약부분

 //char ch = 'a' ;
 DWORD threadIDs[MAX_THREAD];
 HANDLE threadHandles[MAX_THREAD];
 char alphas[MAX_THREAD];

 for(int i =0; i<MAX_THREAD; ++i)
 {
  alphas[i] = 'a' + i;

  threadHandles[i] = (HANDLE) ::_beginthreadex( NULL, 0 , writeCharacter,(LPVOID*)(&alphas[i]),0,(unsigned int *)&threadIDs[i]);
 }
 
 DWORD result =
  ::WaitForMultipleObjects(MAX_THREAD, &threadHandles[0],TRUE,INFINITE); //블록상태로 전환시킨다. 이 커널Object가 사용가능할 때까지
                 //대기시간 무한
 if(result == WAIT_OBJECT_0)  //대기가 끝난후 결과가 나오면 출력
 {
  cout<<g_string;
 }
 for(int i = 0; i< MAX_THREAD; ++i)
 {
  CloseHandle(threadHandles[i]);
 }

 return 0;
}

unsigned int WINAPI writeCharacter(LPVOID param)
{
 char ch = *((char *)param);
 for(int i = 0; i< 1000; ++i)
 {
  g_string += ch;
 }
 
 return 0;
}

'Operations Dev > writech' 카테고리의 다른 글

Mutex  (0) 2010.12.10
Semaphore  (0) 2010.12.10
CriticalSection  (0) 2010.12.10
SpinLock  (0) 2010.12.10
SpinLock2  (0) 2010.12.10
posted by Kyleslab 2010. 12. 10. 17:43

-- Process.h
#pragma once

#include <string>

class Process
{
public:
 Process(std::string const & name);

public:
 void excute(std::string const & message);//const 변경하지말라! &빠르게 넘기겠다!

private:
 std::string _name;
};

-- Process.cpp
#include "stdafx.h"

#include <iostream>
using namespace std;

#include "Process.h"
Process::Process( std::string const & name)
 : _name( name )
{
}

void Process::excute(const std::string & message)//const 변경하지말라! &빠르게 넘기겠다!
{
 cout<<_name<<"executed"<<message<<endl;
}

--Operation System.h
#pragma once

#include <map>
#include <string>

class Process;

class OperatingSystem
{

public:
 OperatingSystem();
 ~OperatingSystem();
public:
 int createProcess(std::string const & processName);
 void sendMessage(int processHandle, std::string const & message);
public:
 void getHandle (std::string const & processName);
private:
 std::map<int, Process *> _table;
 std::map<std::string, int> _handeltable;

private:
 static int _HandleCouter;// 클래스안에서 하나만 생성된다 인스턴스를 아무리 많이 만들어도
};

--Operation.cpp
#include "stdafx.h"
#include "Process.h"
#include "OperatingSystem.h"

#include <iostream>
using namespace std;

int OperatingSystem::_HandleCouter =0;

OperatingSystem::OperatingSystem()
{

}
OperatingSystem::~OperatingSystem()
{
 auto iter = _table.begin();
 for(;iter != _table.end(); ++iter)
 {
  delete (*iter).second;
 }
 _table.clear();

 _handeltable.clear();
}
int OperatingSystem::createProcess(std::string const & processName)
{
 _table.insert( make_pair( OperatingSystem::_HandleCouter,new Process( processName)));

 _handeltable.insert( make_pair( processName, OperatingSystem::_HandleCouter));

 return OperatingSystem::_HandleCouter++;

}
void OperatingSystem::sendMessage(int processHandle, std::string const & message)
{
 auto iter = _table.find(processHandle);
 (*iter).second->excute( message);
}

void OperatingSystem::getHandle(std::string const & processName)
{

 auto iter = _handeltable.find(processName);
 cout<<processName<<"의 HandleCounter는 "<<(*iter).second<<"입니다."<<endl;

}

--HandleBased System.cpp
// HandleBasedSystem.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다.
//

#include "stdafx.h"

#include "OperatingSystem.h"

int _tmain(int argc, _TCHAR* argv[])
{
 OperatingSystem os;

 int p1Han1d = os.createProcess("Process1");
 int p2Han1d = os.createProcess("Process2");
 int p3Han1d = os.createProcess("Process3");
 
 os.sendMessage(p2Han1d, "running");
 os.sendMessage(p1Han1d, "eating");
 os.sendMessage(p2Han1d, "sleeping");

 os.getHandle("Process1");
 os.getHandle("Process2");
 os.getHandle("Process3");

 return 0;
}

 

'Operations Dev > Handle' 카테고리의 다른 글

map과 make_pair  (0) 2010.12.10
posted by Kyleslab 2010. 12. 10. 17:35


// Table.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다.
//

#include "stdafx.h"

#include <map>
#include <string>
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
 map<string, int> table;

 table["abc"] = 10;
 table.insert( make_pair("due",20));// pair 두개의 자료를 하나의 자료로 묶어준다.
 /*pair<int, int> a;
 a.first;
 a.second;
 */
 //map< string, int >::iterator iter;
 auto iter =table.begin();
 for(;
  iter !=table.end();
  ++iter )
 {
  cout<<(*iter).first<<(*iter).second<<endl;
 }

 cout<<table["abc"]<<endl;// 이방식은 거의 사용안함 안의 인덱스가 없는것들도 접근이 가능하므로 위험하다.

 iter = table.find("abc");
 cout<<(*iter).second<<endl;

 cout<<table["asdfasdf"]<<endl;

 return 0;
}

 

'Operations Dev > Handle' 카테고리의 다른 글

HandleBased Operation System  (0) 2010.12.10
posted by Kyleslab 2010. 12. 10. 17:31

다운받기!

'Operations Dev > Testing Timer' 카테고리의 다른 글

TestingSTL  (0) 2010.12.10
TestProcessStateModel  (0) 2010.12.10
TwoStatemodel  (0) 2010.12.10
posted by Kyleslab 2010. 12. 10. 17:27


// TestingSTL.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다.
// 구조체는 디폴트는 public 클래스는 프라이빗, 구조체는 자료형의 집합, 클래스는 자료형과 그것을 사용하는 멤버함수의 집합

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <list>
#include <map>
#include <algorithm>

using namespace std;

class TestClass
{
public:
 TestClass(int x)
  :_x( x )
 {
 }
 TestClass(TestClass const & rhs)//클래스의 인스턴스가 생성될때 호출된다.
  :_x(rhs._x)
 {
 }

 TestClass operator+(TestClass const & rhs) //TestClass를 리턴하는 이유는 z= x+y일때 =연산자도 오버로딩을 해야하기때문에
 {
  //return this->_x + rhs._x;
  return TestClass(_x + rhs._x);

 }

 TestClass & operator=(TestClass const & rhs) //TestClass를 리턴하는 이유는 z= x+y일때 =연산자도 오버로딩을 해야하기때문에
 {//primitive 타입을 제외하고는 사칙연산및 비교연산은 불가능함, 단 연산자 재정의 가능함
  this->_x = rhs._x;
  return *this;

 }

 template<typename E, typename U>
 friend std::basic_ostream<E, U>& operator<<(std::basic_ostream<E, U>& os, TestClass const & rhs)
 {
  os << L"(" << rhs._x<< L")";
  return os;
 }

 bool operator==( TestClass const & rhs) const
 {
  return _x == rhs._x;
 }

 bool operator!=( TestClass const & rhs) const
 {
  return _x != rhs._x;
 }

private:
 int _x;
};

 

template <typename Type>
Type sum( Type t1, Type t2)
{
 return t1+t2;
}

 

 

int _tmain(int argc, _TCHAR* argv[])
{
 vector< int > intVector;
 
 for( int i =0; i < 100; ++i)
 {
  intVector.push_back( i );
 }
 
 
 //wcout<<intVector.size()<<endl; //벡터에서 차례대로 하나씩 꺼내서 출력!
 //wcout<<intVector[50]<<endl;
 //intVector[50]=99999;
 //wcout<<intVector[50];

 //for ( int i = 0; i < intVector.size(); ++i )
 //{
 // wcout<<intVector[i]<<endl;
 //}

 //vector< int >::iterator iter;

 //for( iter = intVector.begin();
 // iter != intVector.end();
 // ++iter)
 //{
 // cout<<(*iter)<<endl;
 //};
 //

 

 vector< int >::iterator iter;

 //iter = find( intVector.begin(), // 찾아서 포인터로 전달한다.
 // intVector.end(),
 // 10);

 //intVector.erase(iter); // 포인터로 아예 삭제

 //cout<<endl<<intVector.size()<<endl;

 //for(iter = intVector.begin(); iter != intVector.end(); iter++) // 출력해보면 오류없이 중간만 빠져서 출력된다.
 // cout<< (*iter) <<endl;


 //cout<< intVector.size() <<endl;

 // iter = find( intVector.begin(),
 // intVector.end(),
 // 70);
 //intVector.insert(iter, 9999); // 찾은 위치에 새로운값넣기

 //cout<<intVector[70]<<endl;

  //iter = find( intVector.begin(),
  //intVector.end(),
  //70);

  //if(iter == intVector.end()) // 포인터가 벡터의 끝이면 출력
  //{
  // cout<< "END" <<endl;
  //}

 


 
 //for_each( intVector.begin(), // foreach사용하여 출력
 // intVector.end(),
 // [](int element)
 //{
 // wcout<<element<<endl;
 //});
 //
 
 
 TestClass t1(5), t2(2);
 TestClass t3(3);
 //TestClass t3= t1 + t2;
 cout<<sum<TestClass>(t1, t2)<<endl; 
 cout<<sum< int >(5, 10)<<endl;
 cout<<sum<double>(1.0, 2.5)<<endl;
 
 return 0;
}

 

'Operations Dev > Testing Timer' 카테고리의 다른 글

Timer 파일들  (0) 2010.12.10
TestProcessStateModel  (0) 2010.12.10
TwoStatemodel  (0) 2010.12.10
posted by Kyleslab 2010. 12. 10. 16:49


// No3_TestTwoStateModel.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다.
//


#include "stdafx.h"


#include <queue>
#include <string>
#include <iostream>
using namespace std;
#include "Timer\Timer.h"
#include "Process.h"

int const INTERLEAVING_TIME = 2000;


int _tmain(int argc, _TCHAR* argv[])
{
 queue< Process > shortermQueue;
 shortermQueue.push(Process( L"AAAAAAAA", 10));
 shortermQueue.push(Process( L"BBBBBBBB", 5));
 shortermQueue.push(Process( L"CCCCCCCC", 9));

 Timer interleavingTimer;

 while( !shortermQueue.empty())
 {
  wcout << L"**********************************interleaving******************************"<<endl;
  Process currentProcess = shortermQueue.front();
  shortermQueue.pop();
  bool isFinished = false;

  interleavingTimer.start();
  while( interleavingTimer.elapsedMilliseconds() < INTERLEAVING_TIME)
  {
   wcout << L"Execute" << currentProcess.getName()
    <<endl;
   wcout << L"Result" << currentProcess.getCount()
    <<endl;
   if(!currentProcess.excute())
   {
    wcout<<L"Discard"<< currentProcess.getName()
     <<endl;
    isFinished = true;
    break;
   }
  }
  interleavingTimer.stop();

  if(!isFinished)
  {
   wcout<<L"Push "<<currentProcess.getName()
    <<L" to a queue"
    <<endl;
   shortermQueue.push(currentProcess);

  }
  wcout << L"********************************************************************"<<endl<<endl;

 }

 return 0;
}

--- Process.cpp
#include "stdafx.h"

#include "Process.h"

Process::Process(std::wstring const & processName,
 int startingCount)
 :name(processName),
 count(startingCount)
{

}

Process::Process(Process const & rhs):name(rhs.name), count(rhs.count)
{
}

bool Process::excute()
{
 bool result = false;
 --count;

 if( 0 < count)
 {
  result = true;
 }

 timer.start();
 while(timer.elapsedMilliseconds() < 2000);
 timer.stop();

 return result;

}

---Process.h
#pragma once

#include <string>

#include "Timer\Timer.h"

class Process
{
public:
 Process(std::wstring const & processName,
  int startingCount );
 Process( Process const & rhs);

public:
 bool excute();
public:
 Process &
  operator=( Process const & rhs);

public :
 std::wstring const &
  getName();
 int
  getCount();

private:
 std::wstring name;
  int count;
  Timer timer;
};

inline  Process &
  Process::operator=( Process const & rhs)
{
 name = rhs.name;
 count = rhs.count;
}

inline  std::wstring const &
  Process::getName()
{
 return name;
}

inline int Process::getCount()
{
 return count;
}

'Operations Dev > Testing Timer' 카테고리의 다른 글

Timer 파일들  (0) 2010.12.10
TestingSTL  (0) 2010.12.10
TwoStatemodel  (0) 2010.12.10
posted by Kyleslab 2010. 12. 10. 16:47


// No2_TestingFiveStateModel.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다.
//

#include "stdafx.h"
#include <Windows.h>
#include <iostream>
using namespace std;
#include "Timer\Timer.h"

 

int _tmain(int argc, _TCHAR* argv[])
{
 Timer timer;

  
 //timer.start(); // 슬립으로 준비와 수행 왔다갔다하기

 //while(10>timer.elapsedSeconds());
 //cout<<"Time Over"<<endl;
 //timer.stop();

 //::Sleep( 10000 );

 //cout<<"Timer Over"<<endl;


 
 //while(true) // 수행! 점유하여 키입력을 수행
 //{
 // if(::GetAsyncKeyState(VkKeyScan('x')))
 // {
 //  break;
 // }
 //}
 //

 while(true) // 블록상태 보기
 {
  char key;
  cin>>key;
  if(key == 'x')
  {
   break;
  }
 }

 return 0;
}

 

'Operations Dev > Testing Timer' 카테고리의 다른 글

Timer 파일들  (0) 2010.12.10
TestingSTL  (0) 2010.12.10
TestProcessStateModel  (0) 2010.12.10