{"id":22815878,"url":"https://github.com/shreyasmehta05/virtufile","last_synced_at":"2026-02-16T15:01:57.035Z","repository":{"id":267043165,"uuid":"900106630","full_name":"shreyasMehta05/VirtuFile","owner":"shreyasMehta05","description":"A thread-safe file system simulation in C, supporting concurrent read, write, and delete operations with synchronization using pthreads.","archived":false,"fork":false,"pushed_at":"2024-12-07T22:20:35.000Z","size":23,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-05T01:28:54.390Z","etag":null,"topics":["c","concurrency","concurrent-programming","filesystem"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shreyasMehta05.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-12-07T21:58:52.000Z","updated_at":"2024-12-07T22:20:39.000Z","dependencies_parsed_at":"2024-12-07T23:17:21.713Z","dependency_job_id":"1a5844f1-c05f-4422-bf1a-9f2d61978540","html_url":"https://github.com/shreyasMehta05/VirtuFile","commit_stats":null,"previous_names":["shreyasmehta05/virtufile"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/shreyasMehta05/VirtuFile","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shreyasMehta05%2FVirtuFile","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shreyasMehta05%2FVirtuFile/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shreyasMehta05%2FVirtuFile/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shreyasMehta05%2FVirtuFile/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shreyasMehta05","download_url":"https://codeload.github.com/shreyasMehta05/VirtuFile/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shreyasMehta05%2FVirtuFile/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29510518,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-16T09:05:14.864Z","status":"ssl_error","status_checked_at":"2026-02-16T08:55:59.364Z","response_time":115,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c","concurrency","concurrent-programming","filesystem"],"created_at":"2024-12-12T14:05:33.290Z","updated_at":"2026-02-16T15:01:57.018Z","avatar_url":"https://github.com/shreyasMehta05.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# **Lazy File Management System (LFMS)** 🚀  \n![Waketime](https://img.shields.io/badge/Waketime-47%20hrs%2032%20mins-blueviolet?style=flat\u0026labelColor=black\u0026logo=clock\u0026logoColor=white)\n\n\n## **Author**  \n👤 **Shreyas Mehta**  \n\n---\n\n## **Overview**  \n\nThe **Lazy File Management System (LFMS)** simulates a **file management system** where multiple users access files **concurrently**. It supports operations like **read**, **write**, and **delete** while ensuring **synchronization** and **mutual exclusion**.  \n\n🔑 **Key Features**:  \n- Uses **pthreads** to handle **concurrent requests**.  \n- Ensures **thread safety** with **mutexes** and **condition variables**.  \n- Guarantees proper synchronization for all operations.  \n\nThis project was implemented in **C** as part of the course *Operating Systems and Networks*.  \n\n---\n\n## **Key Data Structures**  \n\n### **1. `UserQuery` Structure** 📝  \nA `UserQuery` represents a single user request and includes:  \n- **`userID`**: Identifier of the user.  \n- **`fileID`**: Identifier of the file.  \n- **`fileOperation`**: Operation to perform (📖 `0 = Read`, ✏️ `1 = Write`, ❌ `2 = Delete`).  \n- **`reqTime`**: Request timestamp in seconds.  \n\n---\n\n### **2. `FileStatus` Structure** 📂  \nA `FileStatus` tracks the state of a file:  \n- **`isPresent`**: Indicates if the file exists.  \n- **`writeInProgress`**: Flags if a write operation is ongoing.  \n- **`deleteInProgress`**: Flags if the file is being deleted.  \n- **`ReaderCount`**: Number of active readers.  \n- **`currentUserCount`**: Total users accessing the file.  \n- **`threadLock`**: Mutex for synchronizing file access.  \n- **`signalCondition`**: Condition variable to manage threads waiting for access.  \n\n\n## **Highlights**  \n✨ Implements **thread-safe file operations** using **pthreads**.  \n✨ Synchronizes **concurrent user requests** with **mutexes** and **condition variables**.  \n✨ Efficient handling of **read**, **write**, and **delete** operations.  \n\n## **Functions Breakdown**\n\n### **1. `resetFileStatus()`**\n\n#### **Description:**\nThis function resets the status of all files in the system to their initial state, ensuring that no operations (read, write, or delete) are ongoing and that files are available for future operations.\n\n```c\nvoid resetFileStatus() {\n    for (int i = 0; i \u003c MAX_FILES; i++) {\n        fileStatus[i].isPresent = true;\n        fileStatus[i].writeInProgress = false;\n        fileStatus[i].deleteInProgress = false;\n        fileStatus[i].ReaderCount = 0;\n        fileStatus[i].currentUserCount = 0;\n    }\n}\n```\n\n---\n\n### **2. `processInput()`**\n\n#### **Description:**\nThis function processes user input, which could include commands to read, write, or delete files. The input is parsed and translated into a `UserQuery` that is pushed to the query queue for further processing.\n\n```c\nvoid processInput() {\n    scanf(\"%d %d %d\", \u0026readTime, \u0026writeTime, \u0026deleteTime);\n    scanf(\"%d %d %d\", \u0026fileCount, \u0026maxUsers, \u0026maxWaitTime);\n\n    char inputBuffer[20], command[100];\n    int userID, fileID, reqTime;\n    int queryIndex = 0;\n\n    while (true) {\n        scanf(\"%s\", inputBuffer);\n        if (strcmp(inputBuffer, \"STOP\") == 0) {\n            break;\n        }\n        userID = getUserId(inputBuffer);\n        scanf(\"%d %s %d\", \u0026fileID, command, \u0026reqTime);\n        int operation = mapOperationID(command);\n        if (operation == _INVALID_OP_) {\n            printf(RED \"Invalid operation\\n\" RESET);\n            continue;\n        }\n        initialiseQuery(\u0026queryArray[queryIndex++], userID, fileID - 1, operation, reqTime);\n    }\n\n    // Sort the query array before pushing to queue\n    qsort(queryArray, queryIndex, sizeof(UserQuery), compareQueries);\n\n    // Push sorted queries to the queue\n    for (int i = 0; i \u003c queryIndex; i++) {\n        pushQueryToQueue(queryArray[i]);\n        queryCount++;\n    }\n\n    resetFileStatus();\n    initTime = time(NULL);\n}\n```\n\n---\n\n### **3. `handleIncomingRequests()`**\n\n#### **Description:**\nThis function handles incoming requests from the query queue, processes them, and delegates the appropriate actions (read, write, or delete) to the relevant functions like `handleFileRead()`, `handleFileWrite()`, and `handleFileDeletion()`.\n\n```c\nvoid handleIncomingRequests() {\n    while (!isQueueEmpty()) {\n        UserQuery query = popQuery();\n        int fileID = query.fileID;\n\n        switch (query.fileOperation) {\n            case 0:\n                handleFileRead(query, fileID);\n                break;\n            case 1:\n                handleFileWrite(query, fileID);\n                break;\n            case 2:\n                handleFileDeletion(query, fileID);\n                break;\n            default:\n                // Invalid operation\n                break;\n        }\n    }\n}\n```\n\n---\n\n### **4. `fetchCurrentTime()`**\n\n#### **Description:**\nThis function retrieves the current system time in seconds, typically used for timestamping queries or tracking the elapsed time.\n\n```c\nint fetchCurrentTime() {\n    return (int)time(NULL); // Return current time in seconds\n}\n```\n\n---\n\n### **5. `processQuery(void *arg)`**\n\n#### **Description:**\nThis function is the entry point for each worker thread. It processes a single query from the queue and invokes the relevant file operation (read, write, or delete).\n\n```c\nvoid *processQuery(void *arg) {\n    UserQuery *query = (UserQuery *)arg;\n    int fileID = query-\u003efileID;\n\n    // Handle the query based on its type\n    switch (query-\u003efileOperation) {\n        case 0:\n            handleFileRead(*query, fileID);\n            break;\n        case 1:\n            handleFileWrite(*query, fileID);\n            break;\n        case 2:\n            handleFileDeletion(*query, fileID);\n            break;\n        default:\n            break;\n    }\n\n    return NULL;\n}\n```\n\n---\n\n### **6. `pushQueryToQueue(UserQuery query)`**\n\n#### **Description:**\nThis function pushes a `UserQuery` onto the query queue. If the queue is full, it waits for space to become available before pushing the query.\n\n```c\nvoid pushQueryToQueue(UserQuery query) {\n    // Lock the queue before modifying it\n    pthread_mutex_lock(\u0026queueLock);\n\n    while (isQueueFull()) {\n        pthread_cond_wait(\u0026queueNotFull, \u0026queueLock);\n    }\n\n    // Add the query to the queue\n    queryQueue[rear] = query;\n    rear = (rear + 1) % QUEUE_SIZE;\n\n    // Signal that the queue is not empty\n    pthread_cond_signal(\u0026queueNotEmpty);\n    pthread_mutex_unlock(\u0026queueLock);\n}\n```\n\n---\n\n### **7. `popQuery()`**\n\n#### **Description:**\nThis function pops a `UserQuery` from the front of the query queue. If the queue is empty, it waits until a query becomes available.\n\n```c\nUserQuery popQuery() {\n    pthread_mutex_lock(\u0026queueLock);\n\n    while (isQueueEmpty()) {\n        pthread_cond_wait(\u0026queueNotEmpty, \u0026queueLock);\n    }\n\n    UserQuery query = queryQueue[front];\n    front = (front + 1) % QUEUE_SIZE;\n\n    pthread_cond_signal(\u0026queueNotFull);\n    pthread_mutex_unlock(\u0026queueLock);\n\n    return query;\n}\n```\n\n---\n\n### **8. `isQueueEmpty()`**\n\n#### **Description:**\nThis function checks whether the query queue is empty.\n\n```c\nbool isQueueEmpty() {\n    return (front == rear);\n}\n```\n\n---\n\n### **9. `isQueueFull()`**\n\n#### **Description:**\nThis function checks whether the query queue is full.\n\n```c\nbool isQueueFull() {\n    return ((rear + 1) % QUEUE_SIZE == front);\n}\n```\n\n---\n\n### **10. `handleFileRead(UserQuery query, int fileID)`**\n\n#### **Description:**\nThis function handles the read operation for a file specified by `fileID`. It ensures that the file is available for reading, performs necessary checks, and simulates the read operation.\n\n```c\nbool handleFileRead(UserQuery query, int fileID){\n    FileStatus* file = \u0026fileStatus[fileID];\n    pthread_mutex_lock(\u0026file-\u003ethreadLock);\n    file-\u003ewaiting_requests++;\n\n    if(!waitForOneSecondHelper(query,file) || !waitForAccessRead(query,file) || !checkResponseTime(query) || !checkCondForRead(query,file)){\n        file-\u003ewaiting_requests--;\n        pthread_mutex_unlock(\u0026file-\u003ethreadLock);\n        return false;\n    }\n    file-\u003ewaiting_requests--;\n\n    // Start the read operation\n    startRead(query,file);\n    pthread_mutex_unlock(\u0026file-\u003ethreadLock);\n    \n    // simulate reading\n    sleep(readTime);\n\n    // complete the read operation\n    pthread_mutex_lock(\u0026file-\u003ethreadLock);\n    finishRead(query,file);\n    for (int i=0;i\u003cfile-\u003ewaiting_requests;i++)\n    {\n        pthread_cond_signal(\u0026file-\u003esignalCondition);\n        usleep(1000);\n    }\n    pthread_mutex_unlock(\u0026file-\u003ethreadLock);\n    return true;\n}\n```\n\n---\n\n### **11. `handleFileWrite(UserQuery query, int fileID)`**\n\n#### **Description:**\nThis function handles the write operation for a specific file identified by `fileID`. It ensures that the file is available for writing and performs necessary checks.\n\n```c\nbool handleFileWrite(UserQuery query, int fileID){\n    FileStatus* file = \u0026fileStatus[fileID];\n    pthread_mutex_lock(\u0026file-\u003ethreadLock);\n\n    file-\u003ewaiting_requests++;\n    if(!waitForOneSecondHelper(query,file) || !waitForAccessWrite(query,file) || !checkResponseTime(query) || !checkCondForWrite(query,file)){\n        file-\u003ewaiting_requests--;\n        pthread_mutex_unlock(\u0026file-\u003ethreadLock);\n        return false;\n    }\n    file-\u003ewaiting_requests--;\n\n    // Start the write operation\n    startWrite(query,file);\n    pthread_mutex_unlock(\u0026file-\u003ethreadLock);\n\n    // simulate writing\n    sleep(writeTime);\n\n    // complete the write operation\n    pthread_mutex_lock(\u0026file-\u003ethreadLock);\n    completeWrite(query,file);\n    for (int i=0;i\u003cfile-\u003ewaiting_requests;i++)\n    {\n        pthread_cond_signal(\u0026file-\u003esignalCondition);\n        usleep(1000);\n    }\n    pthread_mutex_unlock(\u0026file-\u003ethreadLock);\n\n    return true;  \n}\n```\n\n---\n\n### **12. `handleFileDeletion(UserQuery query, int fileID)`**\n\n#### **Description:**\nThis function handles the delete operation for a specific file identified by `fileID`. It ensures that the file is available for deletion.\n\n```c\nbool handleFileDeletion(UserQuery query, int fileID){\n    FileStatus* file = \u0026fileStatus[fileID];\n    pthread_mutex_lock(\u0026file-\u003ethreadLock);\n    file-\u003ewaiting_requests++;\n\n    if(!waitForOneSecondHelper(query,file) || !waitForAccessDelete(query,file) || !checkResponseTime(query) || !checkCondForDelete(query,file)){\n        file-\u003ewaiting_requests--;\n        pthread_mutex_unlock(\u0026file-\u003ethreadLock);\n        return false;\n    }\n    file-\u003ewaiting_requests--;\n    // Start the delete operation\n    startDelete(query,file);\n    pthread_mutex_unlock(\u0026file-\u003ethreadLock);\n\n    // simulate delete operation\n    sleep(deleteTime);\n\n    // complete the delete operation\n    pthread_mutex_lock(\u0026file-\u003ethreadLock);\n    completeDelete(query,file);\n    pthread_cond_broadcast(\u0026file-\u003esignalCondition);\n    pthread_mutex_unlock(\u0026file-\u003ethreadLock);\n    return true;\n}\n```\n\n---\n\n### **13. `initializeMain()`**\n\n#### **Description:**\nThis function initializes the main system, including setting up mutexes, condition variables, and the query queue.\n\n```c\nbool initializeMain() {\n    // Initialize mutexes and condition variables\n    pthread_mutex_init(\u0026queueLock, NULL);\n    pthread_cond_init(\u0026queueNotEmpty, NULL);\n    pthread_cond_init(\u0026queueNotFull, NULL);\n    return true;\n}\n```\n\n---\n\n### **14. `compareQueries(const void *a, const void *b)`**\n\n#### **Description:**\nThis function compares two `UserQuery` structures to sort them based on their request time.\n\n```c\nint compareQueries(const void *a, const void *b) {\n    return ((UserQuery*)a)-\u003ereqTime - ((UserQuery*)b)-\u003ereqTime;\n}\n```\n\n---\n\n\n#### 15. **checkCondForDelete**\n```c\nbool checkCondForDelete(UserQuery query, FileStatus* file) {\n    // Check if file exists and is not being deleted.\n    pthread_mutex_lock(\u0026file-\u003efileLock);\n    if (!file-\u003eisPresent || file-\u003edeleteInProgress) {\n        pthread_mutex_unlock(\u0026file-\u003efileLock);\n        return false;\n    }\n    pthread_mutex_unlock(\u0026file-\u003efileLock);\n    return true;\n}\n```\n**Description:**  \nThis function checks if the file is present and not in the process of being deleted before allowing the delete operation to proceed. It uses a mutex lock to ensure thread safety.\n\n---\n\n#### 16. **startDelete**\n```c\nvoid startDelete(UserQuery query, FileStatus* file) {\n    // Start the delete operation for a file.\n    pthread_mutex_lock(\u0026file-\u003efileLock);\n    file-\u003edeleteInProgress = true;\n    file-\u003euserCount++;\n    pthread_mutex_unlock(\u0026file-\u003efileLock);\n}\n```\n**Description:**  \nThis function marks a file as being in the process of deletion and increments the user count to track the number of operations on the file.\n\n---\n\n#### 17. **completeDelete**\n```c\nvoid completeDelete(UserQuery query, FileStatus* file) {\n    // Complete the delete operation for the file.\n    pthread_mutex_lock(\u0026file-\u003efileLock);\n    file-\u003edeleteInProgress = false;\n    file-\u003euserCount--;\n    file-\u003eisPresent = false; // File is deleted and no longer available\n    pthread_mutex_unlock(\u0026file-\u003efileLock);\n}\n```\n**Description:**  \nMarks the delete operation as complete, decrements the user count, and sets the file's `isPresent` flag to `false`, indicating the file is no longer available.\n\n---\n\n#### 18. **waitForAccessDelete**\n```c\nbool waitForAccessDelete(UserQuery query, FileStatus* file) {\n    // Wait until the file is available for deletion (not being accessed by other users).\n    pthread_mutex_lock(\u0026file-\u003efileLock);\n    if (file-\u003edeleteInProgress || file-\u003euserCount \u003e 0) {\n        pthread_mutex_unlock(\u0026file-\u003efileLock);\n        return false; // Cannot access the file if it's being deleted or in use\n    }\n    pthread_mutex_unlock(\u0026file-\u003efileLock);\n    return true;\n}\n```\n**Description:**  \nThis function ensures the file is not being deleted or accessed by other users before allowing a delete operation to proceed. It uses a mutex lock to prevent race conditions.\n\n---\n\n#### 19. **checkCondForWrite**\n```c\nbool checkCondForWrite(UserQuery query, FileStatus* file) {\n    // Check if the file exists and is not being deleted or written to.\n    pthread_mutex_lock(\u0026file-\u003efileLock);\n    if (!file-\u003eisPresent || file-\u003ewriteInProgress || file-\u003edeleteInProgress) {\n        pthread_mutex_unlock(\u0026file-\u003efileLock);\n        return false;\n    }\n    pthread_mutex_unlock(\u0026file-\u003efileLock);\n    return true;\n}\n```\n**Description:**  \nChecks the conditions necessary for performing a write operation: the file must exist, and it must not be under deletion or have an ongoing write operation.\n\n---\n\n#### 20. **waitForAccessWrite**\n```c\nbool waitForAccessWrite(UserQuery query, FileStatus* file) {\n    // Wait until the file is available for writing (not being written to).\n    pthread_mutex_lock(\u0026file-\u003efileLock);\n    if (file-\u003ewriteInProgress || file-\u003euserCount \u003e= 1) {\n        pthread_mutex_unlock(\u0026file-\u003efileLock);\n        return false; // Cannot access file if it's being written to or being used by other users\n    }\n    pthread_mutex_unlock(\u0026file-\u003efileLock);\n    return true;\n}\n```\n**Description:**  \nWaits for the file to be available for writing. If the file is being written to or is being accessed by other users, the function will return `false` and wait until the conditions are met.\n\n---\n\n#### 21. **startWrite**\n```c\nvoid startWrite(UserQuery query, FileStatus* file) {\n    // Start the write operation for a file.\n    pthread_mutex_lock(\u0026file-\u003efileLock);\n    file-\u003ewriteInProgress = true;\n    file-\u003euserCount++;\n    pthread_mutex_unlock(\u0026file-\u003efileLock);\n}\n```\n**Description:**  \nThis function starts the write operation for a file, marking it as \"in-progress\" and incrementing the user count to track the number of operations on the file.\n\n---\n\n#### 22. **completeWrite**\n```c\nvoid completeWrite(UserQuery query, FileStatus* file) {\n    // Complete the write operation for the file.\n    pthread_mutex_lock(\u0026file-\u003efileLock);\n    file-\u003ewriteInProgress = false;\n    file-\u003euserCount--;\n    pthread_mutex_unlock(\u0026file-\u003efileLock);\n}\n```\n**Description:**  \nCompletes the write operation, marks the file as no longer being written to, and decrements the user count.\n\n---\n\n#### 23. **waitForOneSecondHelper**\n```c\nbool waitForOneSecondHelper(UserQuery query, FileStatus* file) {\n    // Ensure the file operation is delayed by at least one second.\n    while (!(fetchCurrentTime() \u003e= query.reqTime + 1)) {\n        pthread_mutex_unlock(\u0026file-\u003efileLock);\n        usleep(__SLEEP_TIME__);\n        pthread_mutex_lock(\u0026file-\u003efileLock);\n    }\n    return true;\n}\n```\n**Description:**  \nWaits for at least one second after the request time before allowing the file operation to proceed. This ensures that file operations are not executed immediately, allowing the system to manage concurrency.\n\n---\n\n#### 24. **waitForAccessRead**\n```c\nbool waitForAccessRead(UserQuery query, FileStatus* file) {\n    // Wait until the file is available for reading.\n    pthread_mutex_lock(\u0026file-\u003efileLock);\n    if (file-\u003euserCount \u003e= maxUsers || file-\u003ewriteInProgress) {\n        pthread_mutex_unlock(\u0026file-\u003efileLock);\n        return false; // File cannot be read if it has too many users or is being written to\n    }\n    pthread_mutex_unlock(\u0026file-\u003efileLock);\n    return true;\n}\n```\n**Description:**  \nWaits for the file to be available for reading. If the file is being written to or has too many users accessing it, the function will return `false` and wait until conditions are met.\n\n---\n\n#### 25. **checkResponseTime**\n```c\nbool checkResponseTime(UserQuery query) {\n    // Check if the response time has exceeded the maximum allowed wait time.\n    int currentTime = fetchCurrentTime();\n    if (currentTime - query.reqTime \u003e= maxWaitTime) {\n        printf(RED \"User %d canceled the request due to no response at %d seconds [RED]\\n\" RESET, query.userID, query.reqTime + maxWaitTime);\n        return false;\n    }\n    return true;\n}\n```\n**Description:**  \nThis function checks if the response time for a request exceeds the maximum allowed wait time. If it does, it cancels the request and returns `false`.\n\n---\n\n#### 26. **checkCondForRead**\n```c\nbool checkCondForRead(UserQuery query, FileStatus* file) {\n    // Check if the file exists and is not being deleted.\n    pthread_mutex_lock(\u0026file-\u003efileLock);\n    if (!file-\u003eisPresent || file-\u003edeleteInProgress) {\n        pthread_mutex_unlock(\u0026file-\u003efileLock);\n        return false;\n    }\n    pthread_mutex_unlock(\u0026file-\u003efileLock);\n    return true;\n}\n```\n**Description:**  \nEnsures that the file exists and is not being deleted before allowing a read operation to proceed.\n\n---\n\n#### 27. **startRead**\n```c\nvoid startRead(UserQuery query, FileStatus* file) {\n    // Start the read operation for a file.\n    pthread_mutex_lock(\u0026file-\u003efileLock);\n    file-\u003euserCount++;\n    file-\u003eReaderCount++;\n    pthread_mutex_unlock(\u0026file-\u003efileLock);\n}\n```\n**Description:**  \nMarks the file as being accessed for reading and increments the count of active readers.\n\n---\n\n#### 28. **finishRead**\n```c\nvoid finishRead(UserQuery query, FileStatus* file) {\n    // Complete the read operation for the file.\n    pthread_mutex_lock(\u0026file-\u003efileLock);\n    file-\u003euserCount--;\n    file-\u003eReaderCount--;\n    pthread_mutex_unlock(\u0026file-\u003efileLock);\n}\n```\n**Description:**  \nMarks the read operation as completed, decrementing the user count and the reader count.\n\n---\n\n#### 29. **awaitRequestCompletion**\n```c\nvoid awaitRequestCompletion(pthread_t threads[]) {\n    // Wait for all threads to complete before proceeding.\n    for (int i = 0; i \u003c queryCount; i++) {\n        pthread_join(threads[i], NULL);\n    }\n    simulationComplete = true;\n    printf(GREEN \"LAZY has no more pending requests and is going back to sleep!\\n\" RESET);\n}\n```\n**Description:**  \nWaits for all threads (file operation requests) to complete before ending the simulation and going back to sleep.\n\n---\n\n#### 30. **handleIncomingRequests**\n```c\nvoid handleIncomingRequests() {\n    pthread_t threads[queryCount];\n    for (int i = 0; i \u003c queryCount; i++) {\n        pthread_create(\u0026threads[i], NULL,\n\n executeOperation, (void*)\u0026userRequests[i]);\n    }\n    awaitRequestCompletion(threads);\n}\n```\n**Description:**  \nHandles incoming requests by spawning a new thread for each user request and awaiting their completion.\n\n---\n\n#### 31. **waitingPeriod**\n```c\nbool waitingPeriod(UserQuery query) {\n    int currentTime = fetchCurrentTime();\n    return currentTime - query.reqTime \u003c maxWaitTime;\n}\n```\n**Description:**  \nDetermines if the wait time has expired for a given user request, ensuring that no operation is allowed beyond the defined maximum wait time.\n\n---\n\n### **Mapping File Operations**\n\n```c\nvoid mapOperation(char* operation, int fileOperation) {\n    // Map the user request to the corresponding file operation\n    switch (fileOperation) {\n        case 0: // Read\n            executeReadOperation();\n            break;\n        case 1: // Write\n            executeWriteOperation();\n            break;\n        case 2: // Delete\n            executeDeleteOperation();\n            break;\n        default:\n            printf(\"Invalid operation\\n\");\n    }\n}\n```\n\n**Description:**  \nMaps the requested file operation to the corresponding function, ensuring that the correct file operation is executed based on the user's input.\n\n---\n\n### **Conclusion**\n\nThe system ensures that file operations (read, write, and delete) are performed safely and concurrently using mutex locks to prevent race conditions. Each function is responsible for handling specific parts of the file operation, ensuring proper synchronization and avoiding conflicts between different user requests.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshreyasmehta05%2Fvirtufile","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshreyasmehta05%2Fvirtufile","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshreyasmehta05%2Fvirtufile/lists"}