os tute(6163)l9
TRANSCRIPT
-
8/10/2019 OS Tute(6163)L9
1/45
BITSPilaniPilani Campus
BITSPilaniPilani Campus
OS Tutorial
-
8/10/2019 OS Tute(6163)L9
2/45
BITSPilaniPilani Campus
S. H. [email protected]
Room No. 6120 K (NAB)
mailto:[email protected]:[email protected]:[email protected]:[email protected]:[email protected] -
8/10/2019 OS Tute(6163)L9
3/45
BITS Pilani, Pilani Campus
Interprocess communication (IPC)
3
-
8/10/2019 OS Tute(6163)L9
4/45
BITS Pilani, Pilani Campus
Syntax
int dup (int oldfd)
dup() finds the smallest free file descriptorentry and points it to the same file as oldfd.
dup() system call
4
-
8/10/2019 OS Tute(6163)L9
5/45
BITS Pilani, Pilani Campus
Syntax
int dup2(int oldfd, int newfd )
An existing file descriptor oldfdis duplicated as file
descriptor newfd. If the file corresponding to descriptor newfd is open,
then it is closed.
In both cases, the original and copied file descriptorsshare the same file pointer and access mode.
dup2() system call
5
-
8/10/2019 OS Tute(6163)L9
6/45
BITS Pilani, Pilani Campus
They both return the index of the new filedescriptor if successful, and 1 otherwise.
dup/dup2 duplicates an existing file
descriptor, giving a new file descriptor that isopen to the same file or pipe.
The call fails if the argument is bad (not open)
or if 20 file descriptors are already open.
6
dup/dup2 system call
-
8/10/2019 OS Tute(6163)L9
7/45BITS Pilani, Pilani Campus
By this call, we have the close operation, andthe actual descriptor duplication, wrapped upin one system call.
It is guaranteed to be atomic, which essentiallymeans that it will never be interrupted by anarriving signal. The entire operation will
transpire before returning control to thekernel for signal dispatching.
dup2() system call
7
-
8/10/2019 OS Tute(6163)L9
8/45BITS Pilani, Pilani Campus
With dup() system call, programmers had toperform a close() operation before calling it,i.e., resulted in two system calls, with a small
degree of vulnerability in the brief amount oftime which elapsed between them.
If a signal arrived during that brief instance,
the descriptor duplication would fail. Of course, dup2() solves this problem for us.
dup2() system call
8
-
8/10/2019 OS Tute(6163)L9
9/45BITS Pilani, Pilani Campus
In computing, redirection is a functioncommon to most command-line interpreters,including the various Unix shells that can
redirect standard streams to user-specifiedlocations.
Redirection
9
-
8/10/2019 OS Tute(6163)L9
10/45BITS Pilani, Pilani Campus
Output Redirection: The output from a commandnormally intended for standard output can be easilydiverted to a file instead. This capability is known asoutput redirection:
date > today
It writes its output to the file today instead of terminal.
No output appears at the terminal.
Because the output has been redirected from thedefault standard output device (the terminal) into thespecified file today.
Redirection
10
-
8/10/2019 OS Tute(6163)L9
11/45BITS Pilani, Pilani Campus
Input Redirection: The input of a command beredirected from a file.
The commands that normally take their input from
standard input can have their input redirected from afile.
Redirection
11
-
8/10/2019 OS Tute(6163)L9
12/45BITS Pilani, Pilani Campus
To count the number of lines in the file uu we canexecute : (It produces output 2 lines)
$wc-luu
2 uu
We can count the number of lines in the file byredirecting the standard input ofwc from the file uu:
$ w c - l < u u
2
Redirection
12
-
8/10/2019 OS Tute(6163)L9
13/45BITS Pilani, Pilani Campus
In the first case, uu (file name) is listed with theline count; in the second case, it is not.
In the first case, wc knows that it is reading its
input from uu. In the second case, it only knows that it is reading
its input from standard input so it does not
display file name.
Redirection
13
-
8/10/2019 OS Tute(6163)L9
14/45BITS Pilani, Pilani Campus
Sort < infile > outfile sort reads from infile and writes to outfile.
date >> today
Append the output at the end of file today.
Redirection
14
-
8/10/2019 OS Tute(6163)L9
15/45BITS Pilani, Pilani Campus
The UNIX shell uses following information toimplement redirection.
When a process forks, the child inherits a copy of its
parents file descriptors. When process execs, the standard input, output, and
error channels remain unaffected.
15
Implementation of Redirection
-
8/10/2019 OS Tute(6163)L9
16/45BITS Pilani, Pilani Campus
To perform redirection, the shell performs thefollowing series of actions:
The parent shell forks and then waits for the child shell
to terminate. The child shell opens the file test.txt, creating it or
truncating as necessary.
The child shell then duplicates the file descriptor of
test.txt to the standard output file descriptor,number 1, and then closes the original descriptor oftest.txt.
All standard output is therefore redirected to test.txt.
Implementation of Redirection
16
-
8/10/2019 OS Tute(6163)L9
17/45
-
8/10/2019 OS Tute(6163)L9
18/45BITS Pilani, Pilani Campus
#include #include #include void main (argc, argv)int argc;
char *argv[] ;{int fd ; /* file descriptor or pointer */fd = open (argv[1], O_CREAT | O_TRUNC | O_RDWR, 0777) ;
/* open test.txt in argv[1] */dup2 (fd, 1) ; /* and assign it to fd */close (fd) ; /* duplicate fd with 1 which is standard output */execvp (argv[2], &argv[2]) ;
/* the output is not printed on screen but is redirected totest.txt file */
printf ("End\n") ; /* should never execute */}
18
-
8/10/2019 OS Tute(6163)L9
19/45BITS Pilani, Pilani Campus
19
execvp()
#include #include main (int argc, char *argv[] ){execvp (ls", /* program to load - PATH searched */&argv[0] ) ;printf ("EXEC Failed\n") ;/* This above line will be printed only on error and not otherwise */
}
-
8/10/2019 OS Tute(6163)L9
20/45BITS Pilani, Pilani Campus
20
-
8/10/2019 OS Tute(6163)L9
21/45
-
8/10/2019 OS Tute(6163)L9
22/45BITS Pilani, Pilani Campus
To implement "ls|wc'' the shell will havecreated a pipe and then forked.
The parent will exec to be replaced by "ls'', and
the child will exec to be replaced by "wc.
The write end of the pipe may be descriptor 3and the read end may be descriptor 4.
"ls'' normally writes to descriptor 1 and "wc''normally reads from descriptor 0.
Example
22
-
8/10/2019 OS Tute(6163)L9
23/45
-
8/10/2019 OS Tute(6163)L9
24/45
BITS Pilani, Pilani Campus24
execlp()
#include #include void main ( ){
execlp ("ls", /* program to run - PATH Searched */"ls", /* name of program sent to argv[0] */"-l", /* first parameter (argv[1])*/"-a", /* second parameter (argv[2]) */NULL) ; /* terminate arg list */printf ("EXEC Failed\n") ;/* This above line will be printed only on error and not otherwise */
}
$ ls l a$ ls a Displays all files.$ls l Displays the long format listing.
-
8/10/2019 OS Tute(6163)L9
25/45
BITS Pilani, Pilani Campus
http://www.cs.princeton.edu/courses/archive/spr02/cs217/lectures/communication.pdf
http://www.cs.cf.ac.uk/Dave/C/node23.html
http://www.tldp.org/LDP/lpg/node10.html
http://ocamlunix.forge.ocamlcore.org/pipes.html http://beej.us/guide/bgipc/output/html/multipage/pipes.ht
ml
http://cm.bell-labs.com/who/dmr/ipcpaper.html
http://academic.udayton.edu/SaverioPerugini/courses/cps346/lecture_notes/pipes.html
http://cboard.cprogramming.com/c-programming/112536-implement-redirection-simple-shell.html
References
25
http://www.cs.princeton.edu/courses/archive/spr02/cs217/lectures/communication.pdfhttp://www.cs.princeton.edu/courses/archive/spr02/cs217/lectures/communication.pdfhttp://www.cs.cf.ac.uk/Dave/C/node23.htmlhttp://www.tldp.org/LDP/lpg/node10.htmlhttp://ocamlunix.forge.ocamlcore.org/pipes.htmlhttp://beej.us/guide/bgipc/output/html/multipage/pipes.htmlhttp://beej.us/guide/bgipc/output/html/multipage/pipes.htmlhttp://cm.bell-labs.com/who/dmr/ipcpaper.htmlhttp://academic.udayton.edu/SaverioPerugini/courses/cps346/lecture_notes/pipes.htmlhttp://academic.udayton.edu/SaverioPerugini/courses/cps346/lecture_notes/pipes.htmlhttp://cboard.cprogramming.com/c-programming/112536-implement-redirection-simple-shell.htmlhttp://cboard.cprogramming.com/c-programming/112536-implement-redirection-simple-shell.htmlhttp://cboard.cprogramming.com/c-programming/112536-implement-redirection-simple-shell.htmlhttp://cboard.cprogramming.com/c-programming/112536-implement-redirection-simple-shell.htmlhttp://cboard.cprogramming.com/c-programming/112536-implement-redirection-simple-shell.htmlhttp://cboard.cprogramming.com/c-programming/112536-implement-redirection-simple-shell.htmlhttp://cboard.cprogramming.com/c-programming/112536-implement-redirection-simple-shell.htmlhttp://cboard.cprogramming.com/c-programming/112536-implement-redirection-simple-shell.htmlhttp://cboard.cprogramming.com/c-programming/112536-implement-redirection-simple-shell.htmlhttp://cboard.cprogramming.com/c-programming/112536-implement-redirection-simple-shell.htmlhttp://cboard.cprogramming.com/c-programming/112536-implement-redirection-simple-shell.htmlhttp://cboard.cprogramming.com/c-programming/112536-implement-redirection-simple-shell.htmlhttp://cboard.cprogramming.com/c-programming/112536-implement-redirection-simple-shell.htmlhttp://cboard.cprogramming.com/c-programming/112536-implement-redirection-simple-shell.htmlhttp://cboard.cprogramming.com/c-programming/112536-implement-redirection-simple-shell.htmlhttp://cboard.cprogramming.com/c-programming/112536-implement-redirection-simple-shell.htmlhttp://cboard.cprogramming.com/c-programming/112536-implement-redirection-simple-shell.htmlhttp://cboard.cprogramming.com/c-programming/112536-implement-redirection-simple-shell.htmlhttp://cboard.cprogramming.com/c-programming/112536-implement-redirection-simple-shell.htmlhttp://cboard.cprogramming.com/c-programming/112536-implement-redirection-simple-shell.htmlhttp://cboard.cprogramming.com/c-programming/112536-implement-redirection-simple-shell.htmlhttp://cboard.cprogramming.com/c-programming/112536-implement-redirection-simple-shell.htmlhttp://academic.udayton.edu/SaverioPerugini/courses/cps346/lecture_notes/pipes.htmlhttp://academic.udayton.edu/SaverioPerugini/courses/cps346/lecture_notes/pipes.htmlhttp://academic.udayton.edu/SaverioPerugini/courses/cps346/lecture_notes/pipes.htmlhttp://academic.udayton.edu/SaverioPerugini/courses/cps346/lecture_notes/pipes.htmlhttp://academic.udayton.edu/SaverioPerugini/courses/cps346/lecture_notes/pipes.htmlhttp://academic.udayton.edu/SaverioPerugini/courses/cps346/lecture_notes/pipes.htmlhttp://academic.udayton.edu/SaverioPerugini/courses/cps346/lecture_notes/pipes.htmlhttp://academic.udayton.edu/SaverioPerugini/courses/cps346/lecture_notes/pipes.htmlhttp://academic.udayton.edu/SaverioPerugini/courses/cps346/lecture_notes/pipes.htmlhttp://academic.udayton.edu/SaverioPerugini/courses/cps346/lecture_notes/pipes.htmlhttp://academic.udayton.edu/SaverioPerugini/courses/cps346/lecture_notes/pipes.htmlhttp://academic.udayton.edu/SaverioPerugini/courses/cps346/lecture_notes/pipes.htmlhttp://cm.bell-labs.com/who/dmr/ipcpaper.htmlhttp://cm.bell-labs.com/who/dmr/ipcpaper.htmlhttp://cm.bell-labs.com/who/dmr/ipcpaper.htmlhttp://cm.bell-labs.com/who/dmr/ipcpaper.htmlhttp://cm.bell-labs.com/who/dmr/ipcpaper.htmlhttp://cm.bell-labs.com/who/dmr/ipcpaper.htmlhttp://cm.bell-labs.com/who/dmr/ipcpaper.htmlhttp://cm.bell-labs.com/who/dmr/ipcpaper.htmlhttp://cm.bell-labs.com/who/dmr/ipcpaper.htmlhttp://cm.bell-labs.com/who/dmr/ipcpaper.htmlhttp://cm.bell-labs.com/who/dmr/ipcpaper.htmlhttp://beej.us/guide/bgipc/output/html/multipage/pipes.htmlhttp://beej.us/guide/bgipc/output/html/multipage/pipes.htmlhttp://beej.us/guide/bgipc/output/html/multipage/pipes.htmlhttp://beej.us/guide/bgipc/output/html/multipage/pipes.htmlhttp://beej.us/guide/bgipc/output/html/multipage/pipes.htmlhttp://beej.us/guide/bgipc/output/html/multipage/pipes.htmlhttp://beej.us/guide/bgipc/output/html/multipage/pipes.htmlhttp://beej.us/guide/bgipc/output/html/multipage/pipes.htmlhttp://ocamlunix.forge.ocamlcore.org/pipes.htmlhttp://ocamlunix.forge.ocamlcore.org/pipes.htmlhttp://ocamlunix.forge.ocamlcore.org/pipes.htmlhttp://ocamlunix.forge.ocamlcore.org/pipes.htmlhttp://ocamlunix.forge.ocamlcore.org/pipes.htmlhttp://ocamlunix.forge.ocamlcore.org/pipes.htmlhttp://ocamlunix.forge.ocamlcore.org/pipes.htmlhttp://ocamlunix.forge.ocamlcore.org/pipes.htmlhttp://ocamlunix.forge.ocamlcore.org/pipes.htmlhttp://ocamlunix.forge.ocamlcore.org/pipes.htmlhttp://ocamlunix.forge.ocamlcore.org/pipes.htmlhttp://www.tldp.org/LDP/lpg/node10.htmlhttp://www.tldp.org/LDP/lpg/node10.htmlhttp://www.tldp.org/LDP/lpg/node10.htmlhttp://www.tldp.org/LDP/lpg/node10.htmlhttp://www.tldp.org/LDP/lpg/node10.htmlhttp://www.tldp.org/LDP/lpg/node10.htmlhttp://www.tldp.org/LDP/lpg/node10.htmlhttp://www.tldp.org/LDP/lpg/node10.htmlhttp://www.tldp.org/LDP/lpg/node10.htmlhttp://www.tldp.org/LDP/lpg/node10.htmlhttp://www.cs.cf.ac.uk/Dave/C/node23.htmlhttp://www.cs.cf.ac.uk/Dave/C/node23.htmlhttp://www.cs.cf.ac.uk/Dave/C/node23.htmlhttp://www.cs.cf.ac.uk/Dave/C/node23.htmlhttp://www.cs.cf.ac.uk/Dave/C/node23.htmlhttp://www.cs.cf.ac.uk/Dave/C/node23.htmlhttp://www.cs.cf.ac.uk/Dave/C/node23.htmlhttp://www.cs.cf.ac.uk/Dave/C/node23.htmlhttp://www.cs.cf.ac.uk/Dave/C/node23.htmlhttp://www.cs.cf.ac.uk/Dave/C/node23.htmlhttp://www.cs.cf.ac.uk/Dave/C/node23.htmlhttp://www.cs.cf.ac.uk/Dave/C/node23.htmlhttp://www.cs.cf.ac.uk/Dave/C/node23.htmlhttp://www.cs.cf.ac.uk/Dave/C/node23.htmlhttp://www.cs.princeton.edu/courses/archive/spr02/cs217/lectures/communication.pdfhttp://www.cs.princeton.edu/courses/archive/spr02/cs217/lectures/communication.pdfhttp://www.cs.princeton.edu/courses/archive/spr02/cs217/lectures/communication.pdfhttp://www.cs.princeton.edu/courses/archive/spr02/cs217/lectures/communication.pdfhttp://www.cs.princeton.edu/courses/archive/spr02/cs217/lectures/communication.pdfhttp://www.cs.princeton.edu/courses/archive/spr02/cs217/lectures/communication.pdfhttp://www.cs.princeton.edu/courses/archive/spr02/cs217/lectures/communication.pdfhttp://www.cs.princeton.edu/courses/archive/spr02/cs217/lectures/communication.pdfhttp://www.cs.princeton.edu/courses/archive/spr02/cs217/lectures/communication.pdfhttp://www.cs.princeton.edu/courses/archive/spr02/cs217/lectures/communication.pdfhttp://www.cs.princeton.edu/courses/archive/spr02/cs217/lectures/communication.pdfhttp://www.cs.princeton.edu/courses/archive/spr02/cs217/lectures/communication.pdfhttp://www.cs.princeton.edu/courses/archive/spr02/cs217/lectures/communication.pdfhttp://www.cs.princeton.edu/courses/archive/spr02/cs217/lectures/communication.pdfhttp://www.cs.princeton.edu/courses/archive/spr02/cs217/lectures/communication.pdfhttp://www.cs.princeton.edu/courses/archive/spr02/cs217/lectures/communication.pdf -
8/10/2019 OS Tute(6163)L9
26/45
BITS Pilani, Pilani Campus
Interprocess communication (IPC)(shared memory)
26
-
8/10/2019 OS Tute(6163)L9
27/45
BITS Pilani, Pilani Campus
How the processes can communicate amongthemselves using the Shared Memory.
Creating a Shared Memory Segment.
Controlling a Shared Memory Segment. Attaching and Detaching a Shared Memory Segment.
Objectives
27
-
8/10/2019 OS Tute(6163)L9
28/45
BITS Pilani, Pilani Campus
Shared Memory is an efficient means of passing databetween programs.
One program will create a memory portion, which other
processes (if permitted) can access.
A shared segment can be attached multiple times by the
same process.
A shared memory segment is described by a control
structure with a unique ID that points to an area ofphysical memory.
Shared Memory
28
-
8/10/2019 OS Tute(6163)L9
29/45
BITS Pilani, Pilani Campus
Shared Memory
29
-
8/10/2019 OS Tute(6163)L9
30/45
BITS Pilani, Pilani Campus
A shared memory is an extra piece of memory that isattached to some address spaces for their owners to
use.
As a result, all of these processes share the same
memory segment and have access to it.
Shared Memory
30
-
8/10/2019 OS Tute(6163)L9
31/45
BITS Pilani, Pilani Campus
Shared memory is a feature supported by UNIX SystemV, including Linux, SunOS and Solaris.
One process must explicitly ask for an area, using a key,
to be shared by other processes. This process will be
called the server. All other processes, the clients that
know the shared area can access it.
Shared Memory
31
-
8/10/2019 OS Tute(6163)L9
32/45
BITS Pilani, Pilani Campus
However, there is no protection to a shared memoryand any process that knows it can access it freely.
To protect a shared memory from being accessed at the
same time by several processes, a synchronization
protocol must be setup.
Shared Memory
32
-
8/10/2019 OS Tute(6163)L9
33/45
BITS Pilani, Pilani Campus
The shared memory itself is described by a structure oftype shmid_ds in header file sys/shm.h.
To use this file, following files must be included:
Shared Memory
33
-
8/10/2019 OS Tute(6163)L9
34/45
BITS Pilani, Pilani Campus
For a server, it should be started before any client.
The server should perform the following tasks:
Ask for a shared memory with a memory key and memorize
the returned shared memory ID. This is performed by system
call shmget().
Attach this shared memory to the server's address space with
system call shmat().
Initialize the shared memory, if necessary.
Do something and wait for all clients' completion.
Detach the shared memory with system call shmdt().
Remove the shared memory with system call shmctl().
Shared Memory
34
-
8/10/2019 OS Tute(6163)L9
35/45
BITS Pilani, Pilani Campus
For the client part, the procedure is almost the same: Ask for a shared memory with the same memory key and
memorize the returned shared memory ID.
Attach this shared memory to the client's address space.
Use the memory.
Detach all shared memory segments, if necessary.
Exit.
Shared Memory
35
-
8/10/2019 OS Tute(6163)L9
36/45
BITS Pilani, Pilani Campus
This system call requests a shared memory segment.
It is defined as follows:
shm_id = shmget (
key_t k, /* the key for the segment */int size, /* the size of the segment */
int flag /* create/use flag */
); If it is successful, it returns a non-negative integer, the
shared memory ID; otherwise, the function value is
negative.
shmget()
36
-
8/10/2019 OS Tute(6163)L9
37/45
BITS Pilani, Pilani Campus
k is of type key_t or IPC_PRIVATE. It is the numeric keyto be assigned to the returned shared memory segment.
size is the size of the requested shared memory.
flag is used to specify the way that the shared memorywill be used.
IPC_CREAT | 0666 for a server (i.e., creating and granting
read and write access to the server).
0666 for any client (i.e., granting read and write access tothe client)
shmget()
37
-
8/10/2019 OS Tute(6163)L9
38/45
BITS Pilani, Pilani Campus
shmget()
38
-
8/10/2019 OS Tute(6163)L9
39/45
BITS Pilani, Pilani Campus
UNIX requires a key of type key_t defined in filesys/types.h for requesting shared memory segments.
There are three different ways of using keys, namely:
A specific integer value (e.g., 123456)
A key generated with function ftok()
A uniquely generated key using IPC_PRIVATE (a private key).
key
39
-
8/10/2019 OS Tute(6163)L9
40/45
BITS Pilani, Pilani Campus
The first way is the easiest one; however, its use may bevery risky since a process can access your resource as
long as it uses the same key value to request that
resource.
The following example assigns 1234 to a key:
key_t SomeKey;
SomeKey = 1234;
The ftok() function has the following prototype:
key
40
-
8/10/2019 OS Tute(6163)L9
41/45
BITS Pilani, Pilani Campus
ftok() takes a character string that identifies a path andan integer (usually a character) value, and generates an
integer of type key_t based on the first argument with
the value of id in the most significant position.
Example: if the generated integer is 35028A5D16 and
the value of id is 'a' (ASCII value = 61), then ftok()
returns 61028A5D16. That is, 61 replaces the first byte
of 35028A5D16,generating 61028A5D16.
key
41
-
8/10/2019 OS Tute(6163)L9
42/45
BITS Pilani, Pilani Campus
If a processes use the same arguments to call ftok(), thereturned key value will always be the same.
The most commonly used value for the first argument is
".", the current directory.
If all related processes are stored in the same directory,
the following call to ftok() will generate the same key
value.
key
42
-
8/10/2019 OS Tute(6163)L9
43/45
-
8/10/2019 OS Tute(6163)L9
44/45
-
8/10/2019 OS Tute(6163)L9
45/45
If a resource is requested with IPC_PRIVATE in a placewhere a key is required, that process will receive a
unique key for that resource.
Since that resource is identified with a unique key unknown to
the outsiders, other processes will not be able to share that
resource and, as a result, the requesting process is guaranteed
that it owns and accesses that resource exclusively.
key
45