Share Memory Program Example int array_size=1000 int global_array[array_size] main(argc, argv) { int nprocs=4; m_set_procs(nprocs); /* prepare to launch.

Slides:



Advertisements
Similar presentations
IPC (Interprocess Communication)
Advertisements

Threads Relation to processes Threads exist as subsets of processes Threads share memory and state information within a process Switching between threads.
©2009 Operačné systémy Procesy. 3.2 ©2009 Operačné systémy Process in Memory.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 6: Threads Chapter 4.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues.
1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.
CHAPTER 5 THREADS & MULTITHREADING 1. Single and Multithreaded Processes 2.
Chapter 4: Multithreaded Programming
Chapter 5 Threads os5.
Threads By Dr. Yingwu Zhu. Review Multithreading Models Many-to-one One-to-one Many-to-many.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Threads CSCI 444/544 Operating Systems Fall 2008.
Process Concept n An operating system executes a variety of programs: –Batch system – jobs –Time-shared systems – user programs or tasks n Textbook uses.
02/02/2004CSCI 315 Operating Systems Design1 Interprocesses Communication Notice: The slides for this lecture have been largely based on those accompanying.
Chapter 3: Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 7, 2006 Chapter 3: Processes Process Concept.
02/01/2010CSCI 315 Operating Systems Design1 Interprocess Communication Notice: The slides for this lecture have been largely based on those accompanying.
02/02/2004CSCI 315 Operating Systems Design1 Threads Notice: The slides for this lecture have been largely based on those accompanying the textbook Operating.
1/26/2007CSCI 315 Operating Systems Design1 Processes Notice: The slides for this lecture have been largely based on those accompanying the textbook Operating.
Interprocess Communication. Process Concepts Last class.
Chapter 4: Threads Adapted to COP4610 by Robert van Engelen.
Operating System Principles Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
Chapter 4 Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Thread. A basic unit of CPU utilization. It comprises a thread ID, a program counter, a register set, and a stack. It is a single sequential flow of control.
Chapter 3: Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 3: Processes Process Concept Process Scheduling Operations.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Threads A thread (or lightweight process) is a basic unit of CPU.
Chapter 3: Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Examples of IPC Systems Communication in Client-Server.
Operating System Concepts Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
4.1 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Thread Libraries  Pthreads  Windows.
Silberschatz, Galvin and Gagne  Operating System Concepts Cooperating Processes Independent process cannot affect or be affected by the execution.
Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction.
1 CS.217 Operating System By Ajarn..Sutapart Sappajak,METC,MSIT Chapter 4 Process Slide 1 Chapter 4 Process.
3.1 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Interprocess Communication Processes within a system may be.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
Processes. Chapter 3: Processes Process Concept Process Scheduling Operations on Processes Cooperating Processes Interprocess Communication Communication.
Source: Operating System Concepts by Silberschatz, Galvin and Gagne.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
CS212: OPERATING SYSTEM Lecture 2: Process 1. Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Process-Concept.
Chapter 3 Process Scheduling Bernard Chen Spring 2007.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-4 Process Communication Department of Computer Science and Software.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 10 Processes II Read.
Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Lecture 3 Operating Systems.
PREPARED BY : MAZHAR JAVED AWAN BSCS-III Process Communication & Threads 4 December 2015.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 4: Processes Process Concept Process Scheduling Operations on Processes Cooperating.
Shan Gao Fall 2007 Department of Computer Science Georgia State University.
Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 14 Threads 2 Read Ch.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Outline n Overview n Multithreading.
CS307 Operating Systems Threads Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2011.
Lecture 4: Processes & Threads. Lecture 4 / Page 2AE4B33OSS Silberschatz, Galvin and Gagne ©2005 Contents The concept of Process Process states and life-cycle.
Cooperating Processes The concurrent processes executing in the operating system may be either independent processes or cooperating processes. A process.
 Process Concept  Process Scheduling  Operations on Processes  Cooperating Processes  Interprocess Communication  Communication in Client-Server.
Threads. Thread A basic unit of CPU utilization. An Abstract data type representing an independent flow of control within a process A traditional (or.
4.1 Operating System Chapter 4: Processes Process Concept Process Scheduling Operations on Processes Cooperating Processes Interprocess Communication Communication.
Gokul Kishan CS8 1 Inter-Process Communication (IPC)
CSS430 Threads1 Textbook Ch5 These slides were compiled from the OSC textbook slides (Silberschatz, Galvin, and Gagne) and the instructor’s class materials.
CISC2200 Threads Fall 09. Process  We learn the concept of process  A program in execution  A process owns some resources  A process executes a program.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads  Overview  Multithreading Models  Threading Issues  Pthreads.
Contents 1.Overview 2.Multithreading Model 3.Thread Libraries 4.Threading Issues 5.Operating-system Example 2 OS Lab Sun Suk Kim.
CMSC 421 Spring 2004 Section 0202 Part II: Process Management Chapter 5 Threads.
1 Chapter 5: Threads Overview Multithreading Models & Issues Read Chapter 5 pages
Lecture 5. Example for periority The average waiting time : = 41/5= 8.2.
Chapter 4: Threads.
Processes Overview: Process Concept Process Scheduling
Operating System Concepts
Multithreaded Programming
Typically for using the shared memory the processes should:
Outline Chapter 3: Processes Chapter 4: Threads So far - Next -
Chapter 4: Threads.
Presentation transcript:

Share Memory Program Example int array_size=1000 int global_array[array_size] main(argc, argv) { int nprocs=4; m_set_procs(nprocs); /* prepare to launch this many processes */ m_fork(sum); /* fork out processes */ m_kill_procs(); /* kill activated processes */ } void sum() { int id; id = m_get_myid(); for (i=id*(array_size/nprocs); i<(id+1)*(array_size/nprocs); i++) global_array[id*array_size/nprocs]+=global_array[i]; }

