100% found this document useful (1 vote)
91 views8 pages

CS609 Assignment 2 Solution Spring 2024

CS609_Assignment_2_Solution_S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
100% found this document useful (1 vote)
91 views8 pages

CS609 Assignment 2 Solution Spring 2024

CS609_Assignment_2_Solution_S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 8
Due Date: 24-June-2024 Total Marks: 20 FOR PAID SOLUTION WITH YOUR OWN STUDENTS ID CONTACT WHATSAPP +923162965677 Assignment Submission Instruction: 1. You need to upload the following two files in a single .zip or .rar format: (i) .CPP file (C++ source file, containing code) (ii) .exe file (executable file, created after compilation) You should print your student ID at the top of output screen. You will be awarded zero marks if the mentioned student ID is not yours. Problem Statement: Write a C++ program that copies a file from one location to another using multiple threads to read and write the file in chunks. This will involve using the Windows API for process and thread management, as well as file I/O operations Assignment Tasks: 1. The program should take three command-line arguments: * Source file path ‘* Destination file path ‘* Number of threads to use 2. Use the Windows API to open the source and destination files. 3. Divide the file into chunks and assign each chunk to a separate thread. 4, Each thread will read its assigned chunk from the source file and write it to the destination file. 5. Ensure correct synchronization and handle any potential errors during file operations. 6. Measure and display the time taken to copy the file using the specified number of threads. Output Requirements: 1. After taking the inputs, program should display the following information as the output: a eens Files Size Thread number Number of bytes copied by each thread Offset from where data was copied Total time taken to copy the file. #include #include Hinclude #include #include struct ThreadData { HANDLE hSrcFile; HANDLE hDestFile; LARGE_INTEGER offset; DWORD chunkSize; int threadNum; DWORD bytesCopied; i void copyFileSegment(ThreadData* data) { LARGE_INTEGER offset = data-roffset; DWORD chunkSize = data->chunkSize; HANDLE hSrcFile = data->hSrcFile; HANDLE hDestfile = data->hDestFile; std::vector buffer(chunksize); DWORD bytesRead = 0, bytesWritten = 0; SetFilePointerEx(hSrcFile, offset, NULL, FILE_BEGIN); Readfile(hSrcFile, buffer.data(), chunkSize, &bytesRead, NULL); SetFilePointerEx(hDestFile, offset, NULL, FILE_BEGIN); WriteFile(hDestFile, buffer.data(), bytesRead, &bytesWritten, NULL); data->bytesCopied = bytesWritten; std::cout << "Thread " << data->threadNum + 1 << " copied " << bytesWritten <<" bytes from offset " << offset.QuadPart << std::endl; } int main() { std::string srcFilePath; std::string destFilePath; int numThreads; std::cout <<"BC123456789" << 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 thread: std::cin >> numThreads; std::cin.ignore(std::numeric_limits::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; return 1; HANDLE hDestfile = CreateFile(destFilePath.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (hDestFile == INVALID_HANDLE_VALUE) { std::cerr << "Error opening destination file" << std::endl; CloseHandle(hsrcFile); return 1; LARGE_INTEGER fileSize; if (IGetFileSizeEx(hSrcFile, &fileSize)) { stds:cerr << "Error getting file size" << std::endl; CloseHandle(hsrcFile); CloseHandle(hDestFile); return 1; std::cout << "File Size: " << fileSize.QuadPart <<" bytes" << std::endl; DWORD chunkSize = static_cast(fileSize.QuadPart / numThreads); std::vector threads; std::vector threadData(numThreads); auto startTime = std::chrono::high_resolution_clock::now(); for (int i = 0; 1 < numThreads; ++i) { LARGE_INTEGER offset; offset.QuadPart = i * chunkSize; threadDatali] = { hSrcFile, hDestFile, offset, chunkSize, i, 0 }; threads.emplace_back(copyFileSegment, &threadDatalil); for (auto& t : threads) { tjoin(); auto endTime = std::chronohigh_resolution_clock::now(); std::chrono::duration 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; main.cpp. LARGE_INTEGER offsets offset.QuadPart = i * chunkSizes return 05 WHATSAPP +923162965677 PLEASE NOTE: Don't copy-paste the same answer. Make sure you can make some changes to your solution file before submitting copy paste solution will be marked zero. If you found any mistake then correct yourself and inform me. Before submitting an assignment must check your assignment requirement file. If you need some help or question about file and solutions feel free to ask. VUStudentspk.com

You might also like