Professional Documents
Culture Documents
SOC 3100
OPERATING SYSTEMS
CREDITS/HOURS PER WEEK : 3/3
INSTRUCTOR
DR. A. R. NASEER
DEAN & PROFESSOR OF COMPUTER SCIENCE & ENGG
9/25/2019 7:36 PM Lecture Slides on SOC3100 OPERATING SYSTEMS 1
Fall Semester 2019 @ Dr A R Naseer
INHA UNIVERSITY TASHKENT
FALL SEMESTER 2019
SOC 3100
OPERATING SYSTEMS
CLASS SCHEDULE
GROUP1 ICE-17-1 MON 13.30 – 15.00 WED 13.30 – 15.00(203)
GROUP2 CSE-17-1/ICE17-2 MON 15.00 – 16.30 WED 15.00 – 16.30(203)
GROUP3 CSE-17-2/17-3 TUE 09.00 – 10.30 THUR 09.00 – 10.30(203)
GROUP4 CSE-17-4 TUE 10.30 – 12.00 THUR 10.30 – 12.00(203)
WEEK 3
LINUX/UNIX SHELL
PROGRAMMING
echo =============================================================
echo HARD DISK FREE SPACE INFO
free
echo PRESS Enter KEY
read key
echo =============================================================
echo PROCESSES CURRENTLY RUNNING
ps -la
echo PRESS Enter KEY
read key
9/25/2019 7:36 PM Lecture Slides on SOC 3100 OPERATING SYSTEMS 9
Fall Semester 2019 @ Dr A R Naseer
SHELL SCRIPT
echo =============================================================
echo LINUX VERSION
uname -a
echo PRESS Enter KEY
read key
echo =============================================================
echo WHO ARE CURRENTLY LOGGED IN
who -a
echo PRESS Enter KEY
read key
echo =============================================================
echo Do you want directory listing long or short \( enter 1 or 0 \)
read LONG
if [ $LONG -eq 1 ]
then
ls -la
else
ls
fi
echo PRESS Enter KEY
read key
Function_name() {
List of commands
}
The function name must be followed by parentheses, followed by a list of commands
enclosed within braces.
Example :
#!/bin/bash
#Define your function here
Hello() {
echo “Hello World”
}
#Invoke your function
Hello
9/25/2019 7:36 PM Lecture Slides on SOC 3100 OPERATING SYSTEMS 21
Fall Semester 2019 @ Dr A R Naseer
SHELL SCRIPT
❑ Passing Parameters to a Example 2:
#!/bin/bash
Function #Define your function here
You can define a function that Hello() {
will accept parameters while echo “Hello World $1 $2”
return 5
calling the function. These
}
parameters would be #Invoke your function
represented by $1, $2 and so Hello GOOD MORNING
on. #Capture the value returned by last command
rvalue= $?
echo “Return value is $rvalue”
Example 1: Nested Functions
#!/bin/bash One of the more interesting features of functions
is that they can call themselves and also other
#Define your function here functions
Hello() { func1() {
echo “currently in Function 1…”
echo “Hello World $1 $2” echo “Now invoking Function 2…”
func2
} }
#Invoke your function func2(){
echo “Currently in Function 2 ….”
Hello GOOD MORNING }
#calling function
9/25/2019 7:36 PM func1 22
Lecture Slides on SOC 3100 OPERATING SYSTEMS
Fall Semester 2019 @ Dr A R Naseer
SHELL SCRIPT
Using Arrays Example 3:
Example 1: #!/bin/bash
#!/bin/bash echo Enter size of the list :
list=(12 67 123 49 88 123 -9 0 456 126) read N
let I=0 echo Enter a list of $N numbers :
while [ $I -le 9 ] let K=0
do while [ $K -lt $N ]
echo ${list[$I]} do
let I=$I+1 read VAL
done list[$K]=$VAL
Example 2: let K=$K+1
#!/bin/bash done
list=(12 67 123 49 88 123 -9 0 456 126) let I=0
let I=0 max=${list[0]}
max=${list[0]} let I=+$I
let I=+$I while [ $I -lt $N ]
while [ $I -le 9 ] do
do if [ ${list[$I]} -gt $max ]
if [ ${list[$I]} -gt $max ] then
then max=${list[$I]}
max=${list[$I]} fi
fi let I=$I+1
let I=$I+1 done
done echo Maximum element in the list is : $max
9/25/2019 7:36 PM 23
echo Maximum element in the list is : $max Lecture Slides on SOC 3100 OPERATING SYSTEMS
Fall Semester 2019 @ Dr A R Naseer
SOC 3010
OPERATING SYSTEMS
WEEK 3 Lecture
OVERVIEW OF UNIX
FILESYSTEM
unistd.h
unistd.h
HARD & SOFT LINKS
❑ Hard and Soft Links
A filename included in a directory is called a file hard link, or
more simply, a link.
The same file may have several links included in the same directory
or in different ones, so it may have several filenames.
The Unix command:
$ ln p1 p2
is used to create a new hard link that has the pathname p2 for a
file identified by the pathname p1.
Hard links have two limitations:
➢ It is not possible to create hard links for directories.
When this command is executed, the filesystem extracts the directory part of
p2 and creates a new entry in that directory of type symbolic link, with the
name indicated by p2.
This new file contains the name indicated by pathname p1.
This way, each reference to p2 can be translated automatically into a
reference to p1.
➢ Directory
➢ Symbolic link
➢ Socket
The first three file types are constituents of any Unix filesystem.
Device files are related both to I/O devices, and to device drivers
integrated into the kernel.
Pipes and sockets are special files used for interprocess
communication
lseek()
➢ The kernel keeps track of all information about the open file.
➢ The application only keeps track of the descriptor.
➢ Each process created by a Unix shell begins life with three open files:
▪ standard input (descriptor 0),
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(char *filename, int flags, mode_t mode);
Returns: new file descriptor if OK,−1on error
➢ The flags argument indicates how the process intends to access the
file:
❖ O_RDONLY: Reading only
❖ The mode argument specifies the access permission bits of new files.
Opening Files
For example, here is how to open an existing file for reading:
fd = Open("foo.txt", O_RDONLY, 0);
➢ The flags argument can also be or’d with one or more bitmasks that
provide additional instructions for writing:
➢ For example, here is how you might open an existing file with the intent
of appending some data:
➢ There are three types of access rights(access mode) -- read, write, and execute
for each of these three classes. Thus, the set of access rights associated with a
file consists of nine different binary flags
➢ The symbolic names for these bits are shown in Figure.
Opening Files
➢ As part of its context, each process has a umask that is set
by calling the umask function.
➢ When a process creates a new file by calling the open
function with some mode argument, then the access
permission bits of the file are set to mode & ~umask.
➢ For example, suppose we are given the following default values
for mode and umask:
#define DEF_MODE S_IRUSR| S_IWUSR| S_IRGRP| S_IWGRP| S_IROTH| S_IWOTH
#define DEF_UMASK S_IWGRP|S_IWOTH
➢ Then the following code fragment creates a new file in which the
owner of the file has read and write permissions, and all other
users have read permissions:
umask(DEF_UMASK);
fd = Open("foo.txt", O_CREAT|O_TRUNC|O_WRONLY, DEF_MODE);
Reading and writing files
❑ Reading and writing files
➢ A read operation copies n>0 bytes from a file to memory,
starting at the current file position k, and then incrementing k
by n.
➢ Given a file with a size of m bytes, performing a read
operation when k≥m triggers a condition known as end-of-
file(EOF), which can be detected by the application.
➢ There is no explicit “EOF character” at the end of a file.
➢ Similarly, a write operation copies n>0 bytes from memory to
a file, starting at the current file position k, and then updating
k.
– Reading and writing a file read() and write()
Reading and writing files
❑ Reading and writing files
➢ Applications perform input and output by calling the read and
write functions, respectively.
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t n);
Returns: number of bytes read if OK, 0 on EOF ,−1on error
ssize_t write(int fd, const void *buf, size_t n);
Returns: number of bytes written if OK, −1on error
➢ The read function copies at most n bytes from the current file position of
descriptor fd to memory location buf.
➢ A return value of −1 indicates an error, and a return value of 0 indicates
EOF.
➢ Otherwise, the return value indicates the number of bytes that were
actually transferred.
The write function copies at most n bytes from memory location buf to the
current file position of descriptor fd.
Reading Files
Reading a file copies bytes from the current file position to
memory, and then updates file position
char buf[512];
int fd; /* file descriptor */
int nbytes; /* number of bytes read */
char buf[512];
int fd; /* file descriptor */
int nbytes; /* number of bytes read */
int main(void)
{
char c;
while(read(STDIN_FILENO, &c, 1) != 0)
write(STDOUT_FILENO, &c, 1);
exit(0);
}
Changing the Current File Position
❑ Seek - Changing the current file position
➢The kernel maintains a file position k, initially 0, for each open file.
➢The file position is a byte offset from the beginning of a file.
➢An application can set the current file position k explicitly by performing
a seek operation
– Changing the current file position (seek)
indicates next offset into file to read or write
lseek()
res = unlink(pathname);
decreases the file link count and removes the corresponding
directory entry. The file is deleted only when the link count
assumes the value 0.
– Closing a descriptor that is already closed is an error.
SOC 3010 OPERATING SYSTEMS
Reading Assignments
Text Book
STEPS:
$cd linux-source-4.4.0
$sudo apt-get install libncurses5 libncurses5-dev
$make menuconfig
$sudo apt-get update && sudo apt-get install libssl-dev
$make Clean
$make deb-pkg
$cd ..
$ls *.deb
9/25/2019 7:36 PM Lecture Slides on SOC 3100 OPERATING SYSTEMS 55
Fall Semester 2019 @ Dr A R Naseer
Compile & Build Linux Kernal from Source
STEPS:
$sudo reboot