Hoc Tap

Learning
 
HomeHome  FAQFAQ  SearchSearch  RegisterRegister  Log inLog in  
 

SxTt5

View previous topic View next topic Go down 
AuthorMessage
spider




Joined : 19 Jun 2008
Posts : 21

PostSubject: SxTt5   23/6/2008, 21:02

Mới về là bay lên post liền Very Happy Mình chỉ kịp "Ctrl + A" --> "Ctrl + C" --> "Ctrl + V" thôi Smile thế nào cũng có chỗ sơ sót, bạn nào tinh mắt chỉnh sửa, bổ sung dùm mình nhe, thanks.
Code:

// SxTt5.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <stdio.h>
#include <conio.h>
#include <windows.h>

#define BUFFER_SIZE  10

int buffer[BUFFER_SIZE];
char s[BUFFER_SIZE];

int in=0;
int out=0;

int nextProduced=1;

HANDLE semEmpty, semFull;  // Hai đèn hiệu

CRITICAL_SECTION critSec; // Biến kiểu Mutex
 
HANDLE semProducer1, semProducer2;

void Producer(int p){   
   while (1){

      switch ((int)p){
         case 1:
            WaitForSingleObject(semProducer1, INFINITE);
            break;
         case 2:
            WaitForSingleObject(semProducer2, INFINITE);
            break;
         default:
            s[in]='S';
      };

      // ... Sản xuất (nextProduced)

      // Chờ đến khi có chỗ trống (đến khi giá trị của [semEmpty] >=1)
      WaitForSingleObject(semEmpty, INFINITE);

      EnterCriticalSection(&critSec);

      buffer[in]=nextProduced++;

      switch ((int)p){
         case 1:
            s[in]='P';
            break;
         case 2:
            s[in]='p';
            break;
         default:
            s[in]='S';
      };

      in=(in+1)%BUFFER_SIZE;
      
      //  Tăng giá trị [semFull] lên 1
      ReleaseSemaphore(semFull, 1, NULL);

      LeaveCriticalSection(&critSec);
      
      //   SuspendThread(GetCurrentThread());   
      Sleep(GetTickCount()%5000);
   }
}
 
void Consumer(){
   int nextConsumed;
   while (1){
      // Chờ đến khi có sản phẩm (đến khi giá trị đèn [semFull]>=1)
      WaitForSingleObject(semFull, INFINITE);

      EnterCriticalSection(&critSec);

      nextConsumed=buffer[out];
      out=(out+1)%BUFFER_SIZE;
      
      //  Tăng giá trị đèn [semEmpty] lên 1
      ReleaseSemaphore (semEmpty, 1, NULL);

      LeaveCriticalSection(&critSec);

      // ... Tiêu thụ (nextConsumed)

      //   SuspendThread(GetCurrentThread());
      Sleep(GetTickCount()%8000);
   }
}
 
void ShowBuffer(){   // In nội dung bộ đêm
   const char * LeftMargin="\n      ";
   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[BUFFER_SIZE-1], buffer[BUFFER_SIZE-1]);

   printf(LeftMargin);
   for(i=0; i<(out*5); i++) putchar('  ');
printf("^out");

   printf("\n");
}
 
int main(){
   HANDLE ProducerHandle1, ProducerHandle2;
   HANDLE ConsumerHandle1, ConsumerHandle2;

   DWORD ProducerID1, ProducerID2;
   DWORD ConsumerID1, ConsumerID2;
   

   semEmpty=CreateSemaphore(0, BUFFER_SIZE, BUFFER_SIZE, 0);
   semFull=CreateSemaphore(0, 0, BUFFER_SIZE, 0);

   InitializeCriticalSection(&critSec);

   semProducer1=CreateSemaphore(0, 3, 3, "PRODUCER1");
   semProducer2=CreateSemaphore(0, 5, 5, "PRODUCER2");

   // Tạo các luồng ở trạng thái chạy ngay
   ProducerHandle1=CreateThread(0,0,
(LPTHREAD_START_ROUTINE)Producer, (void *) 1, 0, &ProducerID1);
   ProducerHandle2=CreateThread(0,0,
(LPTHREAD_START_ROUTINE)Producer, (void *) 2, 0, &ProducerID2);

ConsumerHandle1=CreateThread(0,0,
(LPTHREAD_START_ROUTINE)Consumer, 0, 0, &ConsumerID1);
   ConsumerHandle2=CreateThread(0,0,
(LPTHREAD_START_ROUTINE)Consumer, 0, 0, &ConsumerID2);
 
   while(1)
   {
      
      EnterCriticalSection(&critSec);
      ShowBuffer();
      LeaveCriticalSection(&critSec);
      Sleep(1000);
   }
}
Back to top Go down
kieuoanh




Joined : 20 Jun 2008
Posts : 33

PostSubject: Re: SxTt5   27/6/2008, 12:01

Nhìn chung ko có gì sơ suất.Người đâu mà siêng dữ về là post liền
Back to top Go down

SxTt5

View previous topic View next topic Back to top 
Page 1 of 1

Permissions of this forum:You cannot reply to topics in this forum
Hoc Tap :: Môn học HĐH :: Hệ Điều Hành-