Exp 2,3,4 OS-final
Exp 2,3,4 OS-final
THEORY: -
SHELL SCRIPTS:
Shell scripts are short programs that are written in a shell programming
language and interpreted by a shell process. They are extremely useful for automating
tasks on Linux and other Unix-like operating systems.
A shell is a program that provides the traditional, text-only user interface for
Unix- like operating systems. Its primary function is to read commands (i.e., instructions)
that are typed into a console (i.e., an all-text display mode) or terminal window (i.e.,
all-text mode window) and then execute (i.e., run) them. The default shell on Linux is the
very commonly used and highly versatile bash.
#!/bin/bash
echo “HELLO WORLD”
The process list shows all the processes with various process specific details
in separate columns.Some of the column names are pretty self explanatory.
PID –Process ID
USER - The system user account running the process.
%CPU - CPU usage by the process.
%MEM - Memory usage by the process
COMMAND - The command (executable file) of the process
OUTPUT:
2. Display processes with highest memory usage.
To find the process consuming the most CPU or memory, simply sort the
list.
Press M key (yes, in capital, not small) to sort the process list by memory usage.
Processes using the most memory are shown first and rest in order.
Here are other options to sort by CPU usage, Process ID and Running Time -Press
'P' – to sort the process list by cpu usage. Press 'N' - to sort the list by process
idPress 'T' - to sort by the running time.
OUTPUT:
3. Display current logged in user and logname
R1 R2 R3 R4 R5 Total Signature
AIM:- To study and implement the following system calls: open, read,
write, close, getpid, setpid, getuid, getgid, getegid, geteuid.
THEORY: -
SYSTEM CALL:
When a program in user mode requires access to RAM or a hardware resource, it
must ask the kernel to provide access to that resource. This is done via something called a
system call.
When a program makes a system call, the mode is switched from user mode
to kernel mode. This is called a context switch.
Then the kernel provides the resource which the program requested. After
that, another context switch happens which results in change of mode from kernel mode
back to user mode.
Generally, system calls are made by the user level programs in the following
situations:
Where
fileName : an absolute or relative pathname, mode : a bitwise or’ing of a
read/write flag together with zero or more miscellaneous flags. permission : a number
that encodes the value of the file’s permission flags.
CODE:
try:
file = open('raj_file.txt', 'r')
print(f"File {file.name} opened successfully.")
file.close()
except IOError:
print("An error occurred while trying to open the file.")
OUTPUT:
Description: To read bytes from a file, it uses the “read()” system call.
file_path = "/home/user/Desktop/raj/first.txt"
with open(file_path, 'r') as file:
# Read the content of the file
content = file.read()
print(content)
OUTPUT:
3. Writing to a File: write()
Description: To write bytes to a file, it uses the “write()” system call,
file_path =
"/home/user/Desktop/raj/first.txt"
with open(file_path, 'w') as file:
# Write some content to the file
content = "Hello, world!"
file.write(content)
OUTPUT:
OUTPUT:
import os
# Get the current process ID
current_pid = os.getpid()
print(f"Current Process ID: {current_pid}")
CODE:
import os
CONCLUSION:- Thus we have studied and explored the commands of system calls.
R1 R2 R3 R4 R5 Total Signature
AIM: - To Implement basic commands of Linux like ls, cp, mv and others
using kernel APIs.
THEORY:-
1. Command – stat
Name stat
Purpose To check the status of a file. This provides more detailed information about
a file than ‘ls -l’ output.
Syntax $ stat
usrcopy
Example stat data.txt
OUTPUT:
24 25 26 27 28
31
July
Su
22
23
29
30
OUTPUT:
3. Command – VI editor
Name VI editor
Purpose VI stands for Visual editor; another text editor in Linux. This is a standard
editor in many Linux/Unix environments.
Syntax $ vi
filename
Example $ vi hello.txt
OUTPUT:
4. Command – mv
Name mv - move
Purpose Move files or directories. The 'mv' command works like 'cp' command,
except that the original file is removed. But, the mv command can be
used to rename the files (or directories).
Syntax $ mv source
destination
Example mv myfile.txt
myfiles into the directory myfiles. If myfiles is a file, it
Move the fwill file is marked as read-only, but you own the file,
myfile.txt you overwriting it.
overwritten. If the w
be prompted before
OUTPUT:
5. Command copy
Name cp - copy
Purpose Copy files and directories. If the source is a file, and the destination (file)
name does not exit, then source is copied with new name i.e. with the
name provided as the destination.
Syntax $ cp source
destination
Example $ cp usrlisting
listing_copy.txt
OUTPUT:
6. Command – date
Name date
Purpose Displays current time and date.
If you are interested only in time, you can use 'date +%T' (in hh:mm:ss):
Syntax $
date
Example $
date 6 01:07:09 IST 2012
Fri +%T
Jul $
date
01:13:14
OUTPUT:
7. Command – whoami
Name whoami
Purpose This command reveals the user who is currently logged in.
Syntax $
whoami
Example $
whoami
raghu
OUTPUT:
8. Command – pwd
Name pwd
Purpose ‘pwd’ command prints the absolute path to current working directory.
Syntax $
pwd
Example $
pwd
/home/raghu
OUTPUT:
9. Command – touch
Name touch
Purpose For creating an empty file, use the touch command.
Syntax $ touch
filename
Example $ touch file1 file2
file3
$ ls –l
total 4 4096 2012-07-06 14:09 example
drwxr-xr-x 2 rag 0 2012-07-06 14:20 file1
raghu -rw-r--r-- 1 ragh 0 2012-07-06 14:20 file2
raghu 0 2012-07-06 14:20 file3
-rw-r--r-- 1 raghu
raghu
-rw-r--r-- 1 raghu
raghu
OUTPUT:
10. Command – wc
R1 R2 R3 R4 R5 Total Signature