Operating-System Structures
Operating-System Structures
System Calls
Examples:
Example 1: copy.c & jcopy.java
Description: Open an existing input file and copy it to nonexisting output file.
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, 0600)) < 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
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
Example 2: save.c:
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).
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);
printf("\nFile '%s' contains:\n", FileName);
fflush(stdout);
n=read(sfd, buf, sizeof(buf));
write(1,buf, n);
FileLength = lseek(sfd, 0, SEEK_END);
printf("\n\nFile '%s' length:
'%d'\n", FileName, FileLength);
}
To execute:
To execute:
% java jsave savefile how are you?
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 operation 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
Some systems implement a registry - used to store and retrieve
configuration information
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:
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
Kernighans Law: Debugging is twice as hard as writing the
code in the rst place.
Therefore, if you write the code as cleverly as possible, you are,
by denition, not smart enough to debug it.
System Boot:
Operating system must be made available to hardware to start
Small piece of code bootstrap loader, locates the kernel,
loads it into memory, and starts it
Sometimes two-step process where boot block at fixed location
loads bootstrap loader
When power initialized on system, execution starts at a fixed
memory location