You are on page 1of 6

#include <iostream>

#include<Windows.h>
#include <string>
#include<fstream>
#include <tchar.h>
#include<iomanip>
using namespace std;
#define BUFFER_SIZE 1024
#define COPY_SIZE 512
static ULARGE_INTEGER lastCPU, lastSysCPU, lastUserCPU;
static int numProcessors;
static HANDLE self;
//function for getting the current process in the cpu
double getCurrentValue(){
FILETIME ftime, fsys, fuser;
ULARGE_INTEGER now, sys, user;
double percent;
GetSystemTimeAsFileTime(&ftime);
memcpy(&now, &ftime, sizeof(FILETIME));
GetProcessTimes(self, &ftime, &ftime, &fsys, &fuser);
memcpy(&sys, &fsys, sizeof(FILETIME));
memcpy(&user, &fuser, sizeof(FILETIME));
percent = (sys.QuadPart - lastSysCPU.QuadPart) + (user.QuadPart - lastUserCPU.QuadPart);
percent /= (now.QuadPart - lastCPU.QuadPart);
percent /= numProcessors;
lastCPU = now;
lastUserCPU = user;
lastSysCPU = sys;
return percent * 100;
}

void MyCopyMemory(TCHAR *buf, TCHAR *pbData, SIZE_T cbData, SIZE_T bufsize)


{
CopyMemory(buf, pbData, min(cbData,bufsize));
}

DWORD WINAPI SampleThread(LPVOID iValue)


{
HANDLE hEvent;
int iFinish = 120;
for(int i=100;i<=iFinish;i++)
cout<<i<<endl;
SetEvent(hEvent);
return 0;
}
int main()
{
int x;
int y;
HANDLE MATH;
do
{
// Tasks of the operating system
cout<<"You can do the following\n";
cout<<"=========================\n";
cout<<"1 FIle manipulation \n";
cout<<"2 Process manipulation \n";
cout<<"3 Information maintenace \n";
cout<<"4 Memory management \n";
cout<<"5 CPU management \n";
cout<<"6 log off \n";
cout<<"=========================== \n";
cout<<"what is your choice? \n";
cin>>x;
switch (x)
{
case 1:
{
//the functions you can do with file manupilation
cout<<"You can do the following file manipulation tasks\n";
cout<<"===============================================\n";
cout<<"1 create a file \n";
cout<<"2 Write to a file \n";
cout<<"3 Delete a file \n";
cout<<"=========================== \n";
cout<<"what is your choice? \n";
cin>>y;
switch (y)
{
case 1:
{
//HANDLE MATH;
//code to create a file named julius.txt in the location where the project
exists
LPCWSTR k = TEXT ("Julius.txt");
MATH =
CreateFile(k,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
char cont;

//indicate to the user that the file julius.txt has been created successfully
cout << "The file Julius.txt has been created!!\nPress any key to continue\n";
};break;
case 2:
{
//code to open the file named julius.txt
ofstream myfile;
myfile.open("Julius.txt");
//writting to the open file
myfile << "This statement has been written by the program!!";
//close and save the statement written to the file
myfile.close();
//indicate to the user that the statement was successfully written to the file
cout << "The statement has been written to the file\n\n";
/*BOOL mat;
char dataw[] = "i am awesome";
DWORD dwwhat = (DWORD)strlen(dataw);
DWORD dwBytesWritten = 0;
mat = WriteFile (MATH,dataw,dwwhat,&dwBytesWritten,NULL);*/
};
break;
case 3:
{
//code to delete the file in the specified file location
system("del E:\kimson_project\case_code\case_code\Julius.txt");
/*LPCWSTR m = TEXT("Julius.txt");
DeleteFile (m);*/
};break;
}
};
break;
case 2:
{
//tasks that can be conducted in process manipulation
cout<<"You can do the following Process control tasks\n";
cout<<"===============================================\n";
cout<<"1 Create a process ..run firefox application \n";
cout<<"2 Create a process...run a notepad application \n";
cout<<"=========================== \n";
cout<<"what is your choice? \n";
cin>>y;
switch (y)
{

case 1:
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
BOOL sam;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
//run the firefox application already installed in the machine
system("start firefox.exe");
/*if (!
CreateProcess
(
TEXT("C:\Program Files\Mozilla Firefox\firefox.exe"), NULL, NULL, NULL, FALSE,
CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi)
)
{
cout << "Unable to execute."<<endl;
}*/
//system("pause");
};break;
case 2:
{
//void main()
{
//run the notepad application
system("start notepad.exe");
}
};break;
}
};
break;
case 3:
{
cout<<"You can do the following Information Maintenace tasks\n";
cout<<"======================================================\n";
cout<<"1 Get current process ID in a notepad \n";
cout<<"2 Sleep a process and get the amount of time it has slept \n";
cout<<"========================================================= \n";
cout<<"what is your choice? \n";
cin>>y;
switch (y)
{
case 1:
{
//get the id of the process currently running
DWORD id = GetCurrentProcessId();

char buf[sizeof(DWORD) * 8 + 1];


ultoa(id, buf, 2);
ofstream f("file.txt");
f << id;
};break;
case 2:
{
int i, i2;
//get the time before the machine went to sleep mode
i = clock();
std::cout<<" \nTime before Sleeping : "<<i;
Sleep(3000);
//get time after the sleep mode
i2 = clock();
std::cout<<" \nTime After Sleeping : "<<i2;
//compute the sleep duration by substraction
std::cout<<"\nThe program slept for: "<<i2 -i<<endl<<endl;
getchar();
};break;
}
};
break;
case 4:
{
//task in the memory management
cout<<"You can do the following in Memory management\n";
cout<<"=============================================\n";
cout<<"1 Copy a block of memory \n";
cout<<"========================== \n";
cout<<"what is your choice? \n";
cin>>y;
switch (y)
{
case 1:
{
TCHAR buf[BUFFER_SIZE] = TEXT("This is the destination");
cout <<endl;
TCHAR pbData[BUFFER_SIZE] = TEXT("This is the source");
MyCopyMemory(buf, pbData, COPY_SIZE*sizeof(TCHAR), BUFFER_SIZE*sizeof(TCHAR));
_tprintf(TEXT("Destination buffer contents: %s\n"), buf);
system("pause");
};break;
}
};break;

case 5:
{
//tasks in CPU management
cout<<"You can do the following under CPU management\n";
cout<<"=============================================\n";
cout<<"1 Get the processes currently in the CPU \n";
cout<<"2 Get total virtual memory \n";
cout<<"========================== \n";
cout<<"what is your choice? \n";
cin>>y;
switch (y)
{
case 1:
{
//get and display all processes and even background processes
system("tasklist");
char a;
cin >> a;
};break;
case 2:
{
//total virtual memory
MEMORYSTATUSEX memInfo;
memInfo.dwLength = sizeof(MEMORYSTATUSEX);
GlobalMemoryStatusEx(&memInfo);
DWORDLONG totalVirtualMem = memInfo.ullTotalPageFile;
};break;
}

};break;
case 6:
{
//log out of the system
ExitWindows(0,0);
};break;
}
}while(x!=5);
return 0;
}

You might also like