0% found this document useful (0 votes)
6 views

Operating system Lab

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Operating system Lab

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Assignment 03

Name: Soban Khan

Reg no: UW-22-Ai-BS-052

Semester: BS AI 5th

Course: Operating System

Submitted To: Saba latif


Answer the following questions:

Q1: Write a code in C++ and use All the System Call?
A: Program: -
#include <iostream>
#include <windows.h> // For Windows API functions
#include <fstream> // For file operations
using namespace std;
int main() {
// 1. Create a directory
if (!CreateDirectory("Example_dir", NULL)) {
if (GetLastError() != ERROR_ALREADY_EXISTS) { // Allow if directory exists
cerr << "Error creating directory: " << GetLastError() << endl;
return 1;
}
}
// 2. Open a file in the newly created directory
ofstream file("Example_dir\\Example.txt");
if (!file) {
cerr << "Error opening file." << endl;
return 1;
}
const char* message = "Hi! This is a new file!";
file << message; // Write to the file

file.close(); // Important to always close

// 3. Launch Notepad to open the file

STARTUPINFO si = { sizeof(si) };

PROCESS_INFORMATION pi;

// Notepad will open here of the file

string command = "notepad.exe Example_dir\\Example.txt"; // Note: Command must include the path
to the file

if (!CreateProcess(NULL, const_cast<LPSTR>(command.c_str()), NULL, NULL, FALSE, 0, NULL, NULL, &si,


&pi)) {

cerr << "Error creating process: " << GetLastError() << endl;

return 1;

// Waits for the child process to complete

WaitForSingleObject(pi.hProcess, INFINITE);

// Display parent and child process IDs


cout << "Parent process ID: " << GetCurrentProcessId() << endl;

cout << "Child process ID: " << pi.dwProcessId << endl;

CloseHandle(pi.hProcess);

CloseHandle(pi.hThread);

return 0;

Output: -

Code Explanation:
1- To make the code simpler and easy to understand I used only a few basic system calls I can code easily
and understand because C++ has a few limitations regarding how it performs system calls.
2- The overall code first uses the “CreateDirectory” function to make a new folder named example.dir. If
the folder already exists, the code will still continue without error. (I was unable to make a logic on my own
understanding for the case if the folder already exists. Forgot the logic here a little bit)

3- Then inside the “example.dir” folder, the code makes a “example.txt” text file and writes "Hello! This is
a new file!" to it.
4- Then the code will launch Notepad as a new "child" process using “CreateProcess” function. This lets
Notepad open separately from the output.

5- After this when the user closes notepad after viewing it, the code prints the process IDs of both the
parent process (the main program) and the child process (Notepad).

Q2: Brief with Screen Shot about Network and internet setting .
A: If the user wants to change some specific settings or make any network related changes he or she can
follow the following steps to enter the “Network and Internet” settings panel: -

1- First of all open the overall settings interface by either search or command. The default command to
open settings is “Windows + i” combination on keyboard.

2- From the interface that opens choose “Network and Internet” settings. The section for specific network
settings looks like this in the overall settings interface:-

3- After opening the “Network and Internet” settings the first thing we will be able to see is the current
network we are connected to as well as “Network Status” and the rest of the menus in this interface
settings are the settings we can change or do.

4- After this u are good to go and make any setting changes u want.
5- Some examples of available settings are: -

Status: This shows us an overview of the network connection status, with other options as well to
troubleshoot problems or reset the network settings.
Wi-Fi: This allows us to connect, manage, edit and disconnect from Wi-Fi networks.

Ethernet: This manages the wired (Ethernet) network connections connected to the user’s system.

VPN: This allows us to add, edit, manage, or remove VPN (Virtual Private Network) connections.

Mobile hotspot: This allows you to share your internet with other devices.

6- The rest are up to the user.


Q3: Attempt each and every Question with Clear Screen shot and also write short process
description in your own words:
1. File Creation: -
Use C++ to write a program that creates a file named sample.txt, writes “Hello World!” to
it, and then deletes the file.

A: Program: -

#include <iostream>

#include <fstream> // This is for file operations

#include <string> // This is for basic string handling

#include <windows.h> // For CreateProcess

using namespace std;

int main() {

// 1. Creates a file.

ofstream outFile("sample.txt");

// Check if the file was even created or not

if (!outFile) {

cerr << "Error creating file." << endl;

return 1; // Exits if fails to create the file due to any directory issues.

// 2. This writes "Hello World!" to the file

outFile << "Hello World!" << endl;

outFile.close(); // Close the file after writing the given text

// 3. Open the file for the user to see in the output

// Using CreateProcess to open the file in Notepad

STARTUPINFO si = { sizeof(si) };

PROCESS_INFORMATION pi;

if (!CreateProcess(NULL, (LPSTR)"notepad.exe sample.txt", NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {

cerr << "Error opening Notepad." << endl;


return 1; // Exit if Notepad fails to open

// Wait for Notepad to close

WaitForSingleObject(pi.hProcess, INFINITE);

// Close process handles

CloseHandle(pi.hProcess);

CloseHandle(pi.hThread);

// 4. Read and give output of the contents of the file (optional, since it's already opened in Notepad)

ifstream inFile("sample.txt");

// Checks if the file was opened or not.

if (!inFile) {

cerr << "Error opening file." << endl;

return 1; // Exit if file opening fails

string line;

while (getline(inFile, line)) {

cout << line << endl; // Output the line read from the file

inFile.close(); // Closes the file after reading

return 0;

Output: -
2: Change File Permissions: -
Write a PowerShell script that creates a text file and changes its permissions to make it
read-only.
A: To create a text file and change its permissions to make it a read-only file we can run the following
commands on PowerShell: -

Step-1: -
Open PowerShell by via the search option or use the keyboard command “Windows + X” and then select
PowerShell from the menu that appears.

Step-2: -
Write the following commands and execute them to create a new file that will be named as
“someexample.txt” or any name you want.

You can change the directory to any directory u want in the command. In my case I choose to create the
new file in my user folder hence the command.
(You can also use the basic “mkdir” command in windows cli to make a file as well).

Step-3: -
Now that the file has been created u can set it to “Read-Only” by running the command below on your
PowerShell console.
The command in my case will be “$filePath = "C:\Users\ALR\someexample.txt" >> (Get-Item
$filePath).IsReadOnly = $true” as I specified where my text file I just created is stored.

Step-4: -
Now to verify if the file was created in the given directory and also was changed to “Read-Only”, simply use
windows explorer to navigate to that directory. Right click on the text file with the name
“someexample.txt” and check.

After coming here right click on the file and simply click on properties to view its properties.

As we can see in the text file’s attributes the file is in “Read-only” mode.
3: Fork-like Behavior: -
Simulate a parent-child process using PowerShell by launching Notepad from a parent
script and logging messages from both.
A: To simulate the above asked process we can run the following commands to PowerShell to achieve the
desired output:

Step-1: -
Open PowerShell and give the first command of the directory in while your file will be stored which in
addition will contain the log details as desired. I am using the same directory I used earlier.

Step-2: -
Now we will create a logging function for this file. This will allow us to log messages into the log file
created.

Step-3: -
Now write the command to create a parent log process to log the beginning of the process.

Step-4: -
Now start notepad as a child process. Write the following command for this: -
This will open notepad as well. (Don’t close notepad till the end of script).

Step-5: -
Now we will log the creation of the child process we just made: -

Step-6: -
Now we will make the parent script wait till notepad is closed.

Step-7: -
After closing notepad this line will appear in the PowerShell script writing section: -

Step-8: -
After running all the commands above go the directory u gave in the first command and open the file we
just made and see the logs of the processes.
4: Invoke a System Call:
Use C++ to invoke a system call that retrieves system information (e.g., CPU architecture,
OS version) and prints it.

A: Program: -
#include <iostream>

#include <windows.h>

#include <string>

using namespace std;

int main() {

// This will get the OS Version.

OSVERSIONINFO osvi;

osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);

GetVersionEx(&osvi); // This will retrieve the version information.

cout << "Operating System Version: " << osvi.dwMajorVersion << "." << osvi.dwMinorVersion << endl;

// Get CPU Architecture Information

SYSTEM_INFO si;

GetSystemInfo(&si); // Retrieve systems information

switch (si.wProcessorArchitecture) {

case PROCESSOR_ARCHITECTURE_AMD64:

cout << "CPU Architecture: x64 (AMD or Intel)" << endl;

break;

case PROCESSOR_ARCHITECTURE_INTEL:

cout << "CPU Architecture: x86 (Intel)" << endl;

break;
case PROCESSOR_ARCHITECTURE_ARM:

cout << "CPU Architecture: ARM" << endl;

break;

// This Check if PROCESSOR_ARCHITECTURE_ARM64 is defined (for older versions of dev)

#ifdef PROCESSOR_ARCHITECTURE_ARM64

case PROCESSOR_ARCHITECTURE_ARM64:

cout << "CPU Architecture: ARM64" << endl;

break;

#endif

default:

cout << "CPU Architecture: Unknown" << endl;

break;

return 0;

Output: -

5: Automate Backup:
Write a PowerShell script that creates a backup of a specific folder by copying it to
another location and logs the action.
A: To perform the above given requirement follow the steps given below: -

Step-1: -
Open Powershell and write the commands to enter your directory in which u will be performing these
tasks. I am doing this because I want the backup and the log file to be in the same directory to easily find in
case of use.

Step-2: -
Create a folder directory and name it anything u want. In my case I named it “testfolder” in which I will
store my backup of the text file named “someexample.txt”.

In my case it gave me an error because I already had a directory named “testfolder” which I created before
performing this task but in your case, it won’t give any error and make it for you. (If u haven’t made one
earlier).

Step-3: -
Now write and execute the command to give the location where the backup file will be stored.

Also set the destination and log file path as well: -

Step-4: -
Now create the destination directory for the backup: -
Step-5: -
Now we will copy the file from source to destination: -

Step-6: -
Now we will log the backup action we did: -

Step-7: -
Now we will check if the log entry was added successfully or not: -

You might also like