这里用线程来模拟 硬件方法同步机制的Swap方法指令 利用下面的代码 能有效的实现进程互斥 但当临界资源忙碌时其他访问进程 必须不断测试 处于一种忙等状态 不符合让权等待 造成处理机时间的浪费 同时很难用于解决复杂的进程问题
#include "iostream.h"
#include "windows.h"
const int N=5,M=3;
//int Max[N][M]={0};//各进程所需各类资源的最大需求
//int Available[M]={0};//系统可用资源
//int Allocation[N][M]={0};//系统已分配资源
int Max[N][M]={{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}};
int Available[M]={10,5,7};
int Allocation[N][M]={{0,1,0},{2,0,0},{3,0,2},{2,1,1},{0,0,2}};
int Need[N][M]={0};//还需要资源
int Request[M]={0};//请求资源向量
char *Name[]={"P0", "P1", "P2", "P3","P4"};
int Work[M]={0};//存放系统可提供资源
void ShowResourceTable( );
void Alloc(int i);
void RollBack(int i);
bool IsSafe();
int main()
{
int i,j;bool YN;
cout<<"\n\n=====================================";
cout<<"\n Banker\'s Algorithm in C++ ";
cout<<"\n=====================================\n\n";
/*cout<<"输入系统提供ABC三类资源的最大量"<
>Available[j];
cout<<"输入各进程对资源的最大需求量,建立Max矩阵"<
>Max[i][j];
cout<<"输入系统给各进程已分配资源,建立Alloacton矩阵"<
>Allocation[i][j];*/
for(i=0;i
>i;
if(!(i==0||i==1||i==2||i==3||i==4)) break;
cout<
<<"请输入进程 "<
<<" 申请的资源:"<
>Request[j];//输入需要申请的资源 YN=TRUE; for (j=0;j
Need[i][j])//判断申请是否大于需求,若大于则出错,是否合理? { cout<<"进程 "<
<<"申请的资源大于它需要的资源"; cout<<" 非法申请,不予分配!"<
Available[j])//判断申请是否大于当前资源,若大于则出错,是否合法? { cout<<"进程"<
<<"申请的资源大于系统现在可利用的资源"; cout<<" 分配出错,不予分配!"<
Work[j]) Flag=FALSE; if(Flag) { cout<
<<" "<
<<" "<
<<" "<
<<" "; for(j=0;j
void Swap(bool *a,bool *b){ bool temp ; temp = *a; *a = *b; *b = temp; } struct st{ bool lock; int Count; } resource = {false,100}; DWORD WINAPI fun(); DWORD WINAPI fun1(); int main(int argc, char* argv[]) { ::CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)&fun,NULL,NULL,NULL); ::CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)&fun1,NULL,NULL,NULL); while(resource.Count>0){ printf("main is runing\n"); } return 0; } DWORD WINAPI fun(){ do{ bool KEY = true; do{ Swap(&(resource.lock),&KEY); }while(KEY!=false); /*临界区*/ printf("fun:%d\n",resource.Count); resource.Count--; resource.lock = false; }while(true); return 0; } DWORD WINAPI fun1(){ do{ bool KEY = true; do{ Swap(&(resource.lock),&KEY); }while(KEY!=false); /*临界区*/ printf("fun1:%d\n",resource.Count); resource.Count--; resource.lock = false; }while(true); return 0; }empty#include "stdafx.h" #include
char Input[50]={0}; char Output[50]={0}; DWORD ReadByte,WriteByte; BOOL rc; int err; int _tmain(int argc, _TCHAR* argv[]) { HANDLE hPipe; hPipe = CreateNamedPipe(L"\\\\.\\pipe\\PipeServe",PIPE_ACCESS_DUPLEX|FILE_FLAG_OVERLAPPED|WRITE_DAC,PIPE_TYPE_MESSAGE|PIPE_READMODE_BYTE|PIPE_WAIT,1,20,30,NMPWAIT_USE_DEFAULT_WAIT,NULL); if(hPipe == INVALID_HANDLE_VALUE){ err = GetLastError(); printf("pipe create is fail! err = %d\n",err); exit(1); } else { printf("pipe create is sucessful\n"); } while (true) { rc = ConnectNamedPipe(hPipe,NULL); if (rc == 0) { printf("Server is connection fial\n "); exit(2); } else { printf("pipe connection is successful\n"); } strcpy_s(Input,""); strcpy_s(Output,""); rc = ReadFile(hPipe,Input,sizeof(Input),&ReadByte,NULL); if(rc == 0&&ReadByte == 0){ printf("serve is fail"); exit(3); } else { printf("read pipe is successful\n date from client is =%s\n",Input); } strcmp(Input,"end"); } system("pause"); printf("serve is colse"); CloseHandle(hPipe); return 0; }