3.odt

(12 KB) Pobierz

Marcin Płonka

Grupa l10

2 FD

 

 

 

 

 

 

Systemy operacyjne

 

 

 

 

Sprawozdanie z ćwiczenia nr 3

 

Temat: Synchronizacja wątków.

Wymiana informacji między wątkami.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

#include "stdafx.h"

#include "lab3.h"

#include "Okno.h"

 

#ifdef _DEBUG

#define new DEBUG_NEW

#endif

 

#define WM_WIADOMOSC (WM_USER+100)

 

// CAboutDlg dialog used for App About

int volatile LicznikWatkow;        // Deklaracja zmiennych globalnych

int volatile fPrzerwij;

int CzasyPracy[5];

char Wynik[255];

int fJestWynik;

CCriticalSection csWyklucz;

CEvent evWynikObsluzony;

 

class CAboutDlg : public CDialog

{

public:

              CAboutDlg();

 

// Dialog Data

              enum { IDD = IDD_ABOUTBOX };

 

              protected:

              virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

 

// Implementation

protected:

              DECLARE_MESSAGE_MAP()

};

 

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)

{

}

 

void CAboutDlg::DoDataExchange(CDataExchange* pDX)

{

              CDialog::DoDataExchange(pDX);

}

 

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)

END_MESSAGE_MAP()

 

 

// COkno dialog

 

COkno::COkno(CWnd* pParent /*=NULL*/)

              : CDialog(COkno::IDD, pParent)

{

              m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

}

 

void COkno::DoDataExchange(CDataExchange* pDX)

{

              CDialog::DoDataExchange(pDX);

              DDX_Control(pDX, IDC_EKRAN, m_ekran);

              DDX_Control(pDX, IDC_W1, m_w1);

              DDX_Control(pDX, IDC_W2, m_w2);

              DDX_Control(pDX, IDC_W3, m_w3);

              DDX_Control(pDX, IDC_W4, m_w4);

              DDX_Control(pDX, IDC_W5, m_w5);

              DDX_Control(pDX, IDC_URUCHOM, m_uruchom);

              DDX_Control(pDX, IDC_PRZERWIJ, m_przerwij);

              DDX_Control(pDX, IDC_KONIEC, m_koniec);

}

 

BEGIN_MESSAGE_MAP(COkno, CDialog)

              ON_WM_SYSCOMMAND()

              ON_WM_PAINT()

              ON_WM_QUERYDRAGICON()

              //}}AFX_MSG_MAP

              ON_MESSAGE(WM_WIADOMOSC, OnWiadomosc)

              ON_BN_CLICKED(IDC_URUCHOM, OnBnClickedUruchom)

              ON_WM_TIMER()

              ON_BN_CLICKED(IDC_PRZERWIJ, OnBnClickedPrzerwij)

              ON_BN_CLICKED(IDC_KONIEC, OnBnClickedKoniec)

              ON_WM_DESTROY()

END_MESSAGE_MAP()

 

 

// COkno message handlers

 

BOOL COkno::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

 

              // Add extra initialization here

              srand((unsigned )time(0));

              SetTimer(1,4000,0);

 

             

              return TRUE;  // return TRUE  unless you set the focus to a control

}

 

void COkno::OnSysCommand(UINT nID, LPARAM lParam)

{

              if ((nID & 0xFFF0) == IDM_ABOUTBOX)

              {

                            CAboutDlg dlgAbout;

                            dlgAbout.DoModal();

              }

              else

              {

                            CDialog::OnSysCommand(nID, lParam);

              }

}

 

 

void COkno::OnPaint()

{

              if (IsIconic())

              {

                            CPaintDC dc(this); // device context for painting

 

                            SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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 function to obtain the cursor to display while the user drags

//  the minimized window.

HCURSOR COkno::OnQueryDragIcon()

{

              return static_cast<HCURSOR>(m_hIcon);

}

 

UINT COkno::WatekRoboczy(void * pParametr)

{

int i=(int)pParametr;

 

do

{

if(fPrzerwij)

  {

  LicznikWatkow--;

  return 0;

  }

AfxGetMainWnd()->PostMessage(WM_WIADOMOSC,i,1);

Sleep(500);

if(CzasyPracy[i]<=0)

break;

else CzasyPracy[i]--;

}while(1);

 

if(fPrzerwij)

{

LicznikWatkow--;

return 0;

}

 

AfxGetMainWnd()->PostMessage(WM_WIADOMOSC,i,2);

csWyklucz.Lock();

sprintf(Wynik,"Wyniki watku %d",i);

fJestWynik=1;

AfxGetMainWnd()->PostMessage(WM_WIADOMOSC,i,3);

CSingleLock SL(&evWynikObsluzony);

SL.Lock();                                               // blokada

csWyklucz.Unlock();                              // zwolnienie blokady

if(fPrzerwij)

  {

  LicznikWatkow--;

  return 0;

  }

AfxGetMainWnd()->PostMessage(WM_WIADOMOSC,i,4);

LicznikWatkow--;

 

}

 

 

void Cokno::OnBnClickedUruchom() // Obsługa przycisku uruchom

{

              m_uruchom.EnableWindow(FALSE);

              m_przerwij.EnableWindow();

 

fPrzerwij=0;

for(int i=0;i<5;i++)

{

              CzasyPracy[i]=10+(30*rand())/RAND_MAX;

              AfxBeginThread(WatekRoboczy,(void *)i);

}

 

 

}

 

afx_msg LRESULT COkno::OnWiadomosc(WPARAM n, LPARAM m)

{

                            static char* w[4] =

              {"Oblicza...", "Czeka na ekran...", "Wyswietla wynik...", "Zakonczyl prace"};             

 

              CString tmp;             

              tmp = w[m-1];

 

//Dodatkowa obsluga wyswietlania pozostalego czasu obliczen

 

             

              switch (n) {

              case 0: m_w1.SetWindowText(tmp);

                            break;

              case 1: m_w2.SetWindowText(tmp);

                            break;

              case 2: m_w3.SetWindowText(tmp);

                            break;

...

Zgłoś jeśli naruszono regulamin