COMP201 Computer Systems Operating Systems - PowerPoint PPT Presentation

1 / 86
About This Presentation
Title:

COMP201 Computer Systems Operating Systems

Description:

http://www.erlenstar.demon.co.uk/unix/faq_toc.html. Signals ... Each directory contains a list of file names together with a pointer to an index ... – PowerPoint PPT presentation

Number of Views:306
Avg rating:3.0/5.0
Slides: 87
Provided by: scmsWai
Category:

less

Transcript and Presenter's Notes

Title: COMP201 Computer Systems Operating Systems


1
COMP201 Computer SystemsOperating Systems
2
Summary
  • What is an Operating System?
  • Calling the Operating System
  • UNIX System Calls
  • Process Calls

3
Operating Systems
after Peterson Silberschatz. OS Concepts, ISBN
0-201-06097-3
4
Role of the OS
  • According to Englander, the OS is
  • a collection of programs that
  • manage hardware resources
  • manage programs
  • allow the user (and user programs) to access
    those resources.

5
OS Services
from Englander, simplified
6
UNIX OS Roles
  • The UNIX system which we will be looking at in
    more detail has two main roles
  • To provide a simple interface for the user
  • To share the resources of the system between its
    users and processes, this includes
  • fair (or appropriate) sharing of resources
  • protection of jobs from accidental or deliberate
    actions of other jobs.

7
Operating System integrity
  • To support the security of the operating system
    most processor architectures have at least two
    modes of operation
  • user mode
  • kernel mode
  • In user mode the CPU will not execute some
    instructions and can only access memory for a
    single process.

8
System calls
  • In kernel (or supervisor or system or monitor)
    mode only privileged instructions can be carried
    out. User processes can only access User Memory.
    If memory mapped IO is used then the situation
    could be as shown in the diagram.

processes
Accessible in user mode
Accessible only in supervisor mode
9
Calling the OS
  • The user cannot perform IO and other functions
    directly since it is in the fenced off area of
    the primary memory. There are 2 problems with
    calling OS functions
  • How to notify the OS that a function is to be
    called
  • How to notify the OS which function is called and
    pass the parameters

10
System Calls - Software Exceptions
  • Associated with event-driven systems also known
    as a trap.
  • Used by most General purpose operating systems
  • On an exception the OS switches to kernel mode
    and jumps to the exception routine which is in
    the kernel address space.
  • The application actually calls a library stub
    which sets up the parameters and causes the
    software exception.
  • Requires additional mechanism to specify calls
    and pass parameters.
  • Other mechanisms include providing memory
    addresses in kernel space that the user may read
    and call (but not write to). A call to such an
    address causes a switch to kernel mode.

11
Parameter Passing - Registers
  • Registers are available to both User programs and
    the OS kernel
  • Parameters can be written into the registers by
    the library stub.
  • The syscall exception handler reads the
    parameters from the registers and passes them to
    the OS function.

12
Parameter Passing - Memory
  • The OS kernel can read from memory in user space.
  • A register can be used to provide a pointer to
    userspace memory within which parameters are
    stored.
  • Return values are written to the same memory
    area.
  • Used by Windows NT

13
Parameter Passing - Messages
  • In this system the user application builds a
    message and sends it to a mailbox owned by the
    system.
  • This is another form of indirect communication
    between the user application and the OS

14
Reflection time
  • The three methods just described all use a form
    of indirection to communicate from
    User/Application to OS.
  • Why is this?

15
OS Summary
  • An Operating System manages resources and
    programs.
  • User mode is separate from Kernel mode so a
    mechanism is required for user space programs to
    call the OS kernel

16
UNIX System Calls
  • Four main types
  • Process
  • Signal
  • File system
  • Time

17
Process calls
  • A process is a program that is being executed.
  • A program could be executing more than one
    instance at any point in time.
  • A process has the program code but also data,
    runtime stack and various OS facilities (eg
    timers) associated with it.

18
Fork
  • Processes are created using fork() and terminated
    using exit(status).
  • fork creates a duplicate of the process that
    executes it, including the memory, stack, file
    pointers and register values.
  • The only difference between the two processes is
    the value that fork returns.
  • In the new process fork returns 0, in the old
    process fork returns the process ID number (pid)
    of the new process.

19
simple fork program
include ltiostreamgt include ltunistd.hgt int
main(void) if (fork() 0)
coutltlt"Hi, I'm the child process\n" else
coutltlt"Hi, I'm the parent process\n"
coutltltthis is done in both\n"
20
Output of program
  • Hi, I'm the parent process
  • This is done in both
  • Hi, I'm the child process
  • This is done in both

Note that BOTH processes continue their execution
after the end of the if .. else statement testing
fork().
21
Another fork example
include ltunistd.hgt include ltiostreamgt int
main() cout ltlt "x. I am process " ltlt
getpid() ltlt "\n" (void) fork()
cout ltlt "y. I am process " ltlt getpid() ltlt "\n"
f
22
Did we get it?
include ltunistd.hgt include ltiostreamgt int
main() cout ltlt "x. I am process " ltlt
getpid() ltlt "\n" (void) fork()
cout ltlt "y. I am process " ltlt getpid() ltlt "\n"
(void) fork() cout ltlt "z. I am
process " ltlt getpid() ltlt "\n"
23
Solution
f
Pretty clearly you can cause a huge amount of
processor time to be tied up using fork inside a
loop.
f
f
24
Wait
  • Processes created using fork() are terminated
    using exit(status).
  • wait() waits for a child to terminate.
  • It returns the process ID number of the child
    which terminates. The exit code of the child
    that terminates is returned in status.
  • See also man wait

25
fork and wait
include ltiostreamgt include ltunistd.hgt include
ltsys/wait.hgt int main(void) if (fork()
0) cout ltlt "Hi, I'm the child
process\n" exit(10) else
int status wait(status) cout ltlt
"the child returned "ltlt WEXITSTATUS(status)
ltlt "\n"
26
fork and wait output
Hi, I'm the child process the child returned 10
The status returns the exit code of the child
that terminates (its ID number can be returned by
the function also).
27
fork() and execve(name, argv, argp)
  • The execve system call replaces the program code
    in the current process with the program code in
    the file name. Parameters are provided in argv
    and environment variables in argp.
  • If successful, execve never returns.

28
execve
include ltiostreamgt include ltunistd.hgt
include ltsys/wait.hgt int main(void) if
(fork() 0) char av3 "ls",
"-l", 0 char ep2 "COLUMNS75",
0 coutltlt"Hi, I'm the child
process\n" execve("/bin/ls", av, ep)
else int status
wait(status) cout ltlt"the child
returned "ltlt WEXITSTATUS(status) ltlt
"\n"
29
Output
total 200 -rwxr-xr-x 1 Administ None
184842 Aug 21 1511 a.exe -rw-r--r-- 1
Administ None 478 Aug 20 1254
execve.cc -rw-r--r-- 1 Administ None
0 Aug 21 1511 execve.txt -rw-r--r-- 1
Administ None 236 Aug 21 1117
fork1.cc -rw-r--r-- 1 Administ None
96 Aug 21 1127 fork1.txt -rw-r--r-- 1
Administ None 1668 Aug 20 1150
fork2.cc -rw-r--r-- 1 Administ None
1668 Aug 20 1147 fork2.cc.txt -rw-r--r-- 1
Administ None 1812 Aug 20 1157
forkexec.cc -rw-r--r-- 1 Administ None
281 Aug 21 1148 forkthrice.cc -rw-r--r-- 1
Administ None 147 Aug 21 1454
forkthrice.txt -rw-r--r-- 1 Administ None
200 Aug 21 1452 forktwice.cc -rw-r--r-- 1
Administ None 62 Aug 21 1458
forktwice.txt -rw-r--r-- 1 Administ None
326 Aug 20 1245 forkwait.cc -rw-r--r-- 1
Administ None 49 Aug 21 1142
forkwait.txt -rw-r--r-- 1 Administ None
81 Aug 20 1629 main.cc -rw-r--r-- 1
Administ None 81 Aug 20 1628
main.cc.txt -rw-r--r-- 1 Administ None
0 Aug 20 1630 main.i -rw-r--r-- 1 Administ
None 485 Aug 20 1213 simpfork.cc -rw-r--
r-- 1 Administ None 81 Aug 20 1627
simpfork1.cc the child returned 0
30
Other calls
  • The other two process management system calls
    are
  • getpid()
  • returns the process ID number of
  • the process that calls it
  • and brk(addr)
  • allocates more memory to the
  • process and returns the total data
  • segment size.

