0% found this document useful (0 votes)
25 views15 pages

Exp 2,3,4 OS-final

The document outlines experiments aimed at studying and implementing shell scripts, system calls, and basic Linux commands using kernel APIs. It includes detailed explanations of shell scripting, system calls like open, read, write, and various Linux commands such as ls, cp, and mv. The conclusion emphasizes the successful implementation and understanding of these concepts.

Uploaded by

Raj yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views15 pages

Exp 2,3,4 OS-final

The document outlines experiments aimed at studying and implementing shell scripts, system calls, and basic Linux commands using kernel APIs. It includes detailed explanations of shell scripting, system calls like open, read, write, and various Linux commands such as ls, cp, and mv. The conclusion emphasizes the successful implementation and understanding of these concepts.

Uploaded by

Raj yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

EXPERIMENT NO:-2

AIM: - To study and implement the shell scripts.

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.

Steps to write and execute a script:


•​ Open the terminal. Go to the directory where you want to create your script.
•​ Create a file with .sh extension.
•​ Write the script in the file using an editor.
•​ Make the script executable with command chmod +x <fileName.sh>.
•​ Run the script using ./<fileName.sh>.

Example: Print HELLO WORLD using shell script.

#!/bin/bash
echo “HELLO WORLD”

SHELL SCRIPTS EXAMPLE:


1. Display top 10 processes in descending order
The following command will show the list of top processes ordered by RAM
and CPU use in descendant form (remove the pipeline and head if you want to see the full
list)

# ps –eo pid,ppid,cmd,%mem,%cpu –sort=-%mem | head

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

echo "Hi, $USER! Let us be friends."echo


"Hello, $LOGNAME! "
OUTPUT:

4.​ Display OS Version , release number , kernel version


$ uname -a (Print all
Information)
$ uname -r (Print the kernel
name)
$ cat /proc/version
$ cat
/etc/issue
$ cat
/etc/redhat-release
OUTPUT:

CONCLUSION:- Thus we have studied and implemented shell script


programs successfully.

SIGN AND REMARK

R1 R2 R3 R4 R5 Total Signature

(3 Marks) (3 Marks) (3 Marks) (3 Mark) (3 Mark) (15 Marks)


EXPERIMENT NO:-3

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:

●​ Creating, opening, closing and deleting files in the file system.


●​ Creating and managing new processes.
●​ Creating a connection in the network, sending and receiving packets.
●​ Requesting access to a hardware device, like a mouse or a printer.

In a typical UNIX system, there are around 300 system calls.


1.​ Opening a File: open()
Description: “open()” allows you to open or create a file for reading and/or
writing.
Syntax: file = open('file_name.txt', 'r')

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:

2.​ Reading From a File : read()

Description: To read bytes from a file, it uses the “read()” system call.

Syntax: content = file.read()


Here “read()” copies count bytes from the file referenced by the file descriptor fd into the
buffer buf.

#python program to illustrate


# Open a file in read mode
# read.py

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,

Syntax: ssize_t write( int fd, void* buf, size_t count)


Here “write()” copies count bytes from a buffer buf to the file referenced by the file
descriptor fd.
# python program to illustrate
# Open a file in write mode

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)

# Print the content that was written


print(f"Content written to the file:
{content}")

OUTPUT:

4.​ Closing a File: “close()”


Description: uses the “close()” system call to free the file descriptor of the input.

Syntax: int close(int fd)

Here “close()” frees the file descriptor fd.


‡ If fd is the last file descriptor associated with a particular open file, the kernel
resources associated with the file are deallocated.
‡If successful, “close()” returns a value of 0;otherwise, it returns a value
of -1.
// python program to illustrate close
try:
file_path =
"/home/user/Desktop/raj/first.txt"
file = open(file_path, 'w')
file.write("Hello, world!")
except Exception as e:
print(f"An error occurred: {e}")
finally:
try:
file.close()
print("File closed successfully.")
except Exception as e:
print(f"An error occurred while
closing the file: {e}")

OUTPUT:

5.​ Process management - getpid() & getppid()


Description: A process may obtain its own process ID and parent process ID numbers
by using the “getpid()” and “getppid()” system calls, respectively.

Syntax: pid_t getpid(void) pid_t


getppid(void)

Here “getpid()” and “getppid()” return a process’ID number and parent


process’ ID number, respectively.
The parent process ID number of PID 1 (i.e., “init”) is 1.

import os
# Get the current process ID
current_pid = os.getpid()
print(f"Current Process ID: {current_pid}")

# Get the parent process ID


parent_pid = os.getppid()
print(f"Parent Process ID: {parent_pid}")
OUTPUT:

2. Accessing User and Group IDs


Description: The system calls that allow you to read a process real and
effective IDs

Syntax: uid_t getuid()


uid_t geteuid()
gid_t getgid()
gid_t getegid()
Here,
“getuid()” and “geteuid()” return the calling process’ real and effective
user IDs, respectively.
“getgid()” and “getegid()” return the calling process’ real and effective
group IDs, respectively.
The ID numbers correspond to the user and group IDs listed in
“/etc/passwd” and “/etc/group” files.
These calls always succeed.

CODE:​

import os

# Get the current user's ID


user_id = os.getuid()
print(f"User ID: {user_id}")

# Get the current user's effective ID


effective_user_id = os.geteuid()
print(f"Effective User ID: {effective_user_id}")

# Get the current group's ID


group_id = os.getgid()
print(f"Group ID: {group_id}")

# Get the current group's effective ID


effective_group_id = os.getegid()
print(f"Effective Group ID: {effective_group_id}")
OUTPUT:

CONCLUSION:- Thus we have studied and explored the commands of system calls.

SIGN AND REMARK

R1 R2 R3 R4 R5 Total Signature

(3 Marks) (3 Marks) (3 Marks) (3 Mark) (3 Mark) (15 Marks)


EXPERIMENT NO:-4

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:

2.​ Command – cal


Name cal
Purpose Displays the calendar of the current month.
Syntax $
cal
Example $
cal 2012
Mo Tu We Th Fr Sa

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

Name Word count


Purpose wc, or "word count," prints a count of newlines, words, and bytes for each
input file.
Syntax $ wc
filename
Example wc
myfile.txt
5 13 he number of lines, 13 is the number of words, and 57 is the
myfile.txt Waracters.
5 is number o
OUTPUT:

CONCLUSION:- Thus we have studied and implemented basic commands of Linux


like ls, cp, mv and others using kernel APIs.

SIGN AND REMARK

R1 R2 R3 R4 R5 Total Signature

(3 Marks) (3 Marks) (3 Marks) (3 Mark) (3 Mark) (15 Marks)

You might also like