os ashwini
TRANSCRIPT
-
7/31/2019 Os Ashwini
1/27
To study various file management system calls in UNIX .
Write a program to implement
1.Create a file2.Delete a file3.Link a file4.Copy one file to another file5.Read contents of a file in a reverse order.
:
System calls are functions that a programmer can call to perform the services of
the operating system.
: system call to open a file :open returns a file descriptor, an integer
specifying the position of this open file in the table of open files for the current process
.
: system call to close a file
: read data from a file opened for reading
: write data to a file opened for writing
: seek to a specified position in a file (used for random access when
reading or writing a file)
: create a link to a file.
: unlinks a file.
The prototype is
#include
int open(const char *path,int oflag);
-
7/31/2019 Os Ashwini
2/27
The return value is the descriptor of the file
Returns -1 if the file could not be opened.
The first parameter is path name of the file to be opened and the second parameter is
the opening mode specified by bitwise ORing one or more of the following values
O_RDONLY Open for reading only
O_WRONLY Open for writing only
O_RDWR Open for reading and writing
O_APPEND Open at end of file for writing
O_CREAT Create the file if it doesn't already exist
O_EXCLIf set and O_CREAT set will cause open() to fail if the file already
exists
O_TRUNC Truncate file size to zero if it already exists
The close() system call is used to close files. The prototype is
#include
int close(int fildes);
It is always a good practice to close files when not needed as open files do consume
resources and all normal systems impose a limit on the number of files that a process
can hold open.
The read() system call is used to read data from a file or other object identified by a
file descriptor. The prototype is
#include
size_t read(int fildes,void *buf,size_t nbyte);
fildes is the descriptor, bufis the base address of the memory area into which the datais read and nbyte is the maximum amount of data to read.
The return value is the actual amount of data read from the file. The pointer is
incremented by the amount of data read.
An attempt to read beyond the end of a file results in a return value of zero.
http://www.scit.wlv.ac.uk/cgi-bin/mansec?2+closehttp://www.scit.wlv.ac.uk/cgi-bin/mansec?2+readhttp://www.scit.wlv.ac.uk/cgi-bin/mansec?2+readhttp://www.scit.wlv.ac.uk/cgi-bin/mansec?2+close -
7/31/2019 Os Ashwini
3/27
The write() system call is used to write data to a file or other object identified by a file
descriptor. The prototype is
#include
size_t write(int fildes, const void *buf, size_t nbyte);
fildes is the file descriptor, bufis the base address of the area of memory that data is
copied from, nbyte is the amount of data to copy.
The return value is the actual amont of data written.
Whenever a read() or write() operation is performed on a file, the position in the file at
which reading or writing starts is determined by the current value of the
. The value of the read/write pointer is often called the . It is always
measured in bytes from the start of the file. The lseek() system call allows programs to
manipulate this directly so providing the facility for to any part of thefile.
It has three parameters and the prototype is
#include
#include
long lseek(int fildes,off_t offset,int whence)
fildes is the file descriptor, offset the required new value or alteration to the offset and
whencehas one the three values :-
SEEK_SET set pointer to value ofoffset
SEEK_CURset the pointer to its current value plus the value ofoffsetwhich may,
of course, be negative
SEEK_END set the pointer to the size of the file plus the value ofoffset
The system call is used to create a hard link to an existing file. This means
that there will be 2 (or more) directory entries pointing to the same file.
The prototype is
#include
http://www.scit.wlv.ac.uk/cgi-bin/mansec?2+writehttp://www.scit.wlv.ac.uk/cgi-bin/mansec?2+lseekhttp://www.scit.wlv.ac.uk/cgi-bin/mansec?2+linkhttp://www.scit.wlv.ac.uk/cgi-bin/mansec?2+lseekhttp://www.scit.wlv.ac.uk/cgi-bin/mansec?2+write -
7/31/2019 Os Ashwini
4/27
int link(const char *existing, const char *new);
Where existing is the path name of an existing file and new is the name of the link to
associate with the file. Both parameters can be full path names rather than file names.
Existing as well new refer to the same file and so have same permissions ownerships
and inode numbers. On success 0 is returned. On failure 1 is returned.
The unlink() system call removes a directory entry. This will decrement the link count
and, if that goes to zero, will cause the file to be deleted.
The prototype is
#include
int unlink(const char *path);
In order to unlink()a file you require write access to the directory containing the filebecause the directory contents are going to be changed.
Q-1 What is the difference between library procedure and system calls?
Q-2 What is the difference between following commands with respect to system calls.
1)cat filename2)cat < filename3)cat xy >> filename
http://www.scit.wlv.ac.uk/cgi-bin/mansec?2+unlinkhttp://www.scit.wlv.ac.uk/cgi-bin/mansec?2+unlink -
7/31/2019 Os Ashwini
5/27
: Study various Directory management system calls.
Write a program to change current working directory and display the files of
new directory.
:
dirent structure is defined in to provide at least two members
inode number and directory name.
struct dirent
{
ino_t d_ino ; // directory inode number
char d_name[]; // directory name}
UNIX offers a number of system calls to handle a directory.
The following are most commonly used system calls.
Syntax :
DIR * opendir (const char * dirname );
Opendir () takes dirname as the path name and returns a pointer to a
DIR structure. On error returns NULL.
Syntax:
struct dirent * readdir ( DIR *dp) ;
A directory maintains the inode number and filename for every file
in its fold. This function returns a pointer to a dirent structure
consisting of inode number and filename.
Syntax:
int closedir ( DIR * dp);
Closes a directory pointed by dp. It returns 0 on success and -1 on
error.
Syntax :
char * getcwd ( char * buff, size_t size);
This function takes two parameters pointer to a character buffer andsecond is the size of buffer. The function returns the path name
ki di i ifi d b ff
-
7/31/2019 Os Ashwini
6/27
Syntax:
int chdir ( const char * pathname );
Directory can be changed from the parent directory to a new
directory whose path is specified in the character buffer pathname.
-
7/31/2019 Os Ashwini
7/27
To study creation of process and parent child relation.
The entire process life cycle is built around four system calls.
1.execl()2. fork()3.wait()4.exit()
It is used to run a command within a C program.
Syntax :
int execl (path, Arg 0, Arg 1, Arg2, ....,0);
The path argument is the name of the file holding the command we wish torun.
Arg 0 is the name of the command. and Arg1 ,Arg2, Arg3,...are the list of
arguments required for that particular command.
Arg 0 is used to mark the end of the argument list.
Example :
# include < stdio.h>
main()
{
printf( Here comes the data : \n)
execl(/bin/date , date, 0);
printf (Hello);
}
Here execl() overlays the original program with the called program, and the
called program becomes the program of record. In terms of processes, the
summoned command has the same ID that the calling program has before it was
overlaid. In other words, the process remains the same, but the program
instructions in the process are replaced.
fork() is used to create a new process.
The execl () command does not start a new process , it just continues the
original process by overlaying memory with a new set of instructions. As
this occurs a new program is replaced so there is no way to return to the
old program. Some times it is necessary to start a new process , leaving
the old process unrelated. This is done with the fork().
Syntax:
pid_t fork(void);
Fork creates an exact replica of parent ( calling )process. After forkreturns, the parent process and child process now have different PIDs..
A hi i h i h i ll id i l
-
7/31/2019 Os Ashwini
8/27
child process , fork returns with two values:
Zero in the child process.
The PID of the child in the parent process.
Example :
# include main()
{
int pid;
pid = fork();
if ( pid == 0)
{
// this is the child process;
}
else{
// This is the Parent process.
{
}
This function blocks the calling process until one of its childprocesses
exits.
Syntax :
int wait ( int * status);
The function returns the process identification number of the terminated
processes. Further information concerning the terminated process is written
into the location whose address is supplied as the function parameter. One of
the main purposes of is to wait for completion of child processes.
The execution of could have two possible situations.
1. If there are at least one child processes running when the call to is made, the caller will be blocked until one of its child processes exits.
At that moment, the caller resumes its execution.
2. If there is no child process running when the call to wait() is made, thenthis wait() has no effect at all.
The system call waitpid() like wait()suspends the calling process until
one of its children changes state. It is possible to specify to wait for a
child with a specific process id, wait for a child belonging to a specific
process group and wait for any child. It is also possible to specify what
type of child porcess status change is to be waited for.
Th t ll it() i d t t i t th t Th t
-
7/31/2019 Os Ashwini
9/27
1.what is the difference between wait() and waitpid() ?2.What do you mean by zombie state ?3.How can you distinguish between parent and child process.
-
7/31/2019 Os Ashwini
10/27
-
7/31/2019 Os Ashwini
11/27
-
7/31/2019 Os Ashwini
12/27
PRACTICAL 6 :
AIM : Implementing IPC using semaphore.
REFERENCE BOOKS : 'The C Odyssey UNIX' by Vijaymukhi.
THEORY :
: Semaphore is a protected variable that can be accessed and
manipulated by means of only two operations.
1)Wait() :If (s > 0) then decrements the value of s. otherwise the process is blocked
on s.
2)Signal()Increments the value of semaphore. If semaphore queue is not empty , itmoves one of the blocked processes to the ready queue.
Binary Semaphore : can take on only two values 0 and 1
Counting Semaphore : Can take any non-negative integer value.
Use of semaphore :
1)To achieve mutual exclusion2)For synchronization among processes.
syntax :
#include
#include
#include
int semget (key_t key, int nsems, int semflg);
To the variable key we assign a hexadecimal value(0x20). This is the name that
we create the semaphore with.
nsem is the number of sub-semaphores in the semaphore set.semflg can one of the following.
IPC_CREATE | 0666: creates a semaphores with read and alter permission for
each group.
IPC_CREAT | IPC_EXCL | 0666 : creates a semaphore in exclusive mode with
the the given permissions. If semaphore with the specified key already exists,
generates an error.
semget() returns a value which is the identifier of the semaphore . Returns -1
on error.
G i d S i h l
-
7/31/2019 Os Ashwini
13/27
The function semctl performs the control operation specified by cmd on
the semaphore set identified by semid, or on the semnum-th semaphore
of that set.
semid : semaphore identifier
semnum : is the subsemaphore number whose value we want to get ot set. 0value refers to the first sub-semaphore in the set.
cmd can be one of the following.
GETVAL : Returns the value of specified sub-semaphore.
GETALL : Returns the values of all sub-semaphores from a specified
semaphore set.
SETVAL : sets the value of specified sub-semaphore.
GETPID : Returns the pid of the process that has last altered the semaphore
value.
IPC_RMID : Removes the specified sub-semaphore.
GETVAL and SETVAL are not indivisible operations . They do not
guarantees mutual exclusion. So instead of GETVAL and SETVAL we sue
structures and semop() functions, because they give us the indivisibility we
desire.
semop() performs operation on semaphore set.
prototype:
int semop(int semid, struct sembuf *sops, unsigned nsops);
semid : it is the semaphore id returned by a previous semget() call.
sops : sops argument is a pointer to an arrray of structures of type sembuff.
struct sembuf
{
u_short sem_num; /* semaphore number */
short sem_op; /* semaphore operation */
short sem_flg; /* operation flags */
};sem_num is the number associated with a sub semaphore. 0 indicates the first
sub semaphore.
sem_op is a value that defines the operation we want to performed on the
semaphore. Through this value we can define whether we want to capture the
resource or release it.
sem_op member can be passed three kinds of values.
a positive integer increments the semaphore value by that amount.
a negative integer decrements the semaphore value by that amount. An
attempt to set a semaphore to a value less than 0 fails or blocks.(if sem_flg isset to IPC_NOWAIT).
l 0 i f h h l h 0
-
7/31/2019 Os Ashwini
14/27
It defines the step to take , if the semaphore is already in use by another
process.
1)IPC_NOWAIT allows the process to carry on with some other task.
2)SEM_UNDO : Allows individual operations in the array of semaphoreoperations ( sop array) to be undone when the process exists.
nsops : Is the number of operations to be performed on the semaphore.
POST LAB ASSIGNMENTS :
1)What are the requirements of mutual exclusion ?2)Explain hardware approaches to mutual exclusion.
-
7/31/2019 Os Ashwini
15/27
-
7/31/2019 Os Ashwini
16/27
-
7/31/2019 Os Ashwini
17/27
-
7/31/2019 Os Ashwini
18/27
-
7/31/2019 Os Ashwini
19/27
-
7/31/2019 Os Ashwini
20/27
PRACTICAL 10(NO POSTLABS)
AIM : Study of Shell scripting.
REFERENCE : UNIX Shell Programming by Yashwant Kanetkar.
DESCRIPTION :
UNIX shell is an interface between the user and the operating system itself.
The shell also incorporate powerful programming language that enables the user
to exploit the full power and versatility of UNIX. Shell scripts are powerful tool
for invoking and organizing UNIX commands. The shell's more advanced script
can do decision sequences and loops thereby performing almost any
programming task. A script can provide interaction ,interrupts and error
handling and status report just like UNIX commands.
The three widely used UNIX shells are Bourne shell , C shell and Korn shell.All the shells supports processes,pipes,directories and other features of UNIX.
It may happen that the shell scripts written for one shell may not work on
other. This is because the different shells use different mechanism to execute
the commands in the shell script.
Create a file and write all UNIX commands that you want to execute and save
that file with extension .sh.
Example : Create a file script1.sh and type following commands in it.
ls
who
Use the following command to run this script.
Bash$ sh script1.sh
1)Displaying message on the screenecho message
2)reading input in a variableread m1,m2,m3
where m1 m2 and m3 are variables.3)Display value of a variable
echo $m1 $m2 $m3
4)Assigning value to a variablem1=10 m2=20 m3=30
5)Comments in Shell# this is a comment
6)Performing arithmetic operationsBecause all shell variables are string variables. If we want to perform
arithmetic operation on these variables we have to use commanda=20 b=30
h ` $ $b`
-
7/31/2019 Os Ashwini
21/27
7)Control Instructions in shellz
if control command
then# statements
fi
zif control command
then
#statements
else
# statements
fiz
this command translate the result into the language of true and false
if test condition
then
# statements
fi
e.g. if test $num -lt 6
then
echo value is less than 6
fi
-gt : greate than
-lt : less than
-ge : Greater than or equal to
-le : Less than or equal to
-ne : Not equal to
-eq : equal to
z-a (AND)-o (OR)
! (NOT)
zwhile [condition]
do
#statements
done
until [condition]do
#
-
7/31/2019 Os Ashwini
22/27
for counter in *
do
#statements
done
*****************
-
7/31/2019 Os Ashwini
23/27
-
7/31/2019 Os Ashwini
24/27
Operating system with UNIX
LAB ASSIGNMENT 13
Title: Memory allocation algorithm
Aim: study and implement Memory allocation algorithm
1) First fit2) Best-fit3) Worst fit
References:
1. Operating System concepts, Silberschatz, Peterson, Galvin.2. Operating System Internals and Design Principals
Pre-requisite:
Knowledge of memory management algorithm
Description:
In dynamic memory partitioning partitions are of variable number and size Each program is allocated exactly as much memory as it requires. Eventually holes appear in main memory between the partitions
We call this External Fragmentation
One technique for overcoming External fragmentation is compaction, which periodicallyshift programs so, that they are contiguous and all of the free memory is in one block.
Because, memory compaction is time consuming, the operating system designers shouldbe clever in deciding how to assign processes to memory (how to plug holes).
When it is time to load or swap a process into main memory and if there is more than onefree block of main memory of sufficient size, then O.S must decide which free block to
allocate to a program.
Possible algorithms:
-
7/31/2019 Os Ashwini
25/27
Best-fit choose the block that is closest in size to the request. It allocates the smallestblock that is enough.
First-fit begins to scan memory from beginning and chooses the first available block thatis large enough.
Worst-fit scans the entire list and allocates a largest block. The problem with worst fit is it leaves largest leftover
Post Lab Assignment
1. Which algorithm is better? Justify your answer.
-
7/31/2019 Os Ashwini
26/27
Operating system with UNIX
LAB ASSIGNMENT 14
Aim :To implement disk scheduling algorithms
Theory:
I/O request issues a system call to the OS. If desired disk drive or controller is available, requestis served immediately. If busy, new request for service will be placed in the queue of pending
requests. When one request is completed, the OS has to choose which pending request to service
next. In order to satisfy an I/O request the disk controller must first move the head to the correct
track and sector. There is always tradeoff between throughput (the average number of requests
satisfied in unit time) and response time (the average time between a request arriving and it being
satisfied). Various different disk scheduling policies are used:
First-Come-First-Served (FCFS) Disk Scheduling
A disk scheduling strategy which satisfies disk requests in the same order in which they
are received.
The acronym for first-come-first-served disk scheduling is FCFS. Incoming requests are placed in a FIFO queue, and serviced when they get to the front of
the queue.
Response time is minimized. Throughput is not considered. Prevents indefinite postponement (starvation).
Shortest-Seek-Time-First (SSTF) Disk Scheduling
A disk scheduling strategy which satisfies the pending disk request which is physically
closest to the current position of the access arm.
The acronym for shortest-seek-time-first disk scheduling is SSTF. This strategy minimizes seeking. This strategy minimizes mean response time. This strategy does not minimize maximum response time. Allows indefinite postponement (starvation).
SCAN Disk Scheduling
A disk scheduling strategy which satisfies the pending disk request which is physically
closest to the current position of the access arm in the current direction of motion,
reversing direction when the first and last cylinders are reached.
The SCAN strategy is also known as elevator scheduling. Allows indefinite postponement (starvation) in special cases.
C-SCAN Disk Scheduling
A disk scheduling strategy which satisfies the pending disk request which is physically
closest to the current position of the access arm in the direction toward the inside of the
-
7/31/2019 Os Ashwini
27/27
disk, reversing direction and seeking to the outside when it reaches the innermost
cylinder, and repeating.
The acronym for Circular SCAN disk scheduling is C-SCAN. Allows indefinite postponement (starvation) in special cases. postponement (starvation).
C-LOOK Disk Scheduling
A disk scheduling strategy which satisfies the pending disk request which is physically
closest to the current position of the access arm in the direction toward the inside of the
disk, reversing direction and seeking to the outside when it reaches the cylinder of the
innermost request, and repeating.
The acronym for Circular LOOK disk scheduling is C-LOOK. C-LOOK is improvement to SCAN with almost trivial change, but significant
improvement.
Conclusion: