Disk filler multi threading

Contenu du snippet

petit programme qui remplit un disque dur avec des fichiers pleins de zéros!

la partie graphique n'est pas extraordinaire mais c'est la premiere fois que je code un programme en multithreading.

Source / Exemple :


#include <windows.h>
#include <iostream>
#include <conio.h>
#include <iomanip>
#include <string>
#include <cmath>
#include <fstream>
using namespace std;

//Header declarations
long WINAPI WriteToDisk(int koko);
void gotoxy(int x, int y );
bool CheckIfAllFlase(void);
double WrittenData[10];
  HANDLE hThread[10];
  DWORD dwID[10];
  DWORD dwRetVal = 0;
  long double TotalData_d;
  DWORD TotalThread;
  long double TotalDataByThread;
  long double ActualWrittenBytes;
  long double LastActualWrittenBytes;
  string TempData;
  long double SizeOfTempData = 20971520;
  bool started[10];

int main(void){
	ActualWrittenBytes = 0;
	LastActualWrittenBytes = 0;
  //int DataByThread;
  cout << "What space would you want to fill in Bytes ( more than 1024 * number of Thread ):";
	cin >> TotalData_d;
	do{
		cout << "How much Thread will be running [1 to 10]:";
		cin >> TotalThread;
	}while(TotalThread >= 11 || TotalThread == 0);
	TotalDataByThread = ceil(TotalData_d / TotalThread);

	
	cout << endl << "Each Thread will have to generate " << setprecision(25) << TotalDataByThread << " Bytes of Data";
	cout << endl << endl << "Generating Temporary Data: 1 048 576 zeros... (1MB)";
	for(int i=0; i< 9; i++)
		started[i] = false;
	for(int i=1; i< SizeOfTempData+1; i++)
		TempData.insert(i-1,"0");
	cout << endl << endl << "Temporary Data Successfully created Are you Ready to fill your Disk?";
	cout << endl << "Press any key to continue or close this window";
	_getch();

	system("cls");
	cout << "Starting Processes...";
	cout << endl;
	for(int i=0; i < int(TotalThread); i++){
		hThread[i] = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)WriteToDisk,NULL,0,&dwID[i]);
	cout << "\t Thread #" << i+1 << " is running..." << endl;
	}
	do{
		gotoxy(0,0);
		ActualWrittenBytes = 0;
		for(int i=0; i< 9; i++)
			ActualWrittenBytes += WrittenData[i];
		//system("cls");
		cout << "Starting Processes..." << endl;
		for(int i=0; i< int(TotalThread); i++){
			cout << "\t Thread #" << i+1 << " is running...";
			cout << WrittenData[i] << " Bytes" << endl;
		}
		cout << endl << "Writing Speed : " << ceil((ActualWrittenBytes - LastActualWrittenBytes) / ( 1024) / (1024)) << " MB/s";
		cout << endl << "Progress : " << ceil((ActualWrittenBytes / TotalData_d) * 100) << "%";
		cout << endl << "Total Written Data : " << ceil(ActualWrittenBytes / 1024 / 1024) << " MB on " << ceil(TotalData_d /1024 /1024) << " MB";
		LastActualWrittenBytes = ActualWrittenBytes;
		Sleep(1000);
	}while(CheckIfAllFlase());

	for(int i=0; i< int(TotalThread); i++)
		CloseHandle(hThread[i]);

	cout << endl << endl << endl << "Work is Done!!!";
	_getch();
  return 0;
}

long WINAPI WriteToDisk(int koko){
	byte TH;
	TH=0;
	do{
		if(started[TH] == true)
			TH++;
	}while(started[TH] == true);
	started[TH]= true;
	WrittenData[TH] = 0;
	fstream myfile;
	string path = "E:\\";
	char ID_c[2];
	itoa(TH+1,ID_c,10);
	path.insert(3,ID_c);
	myfile.open( path.c_str());
	long double Repeat = ceil(TotalDataByThread / SizeOfTempData);
	for(int i=0; i < Repeat; i++){
		myfile << TempData;
		WrittenData[TH] += SizeOfTempData;
	}
	myfile.close();
	started[TH]= false;
	return 0;
}

void gotoxy(int x, int y ){
  COORD coord;
  coord.X = x;
  coord.Y = y;
  SetConsoleCursorPosition(
    GetStdHandle( STD_OUTPUT_HANDLE ),
    coord
    );

}

bool CheckIfAllFlase(void){
	for(int i=0; i< 9; i++){
		if(started[i]==true)
			return true;
	}
	return false;
}

Conclusion :


Des suggestion ou commentaire seront les bienvenus!

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.