0% found this document useful (0 votes)
1 views6 pages

Cs609.... Assignment

Uploaded by

Xtylish Mughal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views6 pages

Cs609.... Assignment

Uploaded by

Xtylish Mughal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Code:

#include <iostream>

#include <windows.h>

#include <vector #include <thread> #include <chrono> .junaid Qazi

struct ThreadData

HANDLE hSrcFile;

HANDLE hDestFile;

LARGE INTEGER offset

DWORD chunkSize:

int threadNum;

DWORD bytesCopied;

void copyFileSegment(ThreadData* data)

LARGE INTEGER offset = data->offset;

DWORD chunkSize = data->chunkSize:

HANDLE HSrctile = data->hSrctile;

HANDLE hDestFile = data->hDestFile;


std::vcctor<char> buffer(chunkSize);

DWORD bytesRead = 0, bytes Written = 0;

SetFilePointerEx(hSrcFile, offset, NULL, FILE_ BEGIN);

ReadFile(hSrcFile, buffer. data(), chunkSize, &bytesRead, NULL);

SetFilePointerEx (hDestFile, offset, NULL, FILE BEGIN);

WriteFile(hDestFile, bufter. data(), bytesRead, &bytes Written, NULL)

data->bytesCopied = bytes Written:

std:cout < "Thread "<< data->threadNum + 1<<" copied " << bytes
Written

<<" bytes from offset" << offset.QuadPart <<std::endl:

int main() {

std::string srcFilePath;

std::string destFilePath;

int numThreads:

std::cout << "M Junaid Qazi"<< std::endl;

std::cout <«":: Multithreaded File Copying Utility ::"<< std::endl;

std::cout << "Enter the source file path: ";


std::getline(std::cin, srcFilePath):

std::cout << "Enter the destination file path: ";

std::getline(std::cin, destFilePath):

std::cout <<"Enter the number of thrcads: ";

std::cin >> numThreads:

std::cin.ignore(std::numeric limits<std::streamsize>:max(), n');

HANDLE hSrcFile = CreateFile(srcFilePath.c str(), GENERIC READ, 0, NULL,


OPEN EXISTING,

FILE_ATTRIBUTE_ NORMAL, NULL);

if (hSrcFile =INVALID HANDLE VALUE)

std"cerr <<"Error opening source file" << std:endl;

tL.Júnald Qazi

HANDLE hDestFile -CreateFile(destFilePath.c str0,. GENERIC WRITE, 0, NULL,

NULL;

CREATE ALWAYS, FILE ATTRIBUTE NORMAL,

if (hDestFile INVALID HANDLE_VALUE)

std:cerr << "Error opening destination file" << std::endl;

CloseHandle(hSrcFile):
return 1:;

LARGE INTEGER fileSize:

if (GetFileSizeEx(hSrcFile, &fileSize) {

std::cerr << "Error getting file size" << std::endl;

CloseHandle(hSrcFile):

CloseHandle(hDestFile ):

return I:

std::cout <<"Tile Size:" <« fileSize.QuadPart <<"bytcs" << std::cndl;

DWORD chunkSize= static cast<DWORD>(fileSize.QuadPart / numThreads);

std::vector<std::thread> threads:

std::vector<ThreadData> threadData(numThreads);

auto startTime = std::chrono:high resolution clock::now);

for (int i = 0; i< numThreads; +i) {

LARGE INTEGER offset;

offset.QuadPart - chunkSize,

threadDatali] = {hSrcFile, hDestFile, offset, chunkSize, i, 0}:


threads.emplace back(copyFileSegment, &Rahrendbatall) 60034

WnAtSApp:T

for (auto&t:threads)

tjoin();

CONTACT ON WHATSAPP

+923074960034

auto endTime = std::chrono::high resolution clock::now);

std::chrono::duration<double> elapsedTime = endTime - startTime;

DWORD totalBytesCopied = 0;

for (const auto& data : threadData)

totalBytesCopied += data.bytesCopied;

std::cout <«"File copied in " <<< elapsedTime.count() <<" seconds." <<


std.::endl:

CloseHandle(hSrcFile);

CloseHandle(hDestFile):

Return 0;

OUTPUT:

You might also like