⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 060341223dlg.cpp

📁 操作系统课程设计:死锁问题。 哲学家进餐问题的解决!!!很好的操作系统大作业!
💻 CPP
字号:
// 060341223Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "060341223.h"
#include "060341223Dlg.h"
#include "MyStatic.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
int chopstick[5]={1,1,1,1,1};
int rand0,rand_eat,rand_think,rand_wait;
int GetRandTime(void)
{
	srand((unsigned)time(NULL));
	return rand()%2000;	
}
class CAboutDlg : public CDialog
{
public:
	CAboutDlg();
	
	// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA
	
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL
	
	// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// C060341223Dlg dialog

C060341223Dlg::C060341223Dlg(CWnd* pParent /*=NULL*/)
: CDialog(C060341223Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(C060341223Dlg)
	m_Edit1 = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void C060341223Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(C060341223Dlg)
	DDX_Control(pDX, IDC_EDIT1, m_wenben);
	DDX_Control(pDX, IDC_STATIC9, m_Static9);
	DDX_Control(pDX, IDC_STATIC8, m_Static8);
	DDX_Control(pDX, IDC_STATIC7, m_Static7);
	DDX_Control(pDX, IDC_STATIC6, m_Static6);
	DDX_Control(pDX, IDC_STATIC10, m_Static10);
	DDX_Control(pDX, IDC_STATIC2, m_Static2);
	DDX_Control(pDX, IDC_STATIC5, m_Static5);
	DDX_Control(pDX, IDC_STATIC4, m_Static4);
	DDX_Control(pDX, IDC_STATIC3, m_Static3);
	DDX_Control(pDX, IDC_STATIC1, m_Static1);
	DDX_Text(pDX, IDC_EDIT1, m_Edit1);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(C060341223Dlg, CDialog)
//{{AFX_MSG_MAP(C060341223Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// C060341223Dlg message handlers

BOOL C060341223Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	
	// Add "About..." menu item to system menu.
	
	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);
	
	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}
	
	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	UpdateData(TRUE);
	m_wenben.set_wenben();
	m_Edit1="   哲学家进餐问题(The Dinning Philosophers Problem):\r\n\r\n是由Dijkstra提出并解决的哲学家进餐问题是典型的同步问题。\r\n该问题是描述有五个哲学家共用一张圆桌,分别坐在周围的五\r\n张椅子上,在圆桌上有五个碗和五只筷子,他们的生活方式是\r\n交替地进行思考和进餐。平时,一个哲学家进行思考,饥饿时\r\n便试图取用其左右最靠近他的筷子,只有在他拿到两只筷子时\r\n才能进餐。进餐毕,放下筷子继续思考。";
	SetDlgItemText(IDC_STATIC1, "思考");
	m_Static1.set_thinking();
	SetDlgItemText(IDC_STATIC2, "思考");
	m_Static2.set_thinking();
	SetDlgItemText(IDC_STATIC3, "思考");
	m_Static3.set_thinking();
	SetDlgItemText(IDC_STATIC4, "思考");
	m_Static4.set_thinking();
	SetDlgItemText(IDC_STATIC5, "思考");
	m_Static5.set_thinking();
	m_Static6.SetBkColor(RGB(253,205,249));
	m_Static7.SetBkColor(RGB(253,205,249));
	m_Static8.SetBkColor(RGB(253,205,249));
	m_Static9.SetBkColor(RGB(253,205,249));
	m_Static10.SetBkColor(RGB(253,205,249));
	UpdateData(FALSE);
	SetTimer(1,50,NULL);//for Philosophers1
	SetTimer(2,50,NULL);//for Philosophers2
	SetTimer(3,50,NULL);//for Philosophers3
	SetTimer(4,50,NULL);//for Philosophers4
	SetTimer(5,50,NULL);//for Philosophers5
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void C060341223Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void C060341223Dlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting
		
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
		
		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;
		
		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR C060341223Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void C060341223Dlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	OnInitDialog();
	UpdateData(false);
	
	
}

