Operating Systems - SourceForgealphapeeler.sourceforge.net/uit/2016_fall/CS311/week05a.pdf ·...
Transcript of Operating Systems - SourceForgealphapeeler.sourceforge.net/uit/2016_fall/CS311/week05a.pdf ·...
Engr. Abdul-Rahman MahmoodMS, PMP, MCP, QMR(ISO9001:2000)
[email protected] [email protected]
alphapeeler.sf.net/pubkeys/pkey.htm http://alphapeeler.sourceforge.net
pk.linkedin.com/in/armahmood http://alphapeeler.tumblr.com
www.twitter.com/alphapeeler [email protected]
www.facebook.com/alphapeeler [email protected]
abdulmahmood-sss alphasecure mahmood_cubix 48660186
[email protected] [email protected]
http://alphapeeler.sf.net/me http://alphapeeler.sf.net/acms/
VC++, VB, ASP
Operating Systems
Processes and Threads
Resource Ownership
Process includes a
virtual address space
to hold the process
image
the OS performs a
protection function to
prevent unwanted
interference between
processes with respect to
resources
Scheduling/Execution
∗Processes have two characteristics:
Follows an execution path
that may be interleaved
with other processes
a process has an execution state
(Running, Ready, etc.) and a
dispatching priority and is
scheduled and dispatched by
the OS
Processes and Threads
The unit of dispatching is referred to as a
thread or lightweight process
The unit of resource ownership is referred
to as a process or task
Multithreading - The ability of an OS to
support multiple, concurrent paths of
execution within a single process
Single Threaded Approaches
A single thread of
execution per process,
in which the concept
of a thread is not
recognized, is referred
to as a single-threaded
approach
MS-DOS is an
example
MS-DOS
some variants - UNIX
JRE
Windows, Solaris, modern UNIX, Linux
Processes - Multithreaded environment
In multi-threaded env. Process is defined as :
unit of resource allocation and a unit of protection
Associated items with processes:
virtual address space that holds the process image
Protected access to:
processors
other processes
files
I/O resources
One or More Threads in a Process
• an execution state (Running, Ready, etc.)
• saved thread context (program counter) when not running
• an execution stack
• some per-thread static storage for local variables
• access to it’s process memory and resources (all threads of a process share this)
Each thread has:
Threads vs. Processes
• Thread control block: register values, priority, thread-related state info.• All threads shares : PCB, process address space, access to the same data.• What happens when one thread alters data in memory?• What happens when one thread opens a file with read privileges?
Benefits of Threads
Takes less time to create a new thread
than a process
Less time to terminate a
thread than a process
Switching between two threads takes
less time than switching between
processes
Threads enhance efficiency in
communication between programs
Because threads within same process share memory and files, they can communicate with each other without invoking the kernel.
Uses of Thread in a
Single-User Multiprocessing System
Foreground and background work one thread could display menus and read user input, while another thread
executes user commands and updates the spreadsheet.
Asynchronous processing word processor to write its random access memory (RAM) buffer to disk once
every minute.
Speed of execution process can compute one batch of data while reading the next batch from a
device.
Modular program structure easier to design and implement using threads.
Most of the state information dealing with execution is maintained in thread-level data structures
In an OS that supports threads, scheduling and dispatching is done on a thread basis
suspending a process involves suspending all threads of the process termination of a process terminates all threads within the process
The key states for
a thread are:
Running Ready Blocked
Thread operations
associated with a
change in thread
state are:
Spawn Block Unblock Finish
RPC Using Single ThreadScenario: program that performs two remote procedure calls (RPCs) 2 to two different hosts, using a single thread.
RPC Using One Thread per Server
Scenario: program that performs two remote procedure calls (RPCs) 2 to two different hosts, using a multiple threads.
Multithreading on a Uniprocessor
On a uniprocessor, multiprogramming enables the interleaving of multiplethreads within multiple processes.
Scenario: three threads in two processes are interleaved on the processor.
Thread Synchronization
It is necessary to synchronize the activities
of the various threads
all threads of a process share the same
address space and other resources
any alteration of a resource by one
thread affects the other threads in the
same process
Types of Threads
User Level Thread (ULT)
Kernel level Thread (KLT)
User-Level Threads (ULTs)
All thread
management is
done by the
application
The kernel is not
aware of the
existence of
threads
Relationships Between ULT
States and Process States
Figure 4.6 Examples of the Relationships between User-Level Thread States and Process States
Thread switching does not require kernel mode privileges
Scheduling can be application specific
ULTs can run on any OS
Disadvantages of ULTs
In a typical OS many system calls are blocking
as a result, when a ULT executes a system
call, not only is that thread blocked, but all
of the threads within the process are blocked
In a pure ULT strategy, a multithreaded
application cannot take advantage of
multiprocessing (multi-processor or multi-core)
Reason: A kernel assigns one process to only one processor at a time
Overcoming ULT Disadvantages
(1) Jacketing -- converts a blocking system call into a non-blocking system call
(2) Writing an application as multiple processes rather than multiple threads
Kernel-Level Threads (KLTs)
Thread management is
done by the kernel
No thread
management is done
by the application
API to kernel thread
facility
Disadvantage of KLTsThe transfer of control from one thread to another
within the same process requires a mode switch to
the kernel
Combined Approaches
Thread creation is done in
the user space
Bulk of scheduling and
synchronization of threads
is by the application
Solaris is an example
//Ex1: 2 threads displaying 2 strings “Hello” & “How are you?” independent of each other.
#include <stdio.h>#include <pthread.h>#include <stdlib.h>void * thread1() {
while(1){printf("Hello!!\n");
}}
void * thread2() {while(1){
printf("How are you?\n");}
}int main() {
int status;pthread_t tid1,tid2;pthread_create(&tid1,NULL,thread1,NULL);pthread_create(&tid2,NULL,thread2,NULL);pthread_join(tid1,NULL);pthread_join(tid2,NULL);return 0;
}
On running, you can see many interleaved “Hello!!” and “How are you?” messages
/*Example 2This example involves a reader and a writer thread. The reader thread reads a string from the user and writer //thread displays it. This program uses semaphore so as to achieve synchronization */
#include <stdio.h>#include <pthread.h>#include <semaphore.h>#include <stdlib.h> char n[1024];sem_t len;
void * read1() {while(1){
printf("Enter a string");scanf("%s",n);sem_post(&len);
}}
void * write1() {while(1){
sem_wait(&len);printf("The string entered is :");printf("==== %s\n",n);
}}
int main() {int status;pthread_t tr, tw;pthread_create(&tr,NULL,read1,NULL);pthread_create(&tw,NULL,write1,NULL);pthread_join(tr,NULL);pthread_join(tw,NULL);return 0;}
But suppose we insert a sleep function() in write1 likevoid * write1(){
while(1){sleep(5);sem_wait(&len);printf("The string entered is :");printf("==== %s\n",n);
}}
So we may need to use the condition variables to achieve serial read and write.
The thread 1 may read one more string and thread2 displays the last read string. That is no serial read and write is achieved.
Ex3:Involves a reader and a writer thread. Reader thread reads a string from user & writer thread displays it. It uses condition variables to achieve synchronization serial programming.#include <stdio.h>#include <pthread.h>#include <semaphore.h>#include <stdlib.h>#define TRUE 1#define FALSE 0char n[1024];pthread_mutex_t lock=
PTHREAD_MUTEX_INITIALIZER;int string_read=FALSE;pthread_cond_t cond;void * read1() {
while(1){while(string_read);pthread_mutex_lock(&lock);printf("Enter a string: ");scanf("%s",n);string_read=TRUE;pthread_mutex_unlock(&lock);pthread_cond_signal(&cond);
}}void * write1() {while(1){
pthread_mutex_lock(&lock);while(!string_read)pthread_cond_wait(&cond,&lock);printf("The string entered is %s\n",n);string_read=FALSE;pthread_mutex_unlock(&lock);}
}int main() {int status;pthread_t tr, tw;pthread_create(&tr,NULL,read1,NULL);pthread_create(&tw,NULL,write1,NULL);pthread_join(tr,NULL);pthread_join(tw,NULL);return 0;}
The output is serial read and write.
Multicore and Multithreding
Performance of software on multicore:
If only 10% of the code is inherently serial (f = 0.9) , running the program on a multicore system with eight processors yields a performance gain of only a factor of 4.7
Applications That Benefit
Multithreaded native applications characterized by having a small number of highly threaded
processes
Multiprocess applications characterized by the presence of many single-threaded
processes
Java applications
Multiinstance applications multiple instances of the application in parallel