-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateFile.cpp
More file actions
77 lines (58 loc) · 1.7 KB
/
CreateFile.cpp
File metadata and controls
77 lines (58 loc) · 1.7 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
#include <iostream>
#include <fstream>
#include <pthread.h>
#include <sys/ipc.h>
#include <sys/shm.h>
using namespace std;
int *SharedData;
void* createFile(void* filename) {
string fname = *((string*)filename);
// Create a new file
ofstream file(fname);
file.close();
ifstream checkFile(fname);
if (checkFile) {
cout << "File \"" << fname << "\" created successfully!" << endl;
checkFile.close();
} else {
cout << "Failed to create file \"" << fname << "\"!" << endl;
}
pthread_exit(NULL);
}
int main() {
int shmid = shmget((key_t)8749, sizeof(SharedData),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 << "\nProcess Requirements:\n";
cout << "Memory: 800MB\n";
cout << "Hard Disk Space: 300MB\n";
cout << "Number of Cores: 1"<< endl;
cout<<"Available Memory: "<<*SharedData<<endl;
if(*SharedData<800)
{
cout << "\tInsufficient resources to create a new process." << endl;
do{
}while(*SharedData<800);
}
cout<<"Memory Allocation Done to This Process!\n";
*SharedData-=800;
string fileName;
pthread_t thread;
cout << "Enter name of the new file: ";
getline(cin, fileName);
// Create a thread to create the file
pthread_create(&thread, NULL, createFile, (void*)&fileName);
// Wait for the thread to complete
pthread_join(thread, NULL);
*SharedData+=800;
return 0;
}