1 Lecture 11 Operating System Interaction This lecture covers three items, thread, is the most interesting as it makes the system faster if you know how.

Slides:



Advertisements
Similar presentations
Threads, SMP, and Microkernels
Advertisements

Part IV: Memory Management
Copyright © 2000, Daniel W. Lewis. All Rights Reserved. CHAPTER 10 SHARED MEMORY.
WHAT IS AN OPERATING SYSTEM? An interface between users and hardware - an environment "architecture ” Allows convenient usage; hides the tedious stuff.
EEE 435 Principles of Operating Systems Principles and Structure of I/O Software (Modern Operating Systems 5.2 & 5.3) 5/22/20151Dr Alain Beaulieu.
1 Lecture 10 – Memory Operation and Performance CachesCaches – repeat some concepts Virtual Memory (VM)
1: Operating Systems Overview
1 Thursday, June 15, 2006 Confucius says: He who play in root, eventually kill tree.
1 Chapter 4 Threads Threads: Resource ownership and execution.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
A. Frank - P. Weisberg Operating Systems Introduction to Tasks/Threads.
Threads CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University
INPUT/OUTPUT ORGANIZATION INTERRUPTS CS147 Summer 2001 Professor: Sin-Min Lee Presented by: Jing Chen.
1 Threads Chapter 4 Reading: 4.1,4.4, Process Characteristics l Unit of resource ownership - process is allocated: n a virtual address space to.
What do operating systems do? manage processes manage memory and computer resources provide security features execute user programs make solving user.
CS364 CH08 Operating System Support TECH Computer Science Operating System Overview Scheduling Memory Management Pentium II and PowerPC Memory Management.
Layers and Views of a Computer System Operating System Services Program creation Program execution Access to I/O devices Controlled access to files System.
Chapter 51 Threads Chapter 5. 2 Process Characteristics  Concept of Process has two facets.  A Process is: A Unit of resource ownership:  a virtual.
System Calls 1.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
LOGO OPERATING SYSTEM Dalia AL-Dabbagh
Operating System Review September 10, 2012Introduction to Computer Security ©2004 Matt Bishop Slide #1-1.
Chapter 5 Operating System Support. Outline Operating system - Objective and function - types of OS Scheduling - Long term scheduling - Medium term scheduling.
9 Chapter Nine Compiled Web Server Programs. 9 Chapter Objectives Learn about Common Gateway Interface (CGI) Create CGI programs that generate dynamic.
Operating Systems Lecture 2 Processes and Threads Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard. Zhiqing Liu School of.
Recall: Three I/O Methods Synchronous: Wait for I/O operation to complete. Asynchronous: Post I/O request and switch to other work. DMA (Direct Memory.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto OS-Related Hardware.
CIS250 OPERATING SYSTEMS Memory Management Since we share memory, we need to manage it Memory manager only sees the address A program counter value indicates.
Invitation to Computer Science 5 th Edition Chapter 6 An Introduction to System Software and Virtual Machine s.
Lecture 11 Dynamic link libraries. Differences between static libraries and DLLs In static library code is added to the executable. In DLL, the code is.
1 Scheduling The part of the OS that makes the choice of which process to run next is called the scheduler and the algorithm it uses is called the scheduling.
Hardware process When the computer is powered up, it begins to execute fetch-execute cycle for the program that is stored in memory at the boot strap entry.
Games Development 2 Concurrent Programming CO3301 Week 9.
What Every Developer Should Know about the Kernel Dr. Michael L. Collard 1.
Threads G.Anuradha (Reference : William Stallings)
Writing a Run Time DLL The application loads the DLL using LoadLibrary() or LoadLibraryEx(). The standard search sequence is used by the operating system.
ITFN 3601 Introduction to Operating Systems Lecture 3 Processes, Threads & Scheduling Intro.
Lecture 5 Page 1 CS 111 Online Processes CS 111 On-Line MS Program Operating Systems Peter Reiher.
Operating Systems 1 K. Salah Module 1.2: Fundamental Concepts Interrupts System Calls.
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
Operating Systems CSE 411 CPU Management Sept Lecture 10 Instructor: Bhuvan Urgaonkar.
Concurrency, Processes, and System calls Benefits and issues of concurrency The basic concept of process System calls.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
Chapter Eleven Windows XP Professional Application Support.
3/12/2013Computer Engg, IIT(BHU)1 OpenMP-1. OpenMP is a portable, multiprocessing API for shared memory computers OpenMP is not a “language” Instead,
Introduction Contain two or more CPU share common memory and peripherals. Provide greater system throughput. Multiple processor executing simultaneous.
I/O Software CS 537 – Introduction to Operating Systems.
Interrupts and Exception Handling. Execution We are quite aware of the Fetch, Execute process of the control unit of the CPU –Fetch and instruction as.
Embedded Real-Time Systems Processing interrupts Lecturer Department University.
Threads, SMP, and Microkernels Chapter 4. Processes and Threads Operating systems use processes for two purposes - Resource allocation and resource ownership.
CSCI/CMPE 4334 Operating Systems Review: Exam 1 1.
Some of the utilities associated with the development of programs. These program development tools allow users to write and construct programs that the.
Chapter Goals Describe the application development process and the role of methodologies, models, and tools Compare and contrast programming language generations.
Architectures of Digital Information Systems Part 1: Interrupts and DMA dr.ir. A.C. Verschueren Eindhoven University of Technology Section of Digital.
Processes and threads.
Process concept.
CS 6560: Operating Systems Design
William Stallings Computer Organization and Architecture
Introduction to Operating System (OS)
Lecture 1 Runtime environments.
Threads, SMP, and Microkernels
Chapter 15, Exploring the Digital Domain
Machine Independent Features
Memory Management Tasks
Threads Chapter 4.
Chapter 1 Introduction to Operating System Part 2
PROCESSES & THREADS ADINA-CLAUDIA STOICA.
Lecture 1 Runtime environments.
Foundations and Definitions
Chapter 13: I/O Systems “The two main jobs of a computer are I/O and [CPU] processing. In many cases, the main job is I/O, and the [CPU] processing is.
Presentation transcript:

1 Lecture 11 Operating System Interaction This lecture covers three items, thread, is the most interesting as it makes the system faster if you know how to use it properly. Dynamic Linking Time-Sharing Threads

2 Dynamic Linking It means to link the library during run time, not compilation time. Libraries Dynamic-Link Libraries (DLLs) Example of DLL

3 Library in Visual C++

4 Example of linking

5 Explanation – static (it means fixed) In the above diagram, the application object has to link with malloc() and calling main() to form an executable (exe) file.

6 Dynamic-Link Libraries (DLLs) – not fixed during compilation Dynamic linking means where linking is performed on demand at runtime. An advantage of dynamic linking is that executable files can be much smaller than statically linked executables. Of course, the executable is not complete without all of the associated library files, but if many executables share a set of libraries, there can be a significant, overall savings. When you want to run

7 Fixed and Dynamic Linking User’s Code Library User’s Code Library

8 Advantage of DLL (1) In most systems, the space savings extend to memory. When libraries are dynamically linked, the operating system can arrange to let applications share the library code so that only one copy of the library is loaded into memory. With static linking, each executable is a monolithic binary program. If several programs are using the same libraries, there will be several copies of the code in memory.

9 Advantage of DLL(2) Another potential memory savings comes from the fact that dynamically linked libraries do not necessarily need to be loaded. For example, an image editor may support input and output of dozens of file formats. It could be expensive (and unnecessary) to link conversion routines for all of these formats. With dynamic linking, the program can link code as it becomes useful, saving time and memory. This can be especially useful in programs with ever growing lists of features.

10 Disadvantage of DLL First, there are version problems. Like all software, libraries tend to develop. New libraries may be incompatible with old libraries in subtle ways. If you update the libraries used by a program, it may have good, bad, or no effects on the program's behavior. In contrast, a statically linked program will never change its behavior unless the entire program is relinked and installed.

11 Example of DLL DLL Initialization C Names and C++ Decorated Names The.DEF File Calling Conventions Building and Linking Testing the DLL DLL Precautions Using Declarations instead of.DEF Files Backward Compatibility Explicit Control over Run-Time Linking Summary No need to memorise

12 Simple DLL program For the following program, the library will export an integer function that adds one to its integer parameter int mydll_data = 0; int mydll_function(int n) { mydll_data = n; return n + 1; }

13 DLL Initialization the function in mydll.cpp declared as: BOOL __stdcall DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) This routine is called when the DLL is loaded and unloaded.

