![Page 1: CS 149: Operating Systems January 29 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak](https://reader030.vdocuments.us/reader030/viewer/2022032708/56649e725503460f94b71ff8/html5/thumbnails/1.jpg)
CS 149: Operating SystemsJanuary 29 Class Meeting
Department of Computer ScienceSan Jose State University
Spring 2015Instructor: Ron Mak
www.cs.sjsu.edu/~mak
![Page 2: CS 149: Operating Systems January 29 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak](https://reader030.vdocuments.us/reader030/viewer/2022032708/56649e725503460f94b71ff8/html5/thumbnails/2.jpg)
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
![Page 3: CS 149: Operating Systems January 29 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak](https://reader030.vdocuments.us/reader030/viewer/2022032708/56649e725503460f94b71ff8/html5/thumbnails/3.jpg)
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).
![Page 4: CS 149: Operating Systems January 29 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak](https://reader030.vdocuments.us/reader030/viewer/2022032708/56649e725503460f94b71ff8/html5/thumbnails/4.jpg)
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?
![Page 5: CS 149: Operating Systems January 29 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak](https://reader030.vdocuments.us/reader030/viewer/2022032708/56649e725503460f94b71ff8/html5/thumbnails/5.jpg)
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
![Page 6: CS 149: Operating Systems January 29 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak](https://reader030.vdocuments.us/reader030/viewer/2022032708/56649e725503460f94b71ff8/html5/thumbnails/6.jpg)
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
![Page 7: CS 149: Operating Systems January 29 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak](https://reader030.vdocuments.us/reader030/viewer/2022032708/56649e725503460f94b71ff8/html5/thumbnails/7.jpg)
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
![Page 8: CS 149: Operating Systems January 29 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak](https://reader030.vdocuments.us/reader030/viewer/2022032708/56649e725503460f94b71ff8/html5/thumbnails/8.jpg)
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
![Page 9: CS 149: Operating Systems January 29 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak](https://reader030.vdocuments.us/reader030/viewer/2022032708/56649e725503460f94b71ff8/html5/thumbnails/9.jpg)
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
![Page 10: CS 149: Operating Systems January 29 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak](https://reader030.vdocuments.us/reader030/viewer/2022032708/56649e725503460f94b71ff8/html5/thumbnails/10.jpg)
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
![Page 11: CS 149: Operating Systems January 29 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak](https://reader030.vdocuments.us/reader030/viewer/2022032708/56649e725503460f94b71ff8/html5/thumbnails/11.jpg)
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
![Page 12: CS 149: Operating Systems January 29 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak](https://reader030.vdocuments.us/reader030/viewer/2022032708/56649e725503460f94b71ff8/html5/thumbnails/12.jpg)
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
![Page 13: CS 149: Operating Systems January 29 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak](https://reader030.vdocuments.us/reader030/viewer/2022032708/56649e725503460f94b71ff8/html5/thumbnails/13.jpg)
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.
![Page 14: CS 149: Operating Systems January 29 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak](https://reader030.vdocuments.us/reader030/viewer/2022032708/56649e725503460f94b71ff8/html5/thumbnails/14.jpg)
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
![Page 15: CS 149: Operating Systems January 29 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak](https://reader030.vdocuments.us/reader030/viewer/2022032708/56649e725503460f94b71ff8/html5/thumbnails/15.jpg)
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
_
![Page 16: CS 149: Operating Systems January 29 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak](https://reader030.vdocuments.us/reader030/viewer/2022032708/56649e725503460f94b71ff8/html5/thumbnails/16.jpg)
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.
![Page 17: CS 149: Operating Systems January 29 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak](https://reader030.vdocuments.us/reader030/viewer/2022032708/56649e725503460f94b71ff8/html5/thumbnails/17.jpg)
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
![Page 18: CS 149: Operating Systems January 29 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak](https://reader030.vdocuments.us/reader030/viewer/2022032708/56649e725503460f94b71ff8/html5/thumbnails/18.jpg)
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
![Page 19: CS 149: Operating Systems January 29 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak](https://reader030.vdocuments.us/reader030/viewer/2022032708/56649e725503460f94b71ff8/html5/thumbnails/19.jpg)
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
![Page 20: CS 149: Operating Systems January 29 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak](https://reader030.vdocuments.us/reader030/viewer/2022032708/56649e725503460f94b71ff8/html5/thumbnails/20.jpg)
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
![Page 21: CS 149: Operating Systems January 29 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak](https://reader030.vdocuments.us/reader030/viewer/2022032708/56649e725503460f94b71ff8/html5/thumbnails/21.jpg)
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
![Page 22: CS 149: Operating Systems January 29 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak](https://reader030.vdocuments.us/reader030/viewer/2022032708/56649e725503460f94b71ff8/html5/thumbnails/22.jpg)
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
![Page 23: CS 149: Operating Systems January 29 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak](https://reader030.vdocuments.us/reader030/viewer/2022032708/56649e725503460f94b71ff8/html5/thumbnails/23.jpg)
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._
![Page 24: CS 149: Operating Systems January 29 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak](https://reader030.vdocuments.us/reader030/viewer/2022032708/56649e725503460f94b71ff8/html5/thumbnails/24.jpg)
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
![Page 25: CS 149: Operating Systems January 29 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak](https://reader030.vdocuments.us/reader030/viewer/2022032708/56649e725503460f94b71ff8/html5/thumbnails/25.jpg)
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
![Page 26: CS 149: Operating Systems January 29 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak](https://reader030.vdocuments.us/reader030/viewer/2022032708/56649e725503460f94b71ff8/html5/thumbnails/26.jpg)
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
![Page 27: CS 149: Operating Systems January 29 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak](https://reader030.vdocuments.us/reader030/viewer/2022032708/56649e725503460f94b71ff8/html5/thumbnails/27.jpg)
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.
![Page 28: CS 149: Operating Systems January 29 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak](https://reader030.vdocuments.us/reader030/viewer/2022032708/56649e725503460f94b71ff8/html5/thumbnails/28.jpg)
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.
![Page 29: CS 149: Operating Systems January 29 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak](https://reader030.vdocuments.us/reader030/viewer/2022032708/56649e725503460f94b71ff8/html5/thumbnails/29.jpg)
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
![Page 30: CS 149: Operating Systems January 29 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak](https://reader030.vdocuments.us/reader030/viewer/2022032708/56649e725503460f94b71ff8/html5/thumbnails/30.jpg)
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