1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man.

Slides:



Advertisements
Similar presentations
Processes and Threads Chapter 3 and 4 Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
Advertisements

Chap 4 Multithreaded Programming. Thread A thread is a basic unit of CPU utilization It comprises a thread ID, a program counter, a register set and a.
Chapter 4: Multithreaded Programming
Day 10 Threads. Threads and Processes  Process is seen as two entities Unit of resource allocation (process or task) Unit of dispatch or scheduling (thread.
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
Processes CSCI 444/544 Operating Systems Fall 2008.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Threads - Definition - Advantages using Threads - User and Kernel Threads - Multithreading Models - Java and Solaris Threads - Examples - Definition -
3.5 Interprocess Communication Many operating systems provide mechanisms for interprocess communication (IPC) –Processes must communicate with one another.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
Based on Silberschatz, Galvin and Gagne  2009 Threads Definition and motivation Multithreading Models Threading Issues Examples.
3.5 Interprocess Communication
Threads CSCI 444/544 Operating Systems Fall 2008.
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Process Concept An operating system executes a variety of programs
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
ThreadsThreads operating systems. ThreadsThreads A Thread, or thread of execution, is the sequence of instructions being executed. A process may have.
Chapter 51 Threads Chapter 5. 2 Process Characteristics  Concept of Process has two facets.  A Process is: A Unit of resource ownership:  a virtual.
Processes Part I Processes & Threads* *Referred to slides by Dr. Sanjeev Setia at George Mason University Chapter 3.
Chapter 4: Threads Adapted to COP4610 by Robert van Engelen.
9/13/20151 Threads ICS 240: Operating Systems –William Albritton Information and Computer Sciences Department at Leeward Community College –Original slides.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
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 4: Threads. 4.2CSCI 380 Operating Systems Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Multithreading Allows application to split itself into multiple “threads” of execution (“threads of execution”). OS support for creating threads, terminating.
Cosc 4740 Chapter 4 & 5 Threads & Scheduling. Motivation Threads run within application (process) Multiple tasks with the application can be implemented.
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.
Process Management. Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Examples of IPC Systems Communication.
Unix Pipes Pipe sets up communication channel between two (related) processes. 37 Two processes connected by a pipe.
Threads, Thread management & Resource Management.
Processes and Threads CS550 Operating Systems. Processes and Threads These exist only at execution time They have fast state changes -> in memory and.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 13 Threads Read Ch 5.1.
Threads G.Anuradha (Reference : William Stallings)
Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.
Source: Operating System Concepts by Silberschatz, Galvin and Gagne.
CS333 Intro to Operating Systems Jonathan Walpole.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Chapter 4: Threads. 2 Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads.
PREPARED BY : MAZHAR JAVED AWAN BSCS-III Process Communication & Threads 4 December 2015.
Lecture 5: Threads process as a unit of scheduling and a unit of resource allocation processes vs. threads what to program with threads why use threads.
Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.
Chapter 4: Multithreaded Programming. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts What is Thread “Thread is a part of a program.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Department of Computer Science and Software Engineering
Processes, Threads, and Process States. Programs and Processes  Program: an executable file (before/after compilation)  Process: an instance of a program.
CS307 Operating Systems Threads Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2011.
1 OS Review Processes and Threads Chi Zhang
Threads. Thread A basic unit of CPU utilization. An Abstract data type representing an independent flow of control within a process A traditional (or.
Operating System Concepts
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.
Lecturer 3: Processes multithreaded Operating System Concepts Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess.
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
Threads prepared and instructed by Shmuel Wimer Eng. Faculty, Bar-Ilan University 1July 2016Processes.
Processes and Threads Chapter 3 and 4 Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
Protection of System Resources
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
Process Management Presented By Aditya Gupta Assistant Professor
Chapter 4 Threads.
Processes in Unix, Linux, and Windows
Threads & multithreading
Processes in Unix, Linux, and Windows
Processes in Unix, Linux, and Windows
Processes in Unix, Linux, and Windows
Outline Chapter 3: Processes Chapter 4: Threads So far - Next -
Threads CSE 2431: Introduction to Operating Systems
Presentation transcript:

1 Tuesday, June 13, Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man page 4 - Brett L. Huber

2 Announcement §Assignment 1 is up on the website.

3 Output? int main(int argc, char** argv) { int session = 0; char str[] = "a = "; char ptr[] = "x = "; printf("Start.\n"); ++session; fork(); printf("%s %i. \n", str, session); fork(); printf("%s %i. \n", ptr, session); }

4 Interprocess Communication Pipes Shared Memory Message Queues Sockets

5 Pipes cat file1 file2 | sort §Only processes with common ancestor §One-way communication §Data write order What is write end of pipe connected to? What is read end of pipe connected to?

6 Pipes pipe() system call Takes an array of 2 integers int pipesfd[2]; int ret=pipe(pipesfd); if (ret==-1){ perror(“pipe”); exit(1); }

7 Pipes pipesfd[0] contains the read file descriptor pipesfd[1] contains the write file descriptor

8 int main(int argc, char* argv[]){ int data_pipe[2]; int pid; int rc; rc = pipe(data_pipe); if (rc == -1) { perror("pipe"); exit(1); } pid = fork(); switch (pid) { case -1:/* fork failed. */ perror("fork"); exit(1); case 0:/* inside child process. */ do_child(data_pipe); default:/* inside parent process. */ do_parent(data_pipe); } return 0; }

9 void do_parent(int data_pipe[]){ char ch; int rc; close(data_pipe[0]); while ((ch = getchar()) > 0) { /* write the character to the pipe. */ rc = write(data_pipe[1], &ch, 1); if (rc == -1) { perror("Parent: write"); close(data_pipe[1]); exit(1); } close(data_pipe[1]); exit(0); }

10 void do_child(int data_pipe[]) { char ch;/*data received from parent*/ int rc;/* return status of read(). */ close(data_pipe[1]); while ((rc = read(data_pipe[0], &ch, 1)) > 0) { putchar(ch); } close(data_pipe[0]); exit(0); }

11 §Two way communication...

12 Named Pipes §Limitation of anonymous pipes: related processes §IPC in Unrelated Processes? §Named Pipe or FIFO §If file opened for reading, it is blocked until another process opens it for writing. Named pipes use between two processes on same system.

13 Named Pipes § mknod sample p §prw-r--r--

14 int main(int argc, char* argv[]) { FILE* pfile; while (1) { pfile = fopen("/export/home/sample", "w"); if (!pfile) { perror("fopen"); exit(1); } fprintf(pfile, "hello from write process"); fclose(pfile); sleep(1); } return 0; } writefifo.c

15 int main(int argc, char* argv[]){ char buf[200]; FILE* pfile; while (1) { pfile = fopen("/export/home/sample", "r"); if (!pfile) { perror("fopen"); exit(1); } fgets(buf, 200, pfile); printf("%s\n", buf); sleep(1); } return 0; } readfifo.c

16 Run the previous as separate processes … Another way of making named pipes: int mkfifo(const char *path, mode_t mode); Upon successful completion, 0 is returned. Otherwise -1 is returned and errno is set to indicate the error.

17 Message Queues §With pipes l Parse bytes l FIFO §Message queues can be public or private l “Message based” l Several processes may read / write to a queue l Messages may be read by type

18 Sockets

19 Threads §Traditionally a process has an address space and a single thread of execution Process: thread + address space l Keeps track of what to execute next l Registers hold current working variables l It has a stack (execution history) §Threads allow multiple executions to take place in the same process environment

20 (a) Three processes each with one thread (b) One process with three threads Environment (resource) execution THREADS

21 Threads §Multiple threads is analogous to multiple processes running in parallel §Light weight process vs Heavy weight process §Threads share l an address space l open files l other resources

22 §Threads in the same process share resources §Each thread executes separately THREADS

23 §Threads can be in one of the following states: l Running, l Blocked l Ready l Terminated

24 §Thread id §Each thread has its own stack. Why?

25 §Thread id §Each thread has its own stack. Why? l Threads may resume out of order: Different execution history Cannot use a single LIFO stack to save state

26 THREADS

27 Motivation §Spread sheets §Large database access §Spell check, grammar check §Servers §etc.

28 Benefits §Responsiveness Substantial computing + substantial I/O leads to overlap §Resource Sharing §Economy Process creation is time consuming. l Solaris Creating a process is 30 times slower (In some system 100 times faster) Context switch is 5 times slower §Utilization of MP Architectures

29 §Ability of threads to share a set of resources, so that they can work closely together

30 Thread Usage: word processor

31 Thread Usage: word processor Closely collaborate with each other Threads unavoidable. Programming model simpler

32 Single threaded web server

33 Thread Usage: Web Server

34 §Multithreaded kernels l Solaris has a set of threads for interrupt handling

35 Real OS permutations

36 Threads §Not independent as processes §Same address space §Share same global variables §Can read, write another thread’s stack §No protection between threads: l What if a thread runs endlessly? l Necessary?

37 Threads in user space l Threads package entirely in user space l Kernel thinks it is managing a traditional single threaded process l Private thread table + scheduling

38 A user-level threads package

39 Threads in user space l If a thread has to wait for another thread scheduler can choose another ready thread to run l Thread yield l No trap needed l No context switch etc l Scheduler is just like a local procedure and much faster than making a kernel call

40 Threads in user space l Customized scheduling algorithms

41 Threads in user space l However, there are disadvantages as well…

42 Threads in user space l Blocking system calls? Read from keyboard – (e.g. in our word processor example) Disk read l No clock interrupts l Multithreaded webserver : threads often block

43 Many-to-One §Many User-Level Threads Mapped to Single Kernel Thread. §Used on Systems That Do Not Support Kernel Threads.

44 Many-to-one Model

45 Threads in Kernel l When a thread wants to create a new thread or terminate an existing one – it makes a kernel call l Kernel thread table Registers State Etc

46 Threads in Kernel l When thread blocks the kernel can select from same process or different one l Overhead of creation etc is larger

47 One-to-One §Each User-Level Thread Maps to Kernel Thread. §Examples §Windows 95/98/NT/XP §Linux § Solaris 9

48 One-to-one Model

49 Many-to-many Model Example - Version older than Solaris 9

50 Hybrid Implementations Multiplexing user-level threads onto kernel- level threads

51 §Process creation is time consuming

52 §Library procedures to: l Create threads l Exit when finished l Wait for another thread l Yield – voluntarily give up CPU