31
Fork Summary
  • from
  • http//www.cs.uregina.ca/hamilton/courses/330/not
    es/unix/fork/fork.html
  • In UNIX, fork is a system call that creates a new
    process control block.
  • copies most information from the current
    process's pcb into the next free location in the
    process table
  • One can be left/placed in the RUNNING state and
    the other in the READY state.
  • Both will takes turns in using the processor
    (along with all other processes).
  • Which one is chosen to run first, depends on the
    exact version of the operating system.

32
Lab Exercise/More resources
  • Try the code examples given here.
  • Try the exercises at this site
  • http//www.cs.uregina.ca/hamilton/courses/330/not
    es/unix/fork/fork.html
  • Look at the man examples.
  • Look at this site
  • http//www.erlenstar.demon.co.uk/unix/faq_toc.html

33
Signals
  • A signal is an asynchronous (usually) event
    generated by the UNIX operating system.
  • Not a good mechanism for inter-process
    communication.
  • Sometimes will need to deal with them rather than
    use the default handler.

34
SIGINT and SIGTSTP
  • The commonest examples of signals being sent to a
    process is when the user presses
  • ctrl-C SIGINT
  • or
  • ctrl-Z SIGTSTP
  • We might want to intercept these so they dont
    stop a program at a vital time

35
Example SIGINT program
  • include ltiostream.hgt
  • include ltsignal.hgt
  • include ltunistd.h
  • void hitdel(int signo)
  • / called when user hits c /
  • coutltlt"No!\n"
  • main()
  • int z
  • signal(SIGINT, hitdel)
  • / keep busy loop /
  • for( z 1 z lt 30 z)
  • coutltlt z ltlt "\n"

36
Other signals of interest
  • kill(pid, sig)
  • pause()
  • alarm(x)

sends the signal sig to the process pid.
stops a process until it receives a signal.
causes a SIGALRM signal to be sent at least x
seconds after it is called. alarm(0)cancels any
outstanding alarm.
37
Alarm signal example
  • / signal handling routine /
  • void time_out(int signo)
  • cout ltlt "brrrringgg!\n"
  • / reset the alarm again /
  • signal(SIGALRM, time_out)
  • alarm(1)
  • int main(int argc, char argv)
  • / set the handler for ALRM signal /
  • signal(SIGALRM, time_out)
  • alarm(1)
  • cout ltlt "continuing"
  • for (int x 0 x lt 40 x)

38
Signals Summary
  • Signals are asynchronous events sent to processes
    from UNIX
  • See
  • http//users.actcom.co.il/choo/lupg/tutorials/sig
    nals/signals-programming.html
  • http//www.cs.adfa.edu.au/archive/teaching/studinf
    o/csa2/OSLabNotes/node7.html
  • and many more search ltUnix signalsgt
  • gtkill l for a list of signals on your system.

39
Pause
  • Write down any two situations in which you might
    want to intercept a SIGINT signal.

40
File Services
  • Both users and applications need to deal with
    files.
  • Under UNIX, everything is a file.
  • The UNIX system has 26 file system calls that can
    be classified as
  • file
  • directory
  • protection

41
OS Services
USER
command interface (CLI, GUI)
operating system
file services
application
hardware
I/O services
from Englander, simplified
42
Physical storage
Physical file storage can be done in a number of
different ways. UNIX uses an indexed file
allocation method. Each directory contains a
list of file names together with a pointer to an
index block called an i-node.
43
Directories and devices
Unix includes all files in a single structure.
One device, chosen at system boot, is the root
device of the whole file system. If files are
stored on other devices, the root of that device
is mounted onto the existing structure as a
directory
mount (/dev/fd0, /admin, WRITE) // see
also unmount()
44
Core file calls
  • fd open (name, mode)
  • read(fd, buffer, size)
  • write(fd, buffer, size)
  • lseek(fd, pos, from)
  • close(fd)

Before a file can be read it must be opened using
the name (or path name) and the mode (read,
write, read/write etc).
fd is a file descriptor returned by open, this is
used by the other calls.
45
File organisation
  • Often files are opened then read/written
    completely.
  • Random access files can be read using the lseek
    function to position the file pointer.
  • For more see standard C/C texts (eg Kernighan
    Ritchie, The C Programming Language, ISBN
    0-13-110362-8)

46
stdio
  • Functions in the stdio.h file provide buffered
    read and write functions to the unbuffered calls
    described above.
  • These are file descriptors 0 for standard input,
    1 for standard output and 2 for standard error.
  • Normally all three are connected to the terminal
    but they may be connected to other programs or to
    a file.

47
Pipe
  • This system call is passed an array of two file
    descriptors.
  • It opens the first descriptor for reading and the
    second for writing.\ and connects the two
    together.
  • pipe_fdread0 pipe_fdwrite1

48
Pipe example
  • include ltiostreamgt
  • include ltunistd.hgt
  • define BUFFSIZE 7 / allow for cr-lf in
    message /
  • define RDP 0 / first descriptor for
    reading /
  • define WTP 1 / second for writing
    /
  • int main()
  • int status, pid, pipe_fd2
  • char lineBUFFSIZE
  • pipe(pipe_fd) / Create the pipe using
    array of file descriptors /
  • pid fork()
    / create child process /
  • if (pid 0) / in the child
    process, close read end of pipe /
  • close(pipe_fdRDP)
  • cout ltlt "writing...\n" /
    write a message to the pipe /
  • write(pipe_fdWTP, "hello\n", BUFFSIZE)
  • close(pipe_fdWTP)

49
Pipe example
  • http//web.umr.edu/ercal/284/
  • Pipe_exampleA.c Pipe_exampleB.c
  • One program forks and uses execve to run the
    other

50
Dup
  • The dup system call opens the lowest free file
    descriptor and attaches it to the same file as
    its argument.
  • Usually used to connect std input or std output
    to a pipe.
  • The example given above also includes the use of
    dup for this purpose.

51
Dup example
include ltiostreamgt include ltunistd.hgt int
main() int pipe_fd2 pipe(pipe_fd) if
(fork() 0) / create
child process / char av3 "ls",
"-l", 0 / parameters for command
/ close(pipe_fd0) /
close read end of pipe / close(1)
/ close standard
output / dup(pipe_fd1)
/ attach write end of pipe /
/ stdout is
lowest available fd / execve("/bin/ls", av,
0) / run the ls code
/ else /
in the parent process / int files
char ch close(pipe_fd1)
/ close write end of pipe / files 0
/ counter for number
of files / while ( 0 !
read(pipe_fd0, ch, 1) )/ read from pipe until
eof / if (ch '\n')
/ each newline represents a file/
files coutltlt "There are " ltlt files
ltlt " files\n"
52
Other FS system calls
  • There are five other system calls
  • creat(name, mode)
  • mknod(name, mode, type)
  • stat(name, buffer)
  • fstat(fd, buffer)
  • ioctl(fd, operation,argument)

creat makes an empty ordinary file, mknod can
also make files that interface to external
devices. Mode sets the permissions (more
later) stat and fstat return file
information. ioctl provides a mechanism to
control devices for which special files have been
created. Eg set the speed of a serial line.
53
creat and fstat, example
include ltstdio.hgt include ltsys/types.hgt include
ltsys/stat.hgt include ltfcntl.hgt include
ltunistd.hgt void main() int filedes, rc
struct stat buf filedes creat("hello.txt",
S_IRWXG) / create a file / if(
filedes ! -1 ) / if
success then / rc fstat( filedes ,
buf ) / get the stats /
if( rc ! -1 ) printf("File size d,
mode x\n",
buf.st_size, buf.st_mode) close( filedes
)
54
File calls
  • Pause.
  • Jot down a C program fragment that
  • creates a file writes the message hello to
    the file, closes the file
  • opens it and reads back some stats, closes the
    file
  • opens it and reads back some text, closes the
    file.

55
Possible answers - write
/ create the file and write something into it
/ fd creat("hello.txt", 0) if( fd
! -1 ) write(fd, "watcha!\n", 9)
close(fd) cout ltlt "File created\n"

56
Possible answers stats
/ get size stats for the file / fd
open("hello.txt", 0) if( fd ! -1 )
fstat( fd , buf ) cout ltlt "File size "
ltlt buf.st_size ltlt"\n" close(fd)
57
Possible answers - reading
/ read back the data in the file / fd
open("hello.txt", 0) if( fd ! -1 )
read(fd, back, 9) close(fd)
cout ltlt "File contains " ltlt back ltlt "\n"
58
Files Summary
  • File services are a fundamental OS component
  • Under UNIX everything is a file
  • The directory provides structured organisation
    for files
  • Files may be connected and redirected
  • File calls are fundamental for accessing disk
    data.

59
see example programs at http//wand.cs.waikato.a
c.nz/201/2002/lectures/c_source/
60
Directory system calls
  • link(name1, name2)
  • unlink(name)
  • mount(device, name. mode)
  • unmount(device)
  • sync(name)
  • chdir(name)
  • chroot(name)

To understand link/unlink we need to look at the
directory structure again
61
Directory calls
Recall the structure of files and directories
file inode name number
file attributes (eg owner, permisions) links to
data blocks on disc or more indexes
file data
cs201 121 cs412 173 cs645
188 comp-org 121
file data
file data
i-node
directory
It is possible for a file to have more than one
directory entry and therefore more than one name.
62
Hard links
  • Both pointers are entries in a directory file
    pointing to the same i-node.
  • The entries are in one or more directories.
  • The i-node tracks the number of hard links
    pointing to it.
  • If it reaches 0 the file is deleted.
  • Filenames can be different in each directory.

dir 1
dir 2
data block
i-node link count 2
data block
data block
63
Link and unlink
The user is only allowed to create hard links to
files, not directories.
  • Link creates a new directory entry for an
    existing file.
  • Unlink removes a name for a file. If it is the
    only link the file is deleted.

how does unlink know when the file has only one
link?
64
Symbolic links
  • Creates an entry that points to a file containing
    the original files pathname.
  • If the dir 2 link is removed, the symbolic link
    still exists this can cause an error.
  • Unix does not track symbolic links.

dir 1
dir 2
file with pathname
data block
i-node link count 1
data block
data block
note link count
65
Quick demo symbolic links
  • mkdir temp
  • cd temp
  • touch a
  • ln s a b
  • try ls, ls l and ls F commands
  • rm a
  • cat b
  • ls -l

66
Time out
  • i-nodes store the information as to whether the
    file is a directory or not and contain pointers
    to data blocks.
  • Write down any four attributes you could also
    expect to find stored in an i-node.

67
Inside i-nodes
actual block sizes and entries into the i-node
structure and index blocks will be system
dependent.
attributes
pointers to 4k blocks on disk
1
2
3
4
limit 32k
5
7
8
9
index block
10
1
additional pointers to data blocks
11
2
3
4
another 20k
5
pointers to 2 and 3 level index blocks
68
Tree-structured directories
  • Because directories are stored in files a
    directory can contain an entry for another
    directory. The i-node file attributes distinguish
    files and directories.

i-node for root
data block
i-node for file seth
i-nodes for files/directories
i-node for cs201 directory
data block
data block
i-nodes for files/directories
69
Tree structure
A
B
F
C
E
D
Each directory contains links to files and/or to
other directories. There are no cross-links
70
Acyclic-graph directory
  • A tree structure in which there can be links
    between separate branches.

Both E and B point to the same directory. Therefor
e the OS when searching could infinitely cycle
through paths A/C/E or A/B/D/C/E
or A/C/E/D/C/E/D/C/E.
A
C
B
F
D
E
See Englander (course text) p 594.
71
Avoiding link problems
  • Unix only permits superusers to create hard links
    between directories.
  • Unix does not attempt to track symbolic links.

72
Other directory calls
  • chdir(path) This changes the current working
    directory.
  • chroot() This creates a new virtual root
    creating a restricted working environment for a
    user just the files needed by the user can be
    linked in.
  • sync() Unix supports a cache where blocks of data
    that have been used recently are stored. This
    cache must be flushed to disc before shutdown
    this is performed by sync()

73
Protection
  • Each file on a Unix system has an owner and a
    group
  • Normally the owner is the person who created the
    file.
  • All users belong to one or more groups.

74
ls command
The ls command can report the owner and group of
a file
  • richardj_at_warlock/home/201/public_html/2002 ls
    -lg
  • total 410
  • -rw-rw-r-- 1 mpearson 201staff 6703 May 7
    1200 Image10.gif
  • -rw-rw-r-- 1 jpc2 201staff 1339 May 7
    2138 contact.html
  • -rw-rw-r-- 1 jpc2 201staff 4883 Aug 29
    0010 index.html
  • -rw-rw-r-- 1 mpearson 201staff 4372 May 7
    2301 index.tpl
  • drwxrwxr-x 4 mpearson 201staff 2048 Aug 22
    1127 lectures
  • -rw-rw-r-- 1 richardj 201staff 5969 Aug 22
    1132 lectures.html
  • -rw-rw-r-- 1 daa1 201staff 5845 Aug 13
    1620 lectures.html
  • -rw-rw-r-- 1 jpc2 201staff 36123 May 9
    1702 outline.html

permissions owner group
One of these is a directory how do we know?
75
Access permissions
-rw-rw-r-- 1 jpc2 201staff 36123 May 9
1702 outline.html
- r w - r w - r--
r read w write x execute
access permissions
indicates a directory
r w - r w - r--
owners groups other users
76
Protection system calls
  • chmod(name, permissions)
  • uid getuid()
  • gid getgid()
  • setuid(uid)
  • setgid(gid)
  • chown(name, owner, group)
  • oldmask umask(mask)

77
UID and GID
  • The four calls to set and get UID and GID refer
    to the user and group ID numbers.
  • These can only be changed by the superuser.

uid getuid() gid getgid() setuid(uid) setgid(gi
d)
78
Chown(name, owner, group)
  • Used to change ownership, group and permissions.
  • Only the superuser can change the owner of a file
    or set the group to any group.
  • Other users can set the group to a group to which
    they belong.

79
Chmod(name, permissions)
  • Chmod changes the access permissions for a file.
  • The parameter to chmod is treated as a 9-bit
    binary number, each bit corresponding to one of
    the protection modes.
  • thusly

rw-rw-r-- 110110100
(always wanted an excuse to use that word!)
80
umask, Permission mask
  • When a file is created the permissions are
    specified in the creat call.
  • creat (myfile, 777)
  • Some users want to limit the access given to
    other users for all files they create regardless
    of the permission the application attempts to
    set.
  • A limit can be set using umask.

81
umask
  • When a file is created the specified permissions
    are anded with the umask
  • umask (027) 027 111 101 000 rwx r-x ---
  • creat(name,666) 110 110 110 rw- rw- rw-
  • result 110 100 000 rw- r-- ---
  • Notice that the in the call to umask the bits
    specify which permissions are NOT to be granted
    ie it is inverted.

82
Little exercise
  • A user wants to set permissions for all files
    created which will give him/her all permissions,
    give read and execute permissions to the group
    and only execute permissions to all other users.
  • What is the parameter to the umask call?

83
Time calls
  • Unix measures time in seconds since midnight on
    Jan 1, 1970.
  • The time is normally stored in a long int (can
    represent times up to the year 2018).

sec time(0) stime(sec)
times (buffer) utime(file, buffer)
84
Time calls continued
  • time(0) gives the current time.
  • stime(sec) sets the time (only the superuser can
    do this).
  • times(buffer) returns the user and system time
    used by the current process.
  • utime(file, buffer) sets the last modified and
    last accessed times of a file.

85
Pop quiz
  • Describe the difference between a program and a
    process
  • How does Unix distinguish between processes?
  • What does dup do? What is the most common use of
    dup?
  • What does unlink do? what can follow?
  • There are 5 file system calls, what are they?

86
Pops
  • process running program (gt 1)
  • the pid (getpid() function).
  • assigns its argument to the lowest free file
    descriptor, connect stdin or stdout of a process
    to a pipe.
  • decreases the hard link count by 1. the file can
    be deleted if the count drops to 0.
  • creat(n, m), mknod(n, m, t), stat(n, buf),
    fstat(fd, buf), ioctl(fd, op, arg)
Write a Comment
User Comments (0)
About PowerShow.com