Message-Passing Systems A message passing facility provides at least two operations: send(message), receive(message)

Message-Passing Systems If 2 processes want to communicate, a communication link must exist It has the following variations: 1. Direct or indirect communication 2. Synchronize or asynchronize communication 3. Automatic or explicit buffering

Message-Passing Systems Direct communication send(P, message) receive(Q, message) Properties: A link is established automatically A link is associated with exactly 2 processes Between each pair, there exists exactly one link

Message-Passing Systems Indirect communication: the messages are sent to and received from mailbox send(A, message) receive(A, message)

Message-Passing Systems Properties: A link is established only if both members of the pair have a shared mailbox A link is associated with more than 2 processes Between each pair, there exists a number of links

Message-Passing Systems Mailbox sharing P1, P2, andP3 share mailbox A P1, sends; P2 andP3 receive Who gets the message? Solutions Allow a link to be associated with at most two processes Allow only one process at a time to execute a receive operation Allow the system to select arbitrarily the receiver. Sender is notified who the receiver was.

Message-Passing Systems If the mailbox owned by process, it is easy to tell who is the owner and user. And there is no confuse we send the message and who receives it. When process terminates, the mailbox disappear

Message-Passing Systems If the mailbox owned by OS, it requires the following functions: Create a new mailbox Send and Receive message through the mailbox Delete a mailbox

Message-Passing Systems Synchronization: synchronous and asynchronous Blocking is considered synchronous Blocking send has the sender block until the message is received Blocking receive has the receiver block until a message is available

Message-Passing Systems Non-blockingis considered asynchronous Non-blocking send has the sender send the message and continue Non-blocking receive has the receiver receive a valid message or null

Message-Passing Systems Buffering: Queue of messages attached to the link, there are 3 variations: Zero capacity –0 messages Sender must wait for receiver Bounded capacity –finite length of n messages, sender must wait if link full Unbounded capacity –infinite length Sender never waits

MPI Program example #include "mpi.h" #include int main (int argc, char *argv[]) { int id; /* Process rank */ int p; /* Number of processes */ int i,j; int array_size=100; int array[array_size]; /* or *array and then use malloc or vector to increase the size */ int local_array[array_size/p]; int sum=0; MPI_Status stat; MPI_Comm_rank (MPI_COMM_WORLD, &id); MPI_Comm_size (MPI_COMM_WORLD, &p);

MPI Program example if (id==0) { for(i=0; i<array_size; i++) array[i]=i; /* initialize array*/ for(i=0; i<p; i++) MPI_Send(&array[i*array_size/p], /* Start from*/ array_size/p, /* Message size*/ MPI_INT, /* Data type*/ i, /* Send to which process*/ MPI_COMM_WORLD); } else MPI_Recv(&local_array[0],array_size/p,MPI_INT,0,0,MPI_COMM_WORLD,&stat);

MPI Program example for(i=0;i<array_size/p;i++) sum+=local_array[i]; MPI_Reduce (&sum, &sum, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD); if (id==0) printf("%d ",sum); }

Chapter 4 Threads Bernard Chen Spring 2007

Overview A thread is a basic unit of CPU utilization. Traditional (single-thread) process has only one single thread control Multithreaded process can perform more than one task at a time example: word may have a thread for displaying graphics, another respond for key strokes and a third for performing spelling and grammar checking

Overview Another example is web server: a busy web server may have several clients connected. Past method: when receives a request, create another process (time consuming and resource intensive) Multithreaded method: server create threads to “listen” the request from user. When a request is made, the server create another thread to service the request

Benefits Responsiveness: allow a program to continue running even if part of it is blocked or is performing a lengthy operation Resource sharing: share memory and resources Economy: unlike multi processors, no need to allocate memory and resources Utilization of multiprocessor architectures: multithreading on multi-CPU machine increases concurrency

Multithreading Models Support for threads may be provided either at the user level, for user threads, or by the kernel, for kernel threads User threads are supported above kernel and are managed without kernel support Kernel threads are supported and managed directly by the operating system

Multithreading Models Ultimately, there must exist a relationship between user thread and kernel thread User-level threads are managed by a thread library, and the kernel is unaware of them To run in a CPU, user-level thread must be mapped to an associated kernel-level thread

Many-to-one Model User Threads Kernel thread

Many-to-one Model It maps many user-level threads to one kernel thread Thread management is done by the thread library in user space, so it is efficient But the entire system may block makes a block system call. Besides multiple threads are unable to run in parallel on multiprocessors

One-to-one Model User Threads Kernel threads

One-to-one Model It provide more concurrency than the many- to-one model by allowing another thread to run when a thread makes a blocking system call It allows to run in parallel on multiprocessors The only drawback is that creating a user thread requires creating the corresponding kernel thread Most implementation restrict the number of threads create by user

Many-to-many Model User Threads Kernel threads

Many-to-many Model Multiplexes many user-level threads to a smaller or equal number of kernel threads User can create as many threads as they want When a block system called by a thread, the kernel can schedule another thread for execution

Pthread Example #include int sum; /* global variable*/ void *runner(void *param); int main(int argc, char *argv[]) { pthread_t tid; /* thread id*/ pthread_attr_t attr; /* set of thread attributes*/ pthread_attr_init(&attr); /* get the default attributes*/ pthread_create(&tid,&attr,runner,argv[1]); pthread_join(tid,NULL); printf(“sum=%d \n”,sum); }

Pthread Example void *runner(void *param) { int I; int upper = atoi(param); sum=0; for(i=1;i<=upper;i++) sum+=i; pthread_exit(0); }