14 C Names and C++ Decorated Names Consider the following, where myfunc is defined three times: (how can we differentiate?) class c1 { public: int myfunc(); int myfunc(int n); }; class c2 { public: int myfunc(); };

15 Differentiation int mydll_function(void) might appear in the object file as the symbol: Yes, the symbol name actually starts with "?"! The extra characters encode enough type and scope information to make this symbol unique to this declaration of mydll_function. That is three functions can be identified.

16 The.DEF File Before we build the DLL, we need to specify the interface to the DLL. In other words, you need to specify what symbols will be visible to other programs that load the DLL. This is done by adding a.DEF file to the project (Click Project. Then click Add To Project. Then click Files. Then choose the.DEF file). The file for this project is mydll.def, shown here: LIBRARY MYDLL DESCRIPTION "Demonstrates a simple DLL" EXPORTS No need to memorise

17 Building and Linking Note the table

18 Calling procedure

19 DLL Precautions 1. You must specify what symbols are exported from a DLL—for example, using a.DEF file. 2. If you use a.DEF file, you must be careful about names. You can either use "decorated" C++ names (later we will see where to find them), or you can use extern "C" {...} to specify simple names. 3. The program must be linked with the.lib file that corresponds to the.dll file. 4. Functions are typically declared with __stdcall, making DLLs accessible by programs written in other languages. 5. The DLL must be available at run time. In this example, we copied mydll.dll to the same folder as dlltest.exe. 6. Data imported using a.DEF file must be declared using __declspec(dllimport). No need to memorise

