Date post: | 29-Dec-2015 |
Category: |
Documents |
Upload: | charlene-cook |
View: | 213 times |
Download: | 0 times |
CS 149: Operating SystemsJanuary 29 Class Meeting
Department of Computer ScienceSan Jose State University
Spring 2015Instructor: Ron Mak
www.cs.sjsu.edu/~mak
2Computer Science Dept.Spring 2015: January 29
CS 149: Operating Systems© R. Mak
Example System Call: fork()#include <stdio.h>
main(){ printf("Parent: Process started.\n"); printf("Parent: Forking a child.\n");
if (fork() != 0) { int status; printf("Parent: Waiting for child to complete.\n"); waitpid(-1, &status, 0); printf("Parent: Child has completed.\n"); printf("Parent: Terminating.\n"); } else { printf("Child: Process started.\n"); printf("Child: Start 10 second idle:");
int i; for (i = 10; i >= 0; i--) { printf("%3d", i); fflush(stdout); sleep(1); } printf(" done!\n"); printf("Child: Terminating.\n"); }}
Executed by the parent.
Executed by the child.
The two processes fork at this point.
forktest.c
3Computer Science Dept.Spring 2015: January 29
CS 149: Operating Systems© R. Mak
The waitpid() System Call
What are the parameters?
Use the command-line UNIX man (for “manual”) command to get documentation of any system command or API call._
waitpid(-1, &status, 0);
Demo
Pass the address of variable status(i.e., pass by reference).
4Computer Science Dept.Spring 2015: January 29
CS 149: Operating Systems© R. Mak
Shell: I/O Redirection
For a program run on the command line: Standard input (stdin) is the keyboard. Standard output (stdout) is the terminal.
#include <stdio.h>
#define MAX_LENGTH 256
main(int argc, char *args[]){ char *progname = args[0]; char line[MAX_LENGTH];
while (gets(line) != NULL) { printf("%s: %s\n", progname+2, line); }}
This program reads lines of textfrom stdin and echos them tostdout after prepending eachline with the program name.
echo.c
Why the +2?
5Computer Science Dept.Spring 2015: January 29
CS 149: Operating Systems© R. Mak
Shell: I/O Redirection
You can redirect a program’s stdin on the command line with the < operator:
Now the echo program will read its own source file.
You can redirect a program’s stdout on the command line with the > operator:
Now the echo program will write to file echo.txt. Use the >> operator to append to an output file.
You can redirect both stdout and stdin.
./echo < echo.c
./echo < echo.c > echo.txt
Demo
6Computer Science Dept.Spring 2015: January 29
CS 149: Operating Systems© R. Mak
OS Concept: Pipes
Pipes are a mechanism for interprocess communication (IPC).
One process writes to one end of a pipe. Another process reads from the other end. Each process believes it is doing file I/O.
Operating Systems: Design and Implementation, 3rd ed. Andrew Tanenbaum & Albert Woodhull (c) 2006 Prentice-Hall, Inc. 0-13-142938-8
7Computer Science Dept.Spring 2015: January 29
CS 149: Operating Systems© R. Mak
Shell: Piping
You can pipe a program’s output to another program with the | operator:
The first program’s stdout becomes the stdin for the second program.
What’s the result of this command?
./echo1 | ./echo2
./echo1 < echo.txt | ./echo2 | ./echo3
Demo
8Computer Science Dept.Spring 2015: January 29
CS 149: Operating Systems© R. Mak
The POSIX Standard
POSIX (Portable Operating System Interface)
IEEE standard for system calls.
Maintain compatibility among operating systems. UNIX and non-UNIX
9Computer Science Dept.Spring 2015: January 29
CS 149: Operating Systems© R. Mak
Process Management System Calls
POSIX system calls.
Most modern operating systems have system calls that perform these functions. However, details may differ.
Modern Operating Systems, 3rd ed.Andrew Tanenbaum(c) 2008 Prentice-Hall, Inc.. 0-13-600663-9All rights reserved
10Computer Science Dept.Spring 2015: January 29
CS 149: Operating Systems© R. Mak
File Management System Calls
Modern Operating Systems, 3rd ed.Andrew Tanenbaum(c) 2008 Prentice-Hall, Inc.. 0-13-600663-9All rights reserved
11Computer Science Dept.Spring 2015: January 29
CS 149: Operating Systems© R. Mak
File System Management System Calls
Modern Operating Systems, 3rd ed.Andrew Tanenbaum(c) 2008 Prentice-Hall, Inc.. 0-13-600663-9All rights reserved
12Computer Science Dept.Spring 2015: January 29
CS 149: Operating Systems© R. Mak
Miscellaneous System Calls
Modern Operating Systems, 3rd ed.Andrew Tanenbaum(c) 2008 Prentice-Hall, Inc.. 0-13-600663-9All rights reserved
13Computer Science Dept.Spring 2015: January 29
CS 149: Operating Systems© R. Mak
Kernel Mode vs. User Mode
An OS such as UNIX executes its critical system software in kernel mode. This is one way the OS protects itself.
As a user, you run utility programs (such as editors, compilers, and browsers) and application programs in user mode. System calls made by your programs can invoke
some OS code that is executed in kernel mode.
On the command line, you can become “super user” and bypass some of the OS protection mechanisms.
14Computer Science Dept.Spring 2015: January 29
CS 149: Operating Systems© R. Mak
Workflow of a System Call
1. Push nbytes2. Push &buffer3. Push fd4. Invoke the read
library routine.5. Put the system call code
for read in a register.6. TRAP instruction switches
to kernel mode to access the dispatcher in theOS kernel.
7. Dispatcher accesses the read handler.
8. Execute the read handler.9. Return to the user program
at the point after the TRAP instruction.
Invoke the system call count = read(fd, buffer, nbytes)
Operating Systems: Design and Implementation, 3rd ed. Andrew Tanenbaum & Albert Woodhull (c) 2006 Prentice-Hall, Inc. 0-13-142938-8
15Computer Science Dept.Spring 2015: January 29
CS 149: Operating Systems© R. Mak
Operating System Structure
There are various ways to organize the code of an operating system.
Monolithic Layered Virtual machines Client-server Distributed
_
16Computer Science Dept.Spring 2015: January 29
CS 149: Operating Systems© R. Mak
OS Structure: Monolithic
The original classic way an OS was designed.
No structure. A collection of routines. Each routine can call any other routine. No information hiding.
A challenge to build and link.
An even bigger challenge to maintain and debug.
Every software engineer should read the bookThe Mythical Man-Month about the development of IBM OS/360.
17Computer Science Dept.Spring 2015: January 29
CS 149: Operating Systems© R. Mak
OS Structure: Layered
Organize the OS as a hierarchy of layers. Each layer built on top of the one below it.
Operating Systems: Design and Implementation, 3rd ed. Andrew Tanenbaum & Albert Woodhull (c) 2006 Prentice-Hall, Inc. 0-13-142938-8
18Computer Science Dept.Spring 2015: January 29
CS 149: Operating Systems© R. Mak
OS Structure: Layered
Layers of the THE operating system.
For a Dutch computer in the late 1960s. The bottommost layer 0 provided multiprogramming.
_Operating Systems: Design and Implementation, 3rd ed. Andrew Tanenbaum & Albert Woodhull (c) 2006 Prentice-Hall, Inc. 0-13-142938-8
19Computer Science Dept.Spring 2015: January 29
CS 149: Operating Systems© R. Mak
OS Structure: Virtual Machines
Virtual machines (VMs) were first developed for the IBM 370 in the late 1960s.
Each virtual machine behaves like a separate physical machine.
Controlled by a virtual machine monitor.Operating Systems: Design and Implementation, 3rd ed. Andrew Tanenbaum & Albert Woodhull (c) 2006 Prentice-Hall, Inc. 0-13-142938-8
20Computer Science Dept.Spring 2015: January 29
CS 149: Operating Systems© R. Mak
OS Structure: Virtual Machines
Modern VM monitors include:
VirtualBox VMware KVM OpenVZ Xen
21Computer Science Dept.Spring 2015: January 29
CS 149: Operating Systems© R. Mak
OS Structure: Client-Server
Put as much OS code as possible outside of the kernel and into user space.
OS services run as separate processes. Processes send messages to each other to request services. A “microkernel” serves as the communications bus.
Operating Systems: Design and Implementation, 3rd ed. Andrew Tanenbaum & Albert Woodhull (c) 2006 Prentice-Hall, Inc. 0-13-142938-8
22Computer Science Dept.Spring 2015: January 29
CS 149: Operating Systems© R. Mak
OS Structure: Distributed
Like client-server, except that server processes run on different machines on the network.
Service request messages go over the network. User programs do not need to know
where the services are provided._
Operating Systems: Design and Implementation, 3rd ed. Andrew Tanenbaum & Albert Woodhull (c) 2006 Prentice-Hall, Inc. 0-13-142938-8
23Computer Science Dept.Spring 2015: January 29
CS 149: Operating Systems© R. Mak
Processes
A process is basically an abstraction of a running program. The most central concept in any operating system.
Each process runs in its own address space._
24Computer Science Dept.Spring 2015: January 29
CS 149: Operating Systems© R. Mak
Process Models
Multiprogramming withmultiple programs.
One program counterper CPU.
Each program has itsown virtual CPU.
The real CPU rapidlyswitches from oneprogram to another. Process switching
AKA context switchingOperating Systems: Design and Implementation, 3rd ed. Andrew Tanenbaum & Albert Woodhull (c) 2006 Prentice-Hall, Inc. 0-13-142938-8
25Computer Science Dept.Spring 2015: January 29
CS 149: Operating Systems© R. Mak
Process Models, cont’d
Only one program is active on a CPU at any instant.
Operating Systems: Design and Implementation, 3rd ed. Andrew Tanenbaum & Albert Woodhull (c) 2006 Prentice-Hall, Inc. 0-13-142938-8
26Computer Science Dept.Spring 2015: January 29
CS 149: Operating Systems© R. Mak
Context Switching
A process’s stateinformation is keptin its process controlblock (PCB).
Operating Systems: Design and Implementation, 3rd ed. Andrew Tanenbaum & Albert Woodhull (c) 2006 Prentice-Hall, Inc. 0-13-142938-8
27Computer Science Dept.Spring 2015: January 29
CS 149: Operating Systems© R. Mak
Process Creation
Principal events that cause processes to be created:
System initialization.
Execution of a process creation system call by a running process. fork()
A user request to create a new process.
Initiation of a batch job.
28Computer Science Dept.Spring 2015: January 29
CS 149: Operating Systems© R. Mak
Process Termination
Conditions that cause a process to terminate:
Normal exit (voluntary). Error exit (voluntary). Fatal error (involuntary). Killed by another process (involuntary).
Cascading termination On some operating systems, when a process
terminates, so do its child processes.
29Computer Science Dept.Spring 2015: January 29
CS 149: Operating Systems© R. Mak
Process Creation and Termination
What does the exec()system call do?
Operating Systems: Design and Implementation, 3rd ed. Andrew Tanenbaum & Albert Woodhull (c) 2006 Prentice-Hall, Inc. 0-13-142938-8
30Computer Science Dept.Spring 2015: January 29
CS 149: Operating Systems© R. Mak
Process States
Running Actually using the CPU at that instant.
Ready Runnable, but temporarily stopped to let another process run.
Blocked Unable to run until some external event happens.
Operating Systems: Design and Implementation, 3rd ed. Andrew Tanenbaum & Albert Woodhull (c) 2006 Prentice-Hall, Inc. 0-13-142938-8