NhatQuang
Joined : 11 Dec 2007 Posts : 1
 | Subject: Giai lai bai thi Ca2 HDH !! Mong thay xem xet!! 11/12/2007, 19:15 | |
| //Pham Vu Nhat Quang ,muc quan 13, MSSV: 10660221 Cau 1:
#include <iostream.h> #include <stdio.h> #include <conio.h> #include <windows.h> #define BUFFER_SIZE 10 //Khoi tao mang buffer co kich thuoc la 10 int buffer[BUFFER_SIZE]; //Mang ki tu s de phan biet 2 nha san xuat char s[BUFFER_SIZE]; //2 bien con tro in,out int in=0,out=0; int nextProduced =1; //2 bien Handle chua muc quan cua 2 den hieu semEmpty va semFull HANDLE semEmpty,semFull; //Muc quan 2 luong san xuat HANDLE T,t;
CRITICAL_SECTION critSec; //giong den hieu mutex su dung cho vung tuong tranh //Ham san xuat void Producer(void *m) { while(1) { //Neu doi so truyen len la T thi doi den hieu T if(m==T) WaitForSingleObject(T,INFINITE); else if(m==t) WaitForSingleObject(t,INFINITE); //Xet xem trong mang buffer da co san pham hay chua WaitForSingleObject(semEmpty,INFINITE); //Vao vugn tuong tranh EnterCriticalSection(&critSec); buffer[in]=nextProduced++; if(m==T) s[in]='T'; else if(m==t) s[in]='t'; in=(in+1)%BUFFER_SIZE; //Giai phong den semFull ReleaseSemaphore(semFull,1,NULL); //Roi khoi vung tuong tranh LeaveCriticalSection(&critSec); } }
void Consumer() { int nextConsumed; while(1) { WaitForSingleObject(semFull,INFINITE); EnterCriticalSection(&critSec); nextConsumed=buffer[out]; out=(out+1)%BUFFER_SIZE; LeaveCriticalSection(&critSec); ReleaseSemaphore(semEmpty,1,NULL); } } //in mang buffer ra man hinh void ShowBuffer(){ const char * LeftMargin="\n\t"; int i; printf(LeftMargin); for(i=0;i<(in*5);i++) putchar(' '); printf("!in"); printf(LeftMargin); for (i=0;i<BUFFER_SIZE-1;i++) printf("%c%2d, ",s[i],buffer[i]); printf("%c%2d",s[i],buffer[BUFFER_SIZE-1]); printf(LeftMargin); for(i=0; i<(out*5);i++) putchar(' '); printf("^out"); printf("\n"); } //Mau nen void color_background() { HANDLE hConsole; DWORD dwConSize; DWORD cCharsWritten; COORD coordScreen = { 0,0 }; hConsole = GetStdHandle(STD_OUTPUT_HANDLE); FillConsoleOutputAttribute( hConsole, BACKGROUND_GREEN,dwConSize, coordScreen, &cCharsWritten ); } //mau chu void color_text_trang() { HANDLE hConsole; hConsole = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(hConsole,FOREGROUND_BLUE |FOREGROUND_RED|FOREGROUND_GREEN| BACKGROUND_GREEN); } void main() { color_background(); color_text_trang(); //khoi tao 2 den hieu T,t kieu Semaphore voi gia tri ban dau la 3 de san xuat 3 san pham T=CreateSemaphore(0,3,1000,"T"); t=CreateSemaphore(0,3,1000,"t"); //Khoi tao den semEmpty(co kich thuoc bang BUFFER_SIZE),semFull(so san pham co trong BUFFER_SIZE ban dau = 0) semEmpty=CreateSemaphore(0,BUFFER_SIZE,BUFFER_SIZE,0); semFull=CreateSemaphore(0,0,BUFFER_SIZE,0); HANDLE ProducerHandle1,ProducerHandle2; HANDLE ConsumerHandle1,ConsumerHandle2,ConsumerHandle3; //Khai bao cac bien voi kieu DWORD 4 btye de chua ID DWORD ProducerID1,ProducerID2; DWORD ConsumerID1,ConsumerID2,ConsumerID3; //Khoi tao vung tuong tranh InitializeCriticalSection(&critSec); //Khoi tao 2 luong San xuat ProducerHandle1=CreateThread(0,0,(LPTHREAD_START_ROUTINE)Producer,(void *)T,0,&ProducerID1); ProducerHandle2=CreateThread(0,0,(LPTHREAD_START_ROUTINE)Producer,(void *)t,0,&ProducerID2); //Khoi tao 3 luong tieu tu tu dong ConsumerHandle1=CreateThread(0,0,(LPTHREAD_START_ROUTINE)Consumer,0,0,&ConsumerID1); ConsumerHandle2=CreateThread(0,0,(LPTHREAD_START_ROUTINE)Consumer,0,0,&ConsumerID2); ConsumerHandle3=CreateThread(0,0,(LPTHREAD_START_ROUTINE)Consumer,0,0,&ConsumerID3); while(1) { //Ngu 3 giay Sleep(3000); EnterCriticalSection(&critSec); ShowBuffer(); LeaveCriticalSection(&critSec); } }
Cau2:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Net; using System.Net.Sockets; using System.Threading;
namespace Cau2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //khai bao 2 den T,t giong ben cau 1 Semaphore T = new Semaphore(3, 1000, "T"); Semaphore t = new Semaphore(3, 1000, "t"); private void button1_Click(object sender, EventArgs e) { try { //den hieu T giai phong giai tri nhap tu textbox1 T.Release(Convert.ToInt16(textBox1.Text)); } catch (Exception err) { } } private void button2_Click(object sender, EventArgs e) { try { //den hieu t giai phong giai tri nhap tu textbox1 t.Release(Convert.ToInt16(textBox2.Text)); } catch(Exception err) {} }
private void Form1_Load(object sender, EventArgs e) { textBox1.Text = "3"; textBox2.Text = "3"; luongnhan = new Thread(new ThreadStart(nhandulieu)); //cho chay nen luongnhan.IsBackground = true; luongnhan.Start(); } static int Cong = 2000; UdpClient udpClient = new UdpClient(Cong); Thread luongnhan;
private void nhandulieu() { IPEndPoint Congnhan; while (true) { try { //Nhan dia chi bat ky va cong bat ky roi gan vao bien Congnhan Congnhan = new IPEndPoint(IPAddress.Any, 0); Byte[] Chuoinhan = udpClient.Receive(ref Congnhan); string dulieunhan = Encoding.UTF8.GetString(Chuoinhan); textBox2.Text = dulieunhan; //Thuc hien nhu la nhan nut button2 button2_Click(null, null); } catch (Exception err) { } } } } }
Cau 3
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Net; using System.Net.Sockets; using System.Threading;
namespace Cau3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } static int Cong = 2000; UdpClient udpClient = new UdpClient(Cong); private void button1_Click(object sender, EventArgs e) { //lay gia tri trong textbox3 chuyen thanh dang Byte cho vao mang dulieugui Byte[] dulieugui = Encoding.UTF8.GetBytes(textBox3.Text); //nho doi tuong udpClient gui(dulieu,dodaidulieu,DiachiIp,Cong) sang cau 2 udpClient.Send(dulieugui, dulieugui.Length, textBox1.Text, Cong); udpClient.Close(); udpClient = new UdpClient(Cong); } } } |
|
duongminhhuy
Joined : 11 Dec 2007 Posts : 8
 | Subject: Re: Giai lai bai thi Ca2 HDH !! Mong thay xem xet!! 11/12/2007, 19:36 | |
| | Đề lớp T6A1 phải không bạn, lớp mình hình như đề ngược lại thì phải. |
|
chumap

Joined : 31 Oct 2007 Posts : 5 Localisation : TH07
 | Subject: Re: Giai lai bai thi Ca2 HDH !! Mong thay xem xet!! 19/1/2008, 08:09 | |
| Ban co the cho minh cai de thi ban vua thi dc hok? De minh tham khao va thi trong ky thi HDH sap toi. Co the ban gui qua mail minh dc hok? Email: chumapthoinay@yahoo.com or chumapthoinay@gmail.com Cam on ban truoc nghen. |
|