20 Using Declarations instead of.DEF Files In many cases, you can avoid a lot of work by using special declarations in your DLL. These declarations can do the job of the.DEF file, which not only saves work but avoids the need for decorated names or extern "C" declarations.

21 Summary Dynamic linking is an important mechanism for structuring programs and operating systems, especially in Windows systems. As programs become large and offer more and more features, it becomes important to view them as collections of subsystems that can be configured as needed. DLLs allow programs to share code on disk and in memory, making them smaller and more efficient. DLLs can be updated to add features or to fix bugs

22 Time-Sharing Why Time-Share a PC? – to support more processes and make use of the power of CPU Context Switching - switching amongst processes, here, there is a need to prepare from process 1 to process 2 Process 1Process 2Process 3

23 Why Time-Share a PC? Time-sharing occurs when more than one program executes at the same time. Unless there is more than one processor, programs do not actually execute simultaneously, but a rapid switching of the processor from one program to the next gives the impression of simultaneous execution.

24 Example of process – Windows XP

25 System Calls To simplify the programming. Say, for example, you can call malloc() to grab memory instead of writing a subroutine to handle memory allocation.

26 Other reasons of having system call It is to protect the memory 1. Each executing program has its own memory. It is not possible for one process to directly access the memory of another process. 2.No process is allowed to "steal" the processor. Even if a program tries to execute an infinite loop, a timer eventually interrupts the processor and transfers control to some other processes. 3.Processes are not allowed free access to input/output devices such as the screen, the disk, and the network.

27 Context Switching To switch among processes Each process is allocated to use the CPU for a quantum. This quantum is about 20 ms. Say you have three processes, CPU will execute P1 (about 20 ms), P2 (about 20 ms) and P3 (20 ms), P1 or P2 again depending whether P1 is ready. P1 might wait for a request from a disk.

28 Example of Context Switching CPU

29 Process state Here, there are three states for one process. Running means it uses the CPU, ready means it is ready to use the CPU,while suspended means it is waiting for an I/O.

30 Non-Preemptive process Must finish before CPU can switch to others, say you have three processes, P1, P2, P3

31 Preemptive process CPU can switch to the other processes without finishing the current process, say from p1 to p2

32 Threads This lecture covers thread which is a light weight process. Threads vs. Processes Applications of Threads Reentrant Code Concurrent Data Access Scheduling Effects

33 Threads vs. Processes What is different? Thread and process Processes are ideal for running independent programs but sometimes we want to use multiple processes in a single program. A thread is essentially the same as a process except that threads share a single address space. You can create a process or a thread using CreateProcess() …. CreateThread()

34 Advantages of Using Thread First, there is considerable cost to creating a virtual address space and changing address spaces with each context switch. Threads can eliminate the need to change address spaces after a context switch. (thread is simpler than process) Secondly, and perhaps more importantly, due to the fact that threads share memory, threads can share data much more easily. (thread can share memory easier than process)

35 Applications of Threads What are the applications? Background Processing (Some programs have operations that run for many seconds, minutes, or even hours, it is better to use threads for these operations.) Asynchronous I/O (Normally, when a program reads or writes to a file, the I/O operation does not return until it is complete). Sometimes, a program can continue without waiting for completion.

36 Application

37 Applications of Threads Prefetching (Asynchronous input is sometimes called prefetching because data is read (fetched) before it is needed. File systems often prefetch data, so setting up a thread to prefetch data from a file is probably not a good idea.) For example, while reading an image, your browser could pre-load the next image.

38 Push-Pull Conversion – use a file for communication or event to control the flow

39 Solution – to multi-threads Create two threads, one to read, while the other is to write and use events to control the flow of data. Otherwise, the data in the buffer will be corrupted.

40 Applications Real-Time Systems (A program may have a combination of high-priority, time- critical tasks and less important, non- time-critical tasks. For example, an MP3 audio player must decode audio samples and deliver them to an audio output device at a rate of sample pairs (for stereo quality) per second)

41 Example of reentrant code if (i != 0) x = 1.0 / i; It seems to be it is true for a single task, but might not be true in multi-thread/multi-task, as i can be changed by other thread during context switching. Switching amongst processes

42 Another example int temp; void swap(int a[], int i, int j) { temp = a[i]; a[i] = a[j]; a[j] = temp; }

43 Need of synchronisation There are many methods to prevent data corruption due to mis-synchronisation such as: Critical_section (enter critical section and leave critcial section, use SetEvent and WaitForSingle Object to wait unitl one of the threads has finished modified the data.) You can use the above in your lab. to play around it.

44 Scheduling Effects

45 Summary Dynamic linking Time sharing Process Threads