-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWrite.cpp
More file actions
110 lines (91 loc) · 3.18 KB
/
Write.cpp
File metadata and controls
110 lines (91 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <fstream>
#include <pthread.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <mutex>
using namespace std;
int *SharedData;
mutex mtx; // Define a mutex object
void* createFile(void* filename) {
string fname = *((string*)filename);
mtx.lock(); // Lock the mutex before accessing shared resources
// Check if the file exists
ifstream checkFile(fname);
if (checkFile) {
cout << "File \"" << fname << "\" already exists!" << endl;
checkFile.close();
// Open the existing file for appending
ofstream writeFile(fname, ios::app);
if (writeFile) {
// Write data to the file
string data;
cout << "Write the Data You Want to Write in File:" << endl;
getline(cin, data);
writeFile << data << endl;
cout << "Data written to file \"" << fname << "\"!" << endl;
writeFile.close();
} else {
cout << "Failed to open file \"" << fname << "\" for writing!" << endl;
}
} else {
// Create a new file if it doesn't exist
ofstream file(fname);
file.close();
cout << "File \"" << fname << "\" created successfully!" << endl;
ofstream writeFile(fname, ios::app);
if (writeFile) {
// Write data to the file
string data;
cout << "Write the Data You Want to Write in File:" << endl;
getline(cin, data);
writeFile << data << endl;
cout << "Data written to file \"" << fname << "\"!" << endl;
writeFile.close();
} else {
cout << "Failed to open file \"" << fname << "\" for writing!" << endl;
}
}
mtx.unlock(); // Unlock the mutex after accessing shared resources
pthread_exit(NULL);
}
int main() {
// Create or get the shared memory segment
int shmid = shmget((key_t)8749, sizeof(int), IPC_CREAT | 0666);
if (shmid == -1) {
perror("Failed!");
return 1;
}
// Attach the shared memory segment
SharedData = (int*)shmat(shmid, NULL, 0);
if (SharedData == (int*)(-1)) {
perror("Failed!");
return 1;
}
cout << "\n\tProcess Requirements:\n";
cout << "Memory: 400MB\n";
cout << "Hard Disk Space: 100MB\n";
cout << "Number of Cores: 1"<< endl;
cout << "Available Memory: " << *SharedData << endl;
if (*SharedData < 400) {
cout << "\tInsufficient resources to create a new process." << endl;
do {
// Keep waiting until sufficient resources are available
} while (*SharedData < 400);
}
cout << "Memory Allocation Done to This Process!" << endl;
// Update available memory after memory allocation
*SharedData -= 400;
// Get the file name from the user
string fileName;
cout << "Enter name of the file: ";
getline(cin, fileName);
// Create a thread to handle file creation and writing
pthread_t thread;
pthread_create(&thread, NULL, createFile, (void*)&fileName);
// Wait for the thread to complete
pthread_join(thread, NULL);
// Update available memory after file operation
*SharedData += 400;
return 0;
}