«
A View of
Operating System Services
· Communications –Processes may exchange information, on the same computer or between
computers over a network. Communications may be via shared
memory or through message passing
· Error detection – OS needs to be constantly aware of possible errors
§ May occur in the CPU and memory hardware, in I/O
devices, in user program
§ For each type of error, OS take the appropriate action
to ensure correct and consistent computing
§ Debugging facilities enhance the user’s and
programmer’s abilities to efficiently use the system
· Resource
allocation - When multiple users or multiple jobs running
concurrently, resources must be allocated to each of them.
Many
types of resources such as CPU cycles, main memory and file storage have
special allocation code,
others
such as I/O devices may have general request and release code
· Accounting - To keep track
of which users use how much and what kinds of computer resources
· Protection and
security - owners of information stored in a multiuser or
networked computer system may want to control use of that information,
concurrent processes should not interfere with each other
§ Protection involves ensuring that
all access to system resources is controlled
§ Security from outsiders requires user authentication, defending external I/O
devices from invalid access attempts.
§ For a system to be protected and secure, precautions
must be instituted throughout it.
A chain is only as strong as
its weakest link.
«
User
Operating System Interface
· Command Line Interface (CLI) or command interpreter allows
direct command entry
§ Sometimes implemented in kernel, sometimes by systems
program
§ Sometimes multiple flavors implemented – shells
§ Primarily fetches a command from user and executes it
· Sometimes commands built-in, sometimes just names of
programs
If
the latter, adding new commands doesn’t require shell modification.
· User-friendly desktop Graphical
User Interface (GUI) metaphor interface
§ Usually mouse, keyboard, and monitor
§ Icons represent files, programs, actions, etc
§ Various mouse buttons over objects in the interface
cause various actions
(Provide
information, options, execute function, open directory (folder)
§ Invented at Xerox
PARC
· Many systems now include both CLI & GUI interfaces
§ Microsoft Windows is GUI with CLI “command” shell
§ Apple Mac OS has GUI interface with UNIX kernel
underneath and shells available
§ Solaris is CLI with optional GUI interfaces (Java
Desktop, KDE)
«
System
Calls
· Programming interface to the services provided by the
OS
· Typically written in a high-level language (C or C++)
· Mostly accessed by programs via a high-level Application
Program Interface (API)
· Three most common APIs are
§ Win32 API for Windows,
§ POSIX API (all versions of UNIX, Linux, and Mac OS)
§ Java API for the Java virtual machine (JVM)
Example of System Calls:
Examples:
ę Example 1: copy
Description: Open an existing
input file and copy it to non-existing
output file.
v C-version
http://www.cs.odu.edu/~cs471w/code/misc/copy.c
int
main(int argc, char **argv)
{
int n, in, out;
char buf[1024];
char InputFile[1024];
char OutputFile[1024];
struct
stat st;
printf("enter
Input File Name: ");
scanf("%s",
InputFile);
if (stat(InputFile, &st) != 0) {
perror("InputFile does not exit");
exit(1);
}
printf("enter
Outut File Name: ");
scanf("%s",
OutputFile);
if (stat(OutputFile,
&st) == 0) {
perror("OutputFile exists");
exit(1);
}
if ((in = open(InputFile, O_RDONLY))
< 0) {
perror(InputFile);
exit(1);
}
if ((out = open(OutputFile,
O_CREAT | O_WRONLY, 0755)) < 0) {
perror(OutputFile);
exit(1);
}
while ((n = read(in, buf, sizeof(buf))) > 0)
write(out, buf, n);
close(out);
close(in);
printf("copied
%s to %s\n", InputFile, OutputFile);
exit(0);
}
To execute:
% copy
NOTE: 0755 means file permission (0 for -) should be:
rwx r-x r-x
However,
the shell varaible umask (1 for -) may change the final permission.
For
example in /home/cs471w/.cshrc the value of umask 0133:
rw- r-- r--
The file
permission should not exceed the umask value and therefore the file created
will have permission: rw- r--
r--
v Java-version
http://www.cs.odu.edu/~cs471w/code/misc/jcopy.java
public class jcopy {
public static void main(String[] args) throws IOException {
BufferedReader
stdin = new BufferedReader( new InputStreamReader(System.in));
System.out.println("enter Input File
Name: ");
String InputFile
= stdin.readLine();
File infile
= new File(InputFile);
InputStream
fin = new FileInputStream(infile);
System.out.println("enter
Outut File Name: ");
String OutputFile
= stdin.readLine();
File outfile
= new File(OutputFile);
OutputStream
fout = new FileOutputStream(outfile);
int len;
byte[] buf
= new byte[1024];
while ((len
= fin.read(buf)) > 0) {
fout.write(buf, 0, len);
}
fin.close();
fout.close();
System.out.println("copied
" + InputFile + " to " + OutputFile);
}
}
To execute:
% java
jcopy
v Python-version
http://www.cs.odu.edu/~cs471w/code/misc/copy.py
#!
/usr/bin/python
import sys
print ("enter Input File Name: ")
fname = sys.stdin.readline()
InputFile=fname.strip('\n')
print(InputFile)
print ("enter Output File Name: ")
fname = sys.stdin.readline()
OutputFile=fname.strip('\n')
print(OutputFile)
try:
fin = open(InputFile,'r')
fout =
open(OutputFile,'w')
for line in fin:
fout.write(line)
fin.close()
fout.close()
except:
print ('Something went wrong.')
To execute:
% copy.py
ę Example 2: save:
Description: save 2nd argument (a string) into
1st argument file.
If the file exists it is truncated, otherwise
it is created.
It then displays the content of the file to tty (standard output or file
descriptor 1).
v C-version:
http://www.cs.odu.edu/~cs471w/code/misc/save.c
int
main(int argc, char *argv[])
{
int sfd;
int n;
char buf[1024];
int FileLength;
char *FileName
= argv[1];
char *Message = argv[2];
if ((sfd =
open(FileName, O_RDWR | O_TRUNC| O_CREAT, 0600))
< 0) {
perror(FileName);
exit(1);
}
write(sfd,
Message, strlen(Message));
lseek (sfd,0, SEEK_SET); // beginning of file
printf("\nFile '%s' contains:\n", FileName);
fflush(stdout);
n=read(sfd,
buf, sizeof(buf));
write(1,buf, n);
FileLength
= lseek(sfd, 0, SEEK_END); // end of file
printf("\n\nFile '%s' length: '%d'\n", FileName,
FileLength);
}
To execute:
% save
savefile “how are you?”
v Java-version
http://www.cs.odu.edu/~cs471w/code/misc/jsave.java
public class jsave
{
public static void main(String[] args) throws IOException {
File FileName
= new File(args[0]);
String Message = args[1];
OutputStream
fout = new FileOutputStream(FileName);
fout.write(Message.getBytes());
System.out.println("File
" + FileName + " contains:");
BufferedReader fin = new BufferedReader(
new FileReader(FileName));
String str;
while((str
= fin.readLine())!= null)
System.out.println(str);
long length = FileName.length();
System.out.println(
FileName + " lenght
is: " + length);
}
}
To execute:
% java jsave
savefile “how are you?”
v Python-version
http://www.cs.odu.edu/~cs471w/code/misc/save.py
#!
/usr/bin/env python
import sys
fname = sys.argv[1]
msg = sys.argv[2]
try:
fout = open(fname,'w')
fout.write(msg)
fout.close()
fin = open(fname,'r')
for line in fin:
print(line)
fin.seek(0,2)
# move the cursor to the end of the file
size = fin.tell()
print(size)
except:
print ('Something went wrong.')
To execute:
% save.py
savefile “how are you?”
API – System Call – OS Relationship
Standard C
Library Example:
C program invoking printf() library call, which calls write() system call
System Call
Parameter Passing
Three general methods used to pass parameters to the OS
1. Simplest: pass the parameters in registers
2. Parameters stored in a block
in memory, and address of block passed as a parameter in a register.
This approach taken by Linux and Solaris.
3. Parameters pushed onto the stack by the program and popped off the stack by the operating system
Block and stack methods do not limit the number or length of
parameters being passed
Types of System Calls:
ü
Process
control
ü
File
management
ü
Device
management
ü
Information
maintenance
ü
Communications
ü
Protection
Examples of Windows and Unix
System Calls
«
System
Programs
· System programs provide a convenient environment for
program development and execution.
They can be
divided into:
§ File management
§ Status information
§ Programming language support, loading and execution.
§ Communications
§ Application programs
· Most users’ view of
operating system is defined by system programs, not the actual system
calls
· Provide a convenient environment for program
development and execution
Some
of them are simply user interfaces to system calls; others are considerably
more complex
· File
management:
Create,
delete, copy, rename, print, dump, list, and generally manipulate files and
directories
· Status
information:
§ System info - date, time, amount of available memory,
disk space, number of users
§ Others provide detailed performance, logging, and
debugging information
§ Typically, these programs format and print the output
to the terminal or other output devices
«
Operating
System Design and Implementation
·
User
goals – operating system should be convenient to use, easy to learn, reliable,
safe, and fast
·
System
goals – operating system should be easy
to design, implement, maintain, flexible, reliable, error-free, and efficient
·
Important
principle to separate:
Policy: What will be done?
Mechanism: How to do it?
The
separation of policy from mechanism is a very important principle,
it allows
maximum flexibility if policy decisions are to be changed later
Traditional UNIX System Structure:
The UNIX OS
consists of two separable parts
· Systems programs
· The kernel
4 Consists of everything
below the system-call interface and above the physical hardware
4 Provides file system, CPU
scheduling, memory management, and other operating-system functions;
«
Virtual
Machines
· A virtual machine provides an interface identical
to the underlying bare hardware
· The operating system host creates the illusion
that a process has its own processor and (virtual memory)
· Each guest provided with a (virtual)
copy of underlying computer
VMware Architecture:
The Java Virtual Machine:
«
Operating-System
Debugging
· Debugging is finding and fixing errors,
or bugs
· OSs generate log files containing error
information
· Failure of an application can generate core
dump file capturing memory of the process
· Operating system failure can generate
crash dump file containing kernel memory
· Kernighan’s Law:
Debugging is twice
as hard as writing the code
in the first place.
Therefore, if you write the code cleverly
you are not
smart enough to debug it.
«
Operating
System Generation
·
Operating
systems are designed to run on any class of machines; the system must be configured for each specific computer
site
·
SYSGEN program obtains information for specific configuration of the hardware system
·
Booting – starting a computer by loading the
kernel
·
Bootstrap
program –stored in ROM,
locate the kernel, load it into memory, and start its execution