COMP 2007: UNIX Operating System

Download Report

Transcript COMP 2007: UNIX Operating System

Operating system services
•
•
•
•
•
•
•
•
Program execution
I/O operations
File-system manipulation
Communications
Error detection
Resource allocation
Accounting
Protection
System Calls
• Provide an interface between a process and the
operating system. Generally available as
assembly-language instructions
• UNIX system calls maybe invoked directly from a
C or C++ program
• System calls for modern Microsoft Windows
platforms are part of the Win32 application
programmer interface (API), which is available for
use by all the compilers written for Microsoft
Windows
Example of how system calls
are used…
• Consider writing a simple program to read
data from one file and to copy them to
another file
• What is the first thing the program needs?
• Which would be the process?
Details in the lecture…
Passing parameters to the
operating system
• The simplest approach is to pass parameters in
registers. In some cases, there may be more
parameters than registers. In these cases, the
parameters are generally stored in a block or table
in memory, and the address of the block is passed
as a parameter ina register. This is the approach
taken by LINUX. Parameters can also be placed,
or pushed onto a stack by the program, and
popped off the stack by the operating system.
Some operating systems prefer the stack or block
method, because they do not limit the number or
length of parameters being passed.
Groups of system calls
• Process control
–
–
–
–
–
–
–
End, abort
Load, execute
Create process, terminate
Get, set attributes
Wait for time
Wait event, signal event
Allocate free memory
• File management
–
–
–
–
Create, delete file
Open, close
Read, write, reposition
Get, set file attributes
• Device management
–
–
–
–
Request, release device
Read, write
Get, set device attributes
Logically attch, detach devices
• Information maintenance
– Get, set time/date
– Get, set system data
– Get, set process, file or device
attribute
• Communications
– Create, delete communciation
connection
– Send, receive messages
– Transfer status information
– Attach or detach remote
devices
SYSTEM CALLS FOR THE FILE
SYSTEM
Return File
Desc
open
creat
dup
pipe
close
Use of namei
open stat
creat
link
chdir unlink
chroot mknod
chown mount
chmod mount
Assign
inodes
creat
mknod
link
unlink
File
Attributes
chown
chmod
stat
File I/O
File Sys
Structure
Tree
Manipulation
read
write
lseek
mount
umount
chdir
chown
System Calls
• System calls that return file descriptors for use in
other system calls
• System calls that use the namei algorithm to parse a
path name
• System calls that assign and free inodes, using
algorithms ialloc and ifree
• System calls that set or change the attributes of a file
• System calls that do I/O to and from a process, using
algorithms alloc, free and the buffer application
algorithms
• System calls that change the structure of the file
system
• System calls that allow a process to change its view
of the fiel system tree
OPEN
•
•
The open system call is the first step a process must take to access the file
open(pathname,flags,modes)
Algorithm open
Inputs: file name, type of open, file permissions (for creation type of open)
Output: file descriptor
{
convert file name to inode (algorithm namei);
if (file does not exist or not permitted access) return (error);
allocate the file table entry for inode, initialise count, offset;
allocate user file descriptor entry, set pointer to file table entry;
if (type of open specifies truncate file)
free all file blocks (algorithm free);
unlock (inode);
/*locked above in namei */
return (user file descriptor);
}
Data structures after OPEN
User file
descriptor table
0
1
2
3
.
.
…
…
…
…
…
.
file table
inode table
Count 2 (/etc/passwd)
.
.
.
.
Count 1 Read
Count 1 Rd-Wrt
Count 1 Write
Count 1 (local)
Data structures after two processes
OPEN
User file
descriptor table
(proc A)
0
1
2
3
.
.
…
…
…
…
…
.
file table
.
.
.
.
Count 2 (/etc/passwd)
Count 1 Read
User file
descriptor table
(proc B)
0
1
2
3
.
.
…
…
…
…
…
.
inode table
Count 1 Rd-Wrt
Count 1 (local)
Count 1 Read
Count 1 (private)
.
.
.
.
Count 1 Write
Count 1 Read
read (fd,buffer,count)
READ
algorithm read
inputs: user file descriptor, address of buffer in user process, number of bytes to read
output: count of bytes copied into user space
{
get file table entry from user file descriptor;
check file accessibility;
set parameters in u area for user address, byte count, I/O to user;
get inode from file table;
lock inode;
set byte offset in u area from file table offset;
while (count not satisfied)
{
convert file offset to disk block (algorithm bmap);
calculate offset into block, number of bytes to read;
if (number of bytes to read is 0) break;
read block (algorithm breada if with read ahead, algorithm bread otherwise);
copy data from system buffer to user address;
update u area fields for file byte offset, read count, address to write into user space;
release buffer;
}
unlock inode;
update file table offset for next read;
return (total number of bytes read);
}
write (fd,buffer,count)
WRITE
Reading a file via two descriptors:
#include <fcntl.h>
main()
{
int fd1, fd2;
char buf1[512], buf2[512];
fd1 = open(“/etc/passwrd”, O_RD);
fd2 = open(“/etc/passwrd”, O_RD);
read(fd1,buf1,sizeof(buf1));
read(fd2,buf2,sizeof(buf2));
}
Tables after CLOSING a file
User file
descriptors
0
1
2
3
.
.
…
…
…
…
…
.
file table
.
.
.
.
Count 2 (/etc/passwd)
Count 1
Count 1
0
1
2
3
.
.
…
…
…
…
…
.
inode table
Count 1 (local)
Count 1
Count 1 (private)
.
.
.
.
Count 1
Count 1
FILE CREATION
algorithm creat
input: file name, permission settings
output: file descriptor
{
get inode for file name (algorithm namei);
if (file already exists)
{
if (not permitted access)
{ release inode (algorithm iput);
return (error); }
}
else
/*file does not exist yet */
{
assign free inode from file system (algorithm ialloc);
create new directory entry in parent directory;
include new file name and newly assigned inode number;
}
allocate file table entry for inode, initialise count;
if (file did exist at time of create)
free all file blocks (algorithm free);
unlock (inode);
return (user file descriptor);
}
chdir, chroot
•
•
•
•
chdir(pathname)
chroot(pathname)
chown(pathname, owner, group)
chmod(pathname, mode)
PIPES
• Pipes allow transfer of data between
processes in a FIFO manner.
• Named & unnamed pipes
• Pipe System call: pipe(fdptr)
• Opening a named pipe
• Reading and writing pipes
• Closing pipes
MOUNT - UNMOUNT
• The mount system call connects the file
system in a specified section of a disk to the
existing file system hierarchy
• The unmount system disconnects a file
system from the hierarchy
• mount(special pathname, directory
pathname, options)
• unmount(special pathname)
File System tree before and
after Mount
/
bin
cc
date
Root file system
etc
sh
usr
getty passwd
bin
awk
banner yacc
.
.
.
/
/dev/dsk1 file system
include
src
stdio.h
uts
LINK - UNLINK
• The link system call links a file to a new
name in the file system directory structure,
creating a new directory entry for an
exitsing node
• link(source file name, target file name)
• The unlink system call removes a directory
entry for a file
• unlink(pathname)
FILE SYSTEM
MAINTENANCE
• The kernel maintains consistency of the file
system during normal operation.
• The command fsck checks for consistency
and repairs the file system in case of
extraordinary circumstances (power failure)