CS 423 Operating System Design: The Programming Interface Jack - - PowerPoint PPT Presentation

cs 423 operating system design
SMART_READER_LITE
LIVE PREVIEW

CS 423 Operating System Design: The Programming Interface Jack - - PowerPoint PPT Presentation

CS 423 Operating System Design: The Programming Interface Jack Chen * Thanks for Prof. Adam Bates for the slides. CS423: Operating Systems Design Something fun? Tianyin asks me to tell something fun about OS in the beginning of the class.


slide-1
SLIDE 1

CS423: Operating Systems Design

Jack Chen

CS 423 Operating System Design:

The Programming Interface

* Thanks for Prof. Adam Bates for the slides.

slide-2
SLIDE 2

CS423: Operating Systems Design

Something fun?

2

∙ Tianyin asks me to tell something fun about OS in the

beginning of the class.

slide-3
SLIDE 3

CS 423: Operating Systems Design

A Brief note on Threading

3

∙ Why should an application use multiple threads? ∙ Things suitable for threading ∙ Block for potentially long waits ∙ Use many CPU cycles ∙ Respond to asynchronous events ∙ Execute functions of different importance ∙ Execute parallel code

slide-4
SLIDE 4

CS 423: Operating Systems Design

Example: Word Processor

4

What if it the application was single-threaded?

A Brief note on Threading

slide-5
SLIDE 5

CS 423: Operating Systems Design

Example: Web Server

5

A Brief note on Threading

What if it the application was single-threaded?

slide-6
SLIDE 6

CS423: Operating Systems Design

Common Multi-thread Software Architectures

6

■ Manager/worker

■ a single thread, the manager assigns work to other

threads, the workers. Typically, the manager handles all input and parcels out work to the other tasks

■ Pipeline

■ a task is broken into a series of sub-operations, each of

which is handled by a different thread. An automobile assembly line best describes this model

■ Peer

■ similar to the manager/worker model, but after the main

thread creates other threads, it participates in the work.

slide-7
SLIDE 7

CS423: Operating Systems Design

User-level Threads

7

■ Advantages

■ Fast Context Switching:

■ User level threads are implemented using user level thread libraries,

rather than system calls, hence no call to OS and no interrupts to kernel

■ When a thread is finished running for the moment, it can call

thread_yield. This instruction (a) saves the thread information in the thread table, and (b) calls the thread scheduler to pick another thread to run.

■ The procedure that saves the local thread state and the scheduler are

local procedures, hence no trap to kernel, no context switch, no memory switch, and this makes the thread scheduling very fast.

■ Customized Scheduling

slide-8
SLIDE 8

CS423: Operating Systems Design

The Programming Interface!

∙ Programming Interface is a very common thing in

software engineering ∙ It is developer defined, which means sometimes it can be weird ∙ PokeAPI ∙ SWAPI

slide-9
SLIDE 9

CS423: Operating Systems Design

The Programming Interface!

∙ POSIX Overview ∙ POSIX.1 ∙ POSIX.2 ∙ Other than POSIX?

slide-10
SLIDE 10

CS 423: Operating Systems Design 10

Network Hardware

Machine specific part

Web Server Second Life Yahoo Chat Pop Mail Application Software

Read/Write Communication

Portable

The Programming Interface!

Network Hardware

Machine specific part

Web Server Browser Pop Mail Application Software

Read/Write Communication

Operating System (machine independent part) Portable

OS Runs on Multiple Platforms while presenting the same Interface:

slide-11
SLIDE 11

CS423: Operating Systems Design

API is IP of OS

11

The Syscall API is bridges diverse applications and hardware in the system stack. Similar to the Internet Protocol (IP)’s role in the network stack!

slide-12
SLIDE 12

CS 423: Operating Systems Design

Application call libraries…

Software Layers

12

Application Portable OS Layer Libraries (e.g., stdio.h) Machine-dependent layer

Provided pre-compiled Defined in headers Input to linker (compiler) Invoked like functions May be “resolved” when program is loaded

slide-13
SLIDE 13

CS 423: Operating Systems Design