void C060341223Dlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	rand_eat=GetRandTime()+800;//进餐时间至少800
	rand_think=GetRandTime()+1000;//思考时间
	rand_wait=GetRandTime()%1000+300;//等待时间
	CString cs; 
	switch(nIDEvent)//利用定时器编号进入五位哲学家各自的进程
	{
	
	case 1://执行哲学家1的进程
		{
			
			
			if(GetDlgItem(IDC_STATIC1)->GetWindowText(cs),cs=="等待")//如果哲学家1当前状态是等待,即可转入进餐状态状态时,就绪!
			{
				if(chopstick[0]==1 && chopstick[1]==1)//申请时,有足够资源分配
				{
					chopstick[0]=0;
					chopstick[1]=0;//改变互斥访问资源的状态
					m_Static1.set_eating();
					SetDlgItemText(IDC_STATIC1, "用餐");//将哲学家状态改为进餐
					
				}											
			}
			else if(GetDlgItem(IDC_STATIC1)->GetWindowText(cs),cs=="思考")
			{
				m_Static1.set_waiting();
				SetDlgItemText(IDC_STATIC1, "等待");//由思考状态转入等待状态,准备发出申请
				
				UpdateData(FALSE);
				Sleep(rand_wait);//等待一个小的时间片再提出申请
			} 
		}
	case 2://进入哲学家2进程的入口
		{
		
			if(GetDlgItem(IDC_STATIC2)->GetWindowText(cs),cs=="等待")//等待就绪
			{
				if(chopstick[1]==1 && chopstick[2]==1)//有资源可供使用
				{
					chopstick[1]=0;
					chopstick[2]=0;//改变互斥变量状态
					m_Static2.set_eating();
					SetDlgItemText(IDC_STATIC2, "用餐");//进入进餐状态
					
				}
			}
			else if(GetDlgItem(IDC_STATIC2)->GetWindowText(cs),cs=="思考")
			{
				m_Static2.set_waiting();
				SetDlgItemText(IDC_STATIC2, "等待");	//由思考状态转入就绪状态
				
				UpdateData(FALSE);
				Sleep(rand_wait);
			}
		}
	case 3:
		{
			
			if(GetDlgItem(IDC_STATIC3)->GetWindowText(cs),cs=="等待")//等待就绪
			{
				if(chopstick[2]==1 && chopstick[3]==1)//有资源可供使用
				{
					chopstick[2]=0;
					chopstick[3]=0;//改变互斥变量状态
					m_Static3.set_eating();
					SetDlgItemText(IDC_STATIC3, "用餐");//进入进餐状态
					
				}
			}
			else if(GetDlgItem(IDC_STATIC3)->GetWindowText(cs),cs=="思考")
			{
				m_Static3.set_waiting();
				SetDlgItemText(IDC_STATIC3, "等待");	//由思考状态转入就绪状态
				
				UpdateData(FALSE);
				Sleep(rand_wait);
			}
		}
	case 4:
		{
	
			if(GetDlgItem(IDC_STATIC4)->GetWindowText(cs),cs=="等待")//等待就绪
			{
				if(chopstick[3]==1 && chopstick[4]==1)//有资源可供使用
				{
					chopstick[3]=0;
					chopstick[4]=0;//改变互斥变量状态
					m_Static4.set_eating();
					SetDlgItemText(IDC_STATIC4, "用餐");//进入进餐状态	
					
				}
			}
			else if(GetDlgItem(IDC_STATIC4)->GetWindowText(cs),cs=="思考")
			{
				m_Static4.set_waiting();
				SetDlgItemText(IDC_STATIC4, "等待");	//由思考状态转入就绪状态	
				
				UpdateData(FALSE);
				Sleep(rand_wait);
			}	
		}
	case 5:
		{
			if(GetDlgItem(IDC_STATIC5)->GetWindowText(cs),cs=="等待")//等待就绪
			{
				if(chopstick[4]==1 && chopstick[0]==1)//有资源可供使用
				{
					chopstick[4]=0;
					chopstick[0]=0;//改变互斥变量状态
					m_Static5.set_eating();
					SetDlgItemText(IDC_STATIC5, "用餐");//进入进餐状态
					
				}
			}
			else if(GetDlgItem(IDC_STATIC5)->GetWindowText(cs),cs=="思考")
			{
				m_Static5.set_waiting();
				SetDlgItemText(IDC_STATIC5, "等待");	//由思考状态转入就绪状态
				
				UpdateData(FALSE);
				Sleep(rand_wait);
			}
			UpdateData(FALSE);//刷新界面
			Sleep(rand_eat);//进餐过程
	
			if(GetDlgItem(IDC_STATIC1)->GetWindowText(cs),cs=="用餐")//判断以上过程中那些哲学家使用了资源,进餐结束,资源释放
			{
			m_Static1.set_thinking();
				SetDlgItemText(IDC_STATIC1, "思考");
				
				chopstick[0]=1;
				chopstick[1]=1;
			}

			if(GetDlgItem(IDC_STATIC2)->GetWindowText(cs),cs=="用餐")
			{
				m_Static2.set_thinking();
				SetDlgItemText(IDC_STATIC2, "思考");
				
				chopstick[1]=1;
				chopstick[2]=1;
			}
		
			if(GetDlgItem(IDC_STATIC3)->GetWindowText(cs),cs=="用餐") 
			{
			m_Static3.set_thinking();
				SetDlgItemText(IDC_STATIC3, "思考");
				
				chopstick[2]=1;
				chopstick[3]=1;
			}
	
			if(GetDlgItem(IDC_STATIC4)->GetWindowText(cs),cs=="用餐") 
			{
				m_Static4.set_thinking();
				SetDlgItemText(IDC_STATIC4, "思考");
				
				chopstick[3]=1;	
				chopstick[4]=1;
			}
			//GetDlgItem(IDC_STATIC1)->GetWindowText( cs[4]);
			if(GetDlgItem(IDC_STATIC5)->GetWindowText(cs),cs=="用餐") 
			{
				m_Static5.set_thinking();
				SetDlgItemText(IDC_STATIC5, "思考");
				
				chopstick[4]=1;
				chopstick[0]=1;
			}
			UpdateData(FALSE);
			Sleep(rand_think);
		}
}

CDialog::OnTimer(nIDEvent);
}

void C060341223Dlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	KillTimer(1);
	KillTimer(2);
	KillTimer(3);
	KillTimer(4);
	KillTimer(5);
	
}

void C060341223Dlg::OnButton3() 
{
	// TODO: Add your control notification handler code here
	CDialog::OnCancel(); 


	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -