CSIT345-Operating Systems Lecture 14 System Call, Process, Thread, IPC

Download Report

Transcript CSIT345-Operating Systems Lecture 14 System Call, Process, Thread, IPC

CSIT345-Operating Systems
Lecture 14
System Call, Process, Thread,
IPC
Prof. Boxiang Dong
www.cs.stevens.edu/~bdong
Office: RI-320
Email: [email protected]
System Call
• System call: the programming interface / instructions provided by the
kernel.
System Call
• System call sequence to copy the contents of one file to another file
System Call
System Call
• Dual-mode
• Kernel mode: execute system calls with higher privilege
• User mode: execute library calls or user applications
• Aim: protect the operating system from errant users
• Transition from user mode to kernel mode
• Interrupt: wait for I/O, program finishes
• Trap: exception or error
• System call: explicitly revoke system calls
System Call
Explicit system call
System Call
Process Concept
Windows
CSIT 345 - Operating Systems, Prof. Boxiang Dong, Montclair
State University
Linux
8
Process Concept – Informal Definition
• Process is the program in execution.
• Process is the unit of work in modern time-sharing operating systems.
CSIT 345 - Operating Systems, Prof. Boxiang Dong, Montclair
State University
9
Process Concept – Process v.s. Program
• Program is passive entity stored on disk (executable file), process is
active
• Program becomes process when executable file loaded into memory
• Execution of program started via GUI mouse clicks, command line
entry of its name, etc
• One program can be several processes
• Consider multiple users executing the same program
CSIT 345 - Operating Systems, Prof. Boxiang Dong, Montclair
State University
10
Process Concept – Memory Space
•
•
•
•
Text: code or program.
Data: static and global variable.
Stack: static local data.
Heap: dynamic local data.
CSIT 345 - Operating Systems, Prof. Boxiang Dong, Montclair
State University
11
Process Concept – Process State
• Each process may be in one of the five states.
CSIT 345 - Operating Systems, Prof. Boxiang Dong, Montclair
State University
12
Process Concept – Process State
Only one process can be running on any processor at any instant.
CSIT 345 - Operating Systems, Prof. Boxiang Dong, Montclair
State University
13
Process Concept – Process Control Block
CSIT 345 - Operating Systems, Prof. Boxiang Dong, Montclair
State University
14
Process Concept – Process Control Block
• The view of a process to the operating system.
•
•
•
•
•
•
Process state
Program counter
CPU registers
CPU-scheduling information: process priority
Account information: CPU usage
I/O status information: list of I/O devices allocated to the process.
CSIT 345 - Operating Systems, Prof. Boxiang Dong, Montclair
State University
15
Code Review – Process Creation I
CSIT 345 - Operating Systems, Prof. Boxiang Dong, Montclair
State University
16
Code Review – Process Creation II
What is the expected output?
CSIT 345 - Operating Systems, Prof. Boxiang Dong, Montclair
State University
17
Thread Concept
• Thread is a basic unit of CPU utilization.
CSIT 345 - Operating Systems, Prof. Boxiang Dong, Montclair
State University
18
Thread Concept – Multiprogramming v.s.
Multithreading v.s. Multicore Programming
• Multiprogramming: more than one programs loaded into memory for
execution
• Multithreading: a process contains multiple threads.
• Multicore Programming: threads run in parallel.
CSIT 345 - Operating Systems, Prof. Boxiang Dong, Montclair
State University
19
Programming – Thread Libraries
An extension of POSIX standard
POSIX Threads
Both user and kernel library
Win32
Java thread
Kernel library
User library
CSIT 345 - Operating Systems, Prof. Boxiang Dong, Montclair
State University
20
Programming – POSIX Thread (Pthread)
Library
• POSIX Thread Library: a standardized C language threads
programming interface
• Thread management
• Creation and termination
• Join and detaching
• Thread synchronization
• Mutex variables
• Conditional variables
• Install on Ubuntu: sudo apt-get install libpthread-stubs0-dev
CSIT 345 - Operating Systems, Prof. Boxiang Dong, Montclair
State University
21
Programming – POSIX Thread (Pthread)
Library
• thread: An opaque, unique identifier for the new thread returned by the subroutine.
• attr: An opaque attribute object that may be used to set thread attributes. You can
specify a thread attributes object, or NULL for the default values.
• start_routine: the C routine that the thread will execute once it is created.
• arg: A single argument that may be passed to start_routine. It must be passed by
reference as a pointer cast of type void. NULL may be used if no argument is to be
CSIT 345 - Operating Systems, Prof. Boxiang Dong, Montclair
22
passed.
State University
Programming – Pthread Creation
CSIT 345 - Operating Systems, Prof. Boxiang Dong, Montclair
State University
23
Code Review – Pthread Creation
CSIT 345 - Operating Systems, Prof. Boxiang Dong, Montclair
State University
24
IPC Channels
faster
shared memory
subject to conflict
message queue: structured data
slower
socket: unstructured data
between hosts
conflict-free
Shared Memory
• Communicating processes establish a region of shared-memory.
• Only one system call transition is required for shared-memory establishment.
• Exchange information by reading and writing data in the shared areas.
• Conflict arises when two processes write to the same location simultaneously.
Shared Memory
Code Review – Attach to and Send Message
vis Shared Memory
Code Review – Online Chat System via Shared
Memory
Code Review – Online Chat System via Shared
Memory
Message Queue
• Message queue allows processes to communicate without sharing the
same address space.
• A message queue facility provides at least two operations:
• Send(message)
• Receive(message)
Code Review – Online Chat System via
Message Queue
Code Review – Online Chat System via
Message Queue
Socket - Motivation
• So far, we only consider the interaction between processes that reside
in the same host.
• Shared memory: processes reside in the same memory.
• Message queue: processes have the same message queue identifier.
• What if two processes that reside in two remote computer systems
need communication?
Socket - Definition
Socket is an end point of
communication between
two systems on a network.
Socket - Definition
• Socket is a combination of IP address and port on one system.
Socket - Definition
• In an operating system, port numbers range from 0 to 65536.
• All ports below 1024 are used for specific and well-known services.
• Telnet 23.
• FTP 21.
• HTTP 80.
Socket - Workflow
Programming – Socket Write
Programming – Socket Write
Programming – Socket Read
Programming – Socket Read
Programming - Socket
Remote Procedure Call (RPC)
The RPC run-time stubs and libraries manage most of the processes relating to
network protocols and communication. This enables you to focus on the details of the
application rather than the details of the network.