… libraries make OS system calls…

Software Layers

13

Application Portable OS Layer Libraries (e.g., stdio.h) Machine-dependent layer

system calls (read, open..) All “high-level” code

slide-14
SLIDE 14

CS 423: Operating Systems Design

… system calls access drivers, machine-specific code, etc.

Software Layers

14

Application Portable OS Layer Libraries (e.g., stdio.h) Machine-dependent layer

Bootstrap System initialization Interrupt and exception I/O device driver Memory management Kernel/user mode switching Processor management

slide-15
SLIDE 15

CS423: Operating Systems Design

Some Important Syscall Families

15

∙ Performing I/O ∙ open, read, write, close ∙ Creating and managing processes ∙ fork, exec, wait ∙ Communicating between processes ∙ pipe, dup, select, connect

slide-16
SLIDE 16

CS 423: Operating Systems Design

Example Syscall Workflow

16

read (fd, buffer, nbytes)

slide-17
SLIDE 17

CS423: Operating Systems Design

Question

17

Is it possible to invoke a system call without libc? yes.

# define _GNU_SOURCE # include <unistd.h> # include <sys/syscall.h> # include <sys/types.h> # include <signal.h> int main (int argc, char *argv[]) { pid_t tid; tid = syscall(SYS_gettid); syscall(SYS_tgkill, getpid(), tid, SIGHUP); }

slide-18
SLIDE 18

CS 423: Operating Systems Design

… file management:

POSIX Syscalls for…

18

slide-19
SLIDE 19

CS 423: Operating Systems Design

… directory management:

19

POSIX Syscalls for…

slide-20
SLIDE 20

CS423: Operating Systems Design

Open: more than meets the eye

20

slide-21
SLIDE 21

CS 423: Operating Systems Design

Shells… how do they work?

21

SHELLS

A shell is a job control system Allows programmer to create and manage a set of programs to do some task Windows, MacOS, Linux all have shells

Example: Shell cmds to compile a C program cc –c sourcefile1.c cc –c sourcefile2.c ld –o program sourcefile1.o \ sourcefile2.o

slide-22
SLIDE 22

CS423: Operating Systems Design

Shell Question

22

If the shell runs at user-level, what system calls does it make to run each of the programs?

slide-23
SLIDE 23

CS 423: Operating Systems Design

… process management:

23

POSIX Syscalls for…

UNIX fork – system call to create a copy of the current process, and start it running No arguments!

slide-24
SLIDE 24

CS423: Operating Systems Design

UNIX Process Mgmt

24

slide-25
SLIDE 25

CS423: Operating Systems Design

Implementing UNIX Fork

25

slide-26
SLIDE 26

CS423: Operating Systems Design

Implementing UNIX Exec

26

slide-27
SLIDE 27

CS423: Operating Systems Design

Simple Shell Implementation

27

char *prog, **args; int child_pid; // Read and parse the input a line at a time while (readAndParseCmdLine(&prog, &args)) { child_pid = fork(); // create a child process if (child_pid == 0) { exec(prog, args); // I'm the child process. Run program // NOT REACHED } else { wait(child_pid); // I'm the parent, wait for child return 0; } }

slide-28
SLIDE 28

CS423: Operating Systems Design

Process Mgmt Questions

28

slide-29
SLIDE 29

CS 423: Operating Systems Design

… miscellaneous tasks:

29

POSIX Syscalls for…

slide-30
SLIDE 30

CS 423: Operating Systems Design

POSIX 2

  • Simple utilities

○ cd ○ cp ○ ls ○ grep ○ wc ○ ...

slide-31
SLIDE 31

CS423: Operating Systems Design

What about Windows?

31

Windows has CreateProcess

slide-32
SLIDE 32

CS423: Operating Systems Design

What about Windows?

32

Windows has CreateProcess

slide-33
SLIDE 33

CS 423: Operating Systems Design

What about … Others?

∙ Linux ○ cgroup ■ enforce resource limits on different processes ∙ FreeBSD ○ kqueue() ■ implementation to solve C10K problem. Linux counterpart is epoll()