SlideShare uma empresa Scribd logo
1 de 41
MULTI-THREADED  PROGRAMMING Reported by: N ikko  D elumen M ary  J oy  T apar
4.1 OVERVIEW Thread  – basic unit of CPU utilization; it comprises a thread ID, a program counter, a register set, and a stack.
[object Object],[object Object],[object Object]
thread thread Single-threaded process Multi-threaded process
[object Object],[object Object],[object Object]
[object Object],[object Object]
[object Object],[object Object]
[object Object]
[object Object],[object Object]
4.2.1 Many-to-One Model User thread Kernel thread
4.2.2 One-to-One Model User thread Kernel thread
Two-level Model User thread Kernel thread
4.2.3 Many-to-Many Model User thread Kernel thread
4.3 THREAD LIBRARIES A  Thread Library  provides the programmer an API for creating and managing threads.
[object Object],[object Object],[object Object]
4.3.1 PTHREADS Pthreads  refers to the POSIX standard (IEEE 1003.1c) defining an API for thread creation and synchronization.
#include <pthread.h> #include <stdio.h> int sum; void *runner(void *param); int main(int argc, char *argv[]) { pthread.t tid; pthread_attr_t attr; if (argc !=2){ fprintf(stderr,”usage: a.out(integer value>”); return -1; } if (atoi (argv[1]) < 0) { fprintf(stderr,”%d must be >= 0”, atoi (argv[1])); return -1; } pthread_attr_init(&attr); pthread_create (&tid, &attr, runner, argv[1]); pthread_join (tid,NULL); printf(“sum=%d”, sum); } void *runner(void *param) { int i, upper = atoi(param) sum = 0; for (i=1; i<=upper; i++) sum += I; pthread_exit (0); }
4.3.2 WIN32 THREADS The technique of creating threads using Win32 thread library is similar to the Pthreads technique in several ways.
#include <windows.h> #include <stdio.h> DWORD Sum; DWORD WINAPI Summation(LPVOID Param) { DWORD UPPER = *(DWORD*) Param; for (DWORD I = 0; I <= Upper; i++) Sum += I; return 0; } int main (int argc, char *argv[]) { DWORD ThreadId; HANDLE ThreadHandle; int Param; if (argc !=2){ fprintf(stderr,”An integer parameter is required”); return – 1; } Param=atoi(argv[1]); if (Param<0){ fprintf(stderr,”An integer >= 0 is required); return – 1; }
ThreadHandle=CreateThread( NULL, 0, Summation, &Param, 0, &ThreadId); if (ThreadHandle != NULL){ WaitForSingleObject(ThreadHandle, INFINITE); CloseHandle(ThreadHandle); printf(“sum = %d”,Sum); } }
4.3.3 JAVA THREADS Threads are fundamental model of program execution in a Java program, and the Java language and it’s API provide a rich set of features for the creation and management of threads.
class Sum { private int sum; public int getSum() { return sum; } public void setSum (int sum) { this.sum = sum; } } class Summation implements Runnable { private int upper; private Sum sumValue; public Summation (int upper, Sum sumValue) { this.upper = upper; this.sumValue = sumValue } public void run() { int sum = 0; for (int i=0; i<= upper; i++) sum+=I; sumValue.setSum(sum); } }
public class Driver { public static void main(String[]args){ if(args.length >0){ if (Integer.parseInt (args[0])<0) System.err.printIn(args[0]+” must be >= 0.”); else{ Sum sumObject = new Sum(); int upper = Integer.parseInt(args[0]); Thread thrd = new Thread(new Summation (upper, sumObject)); thrd.start(); try { thrd.join(); System.out.printIn (“The sum of “+upper+” is “+sumObject.getSum()); } catch (InterruptedException ie) { } } } else System.err.println(“Usage: Summation <integer value>”); } }
4.4.3 THREADING ISSUES In this section, we discuss some of the issues to consider with multithreaded programs.
[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object]
4.4.4 THREAD POOLS Limits the number of threads that exist at anyone point. This is particularly important on systems that cannot support a large number of concurrent threads. Example: DWORD WINAPI PoolFunction (AVOID Param) { /** * this function runs as a separate thread. **/ }
[object Object],[object Object],[object Object],[object Object]
4.4.5 THREAD-SPECIFIC DATA In a transaction-processing system, we might service each transaction in a separate thread. User thread Lightweight  process Kernel thread Figure 4.9  Lightweight process (LWP) LWP  k
[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object]
Some of these flags are listed below: The set of open file is shared.  CLONE_FILES Signal handlers are shared. CLONE_SIGHAND The same memory space is shared. CLONE_VM File-system information is shared. CLONE_FS MEANING FLAG
[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object]
THE END…

Mais conteúdo relacionado

Mais procurados

Operating system.ppt (1)
Operating system.ppt (1)Operating system.ppt (1)
Operating system.ppt (1)
Vaibhav Bajaj
 
Process management in os
Process management in osProcess management in os
Process management in os
Miong Lazaro
 
Multithreading models.ppt
Multithreading models.pptMultithreading models.ppt
Multithreading models.ppt
Luis Goldster
 
Secondary storage management in os
Secondary storage management in osSecondary storage management in os
Secondary storage management in os
Sumant Diwakar
 

Mais procurados (20)

Threads (operating System)
Threads (operating System)Threads (operating System)
Threads (operating System)
 
Os Swapping, Paging, Segmentation and Virtual Memory
Os Swapping, Paging, Segmentation and Virtual MemoryOs Swapping, Paging, Segmentation and Virtual Memory
Os Swapping, Paging, Segmentation and Virtual Memory
 
Deadlock ppt
Deadlock ppt Deadlock ppt
Deadlock ppt
 
Unix ppt
Unix pptUnix ppt
Unix ppt
 
Disk Scheduling Algorithm in Operating System
Disk Scheduling Algorithm in Operating SystemDisk Scheduling Algorithm in Operating System
Disk Scheduling Algorithm in Operating System
 
Unix OS & Commands
Unix OS & CommandsUnix OS & Commands
Unix OS & Commands
 
NFS(Network File System)
NFS(Network File System)NFS(Network File System)
NFS(Network File System)
 
Context free grammar
Context free grammar Context free grammar
Context free grammar
 
Multithreading
MultithreadingMultithreading
Multithreading
 
System call
System callSystem call
System call
 
IPC
IPCIPC
IPC
 
Operating system.ppt (1)
Operating system.ppt (1)Operating system.ppt (1)
Operating system.ppt (1)
 
Demand paging
Demand pagingDemand paging
Demand paging
 
Linux commands and file structure
Linux commands and file structureLinux commands and file structure
Linux commands and file structure
 
Process management in os
Process management in osProcess management in os
Process management in os
 
Multithreading models.ppt
Multithreading models.pptMultithreading models.ppt
Multithreading models.ppt
 
Multithreading
Multithreading Multithreading
Multithreading
 
Secondary storage management in os
Secondary storage management in osSecondary storage management in os
Secondary storage management in os
 
Linux commands
Linux commandsLinux commands
Linux commands
 
Concurrency
ConcurrencyConcurrency
Concurrency
 

Semelhante a Operating System Chapter 4 Multithreaded programming

OS Module-2.pptx
OS Module-2.pptxOS Module-2.pptx
OS Module-2.pptx
bleh23
 
Operating System 4 1193308760782240 2
Operating System 4 1193308760782240 2Operating System 4 1193308760782240 2
Operating System 4 1193308760782240 2
mona_hakmy
 
Operating System 4
Operating System 4Operating System 4
Operating System 4
tech2click
 
Multithreading
MultithreadingMultithreading
Multithreading
backdoor
 
WEEK07operatingsystemdepartmentofsoftwareengineering.pptx
WEEK07operatingsystemdepartmentofsoftwareengineering.pptxWEEK07operatingsystemdepartmentofsoftwareengineering.pptx
WEEK07operatingsystemdepartmentofsoftwareengineering.pptx
babayaga920391
 
Introto netthreads-090906214344-phpapp01
Introto netthreads-090906214344-phpapp01Introto netthreads-090906214344-phpapp01
Introto netthreads-090906214344-phpapp01
Aravindharamanan S
 
Posix threads(asha)
Posix threads(asha)Posix threads(asha)
Posix threads(asha)
Nagarajan
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Raghu nath
 

Semelhante a Operating System Chapter 4 Multithreaded programming (20)

OS Module-2.pptx
OS Module-2.pptxOS Module-2.pptx
OS Module-2.pptx
 
CH04.pdf
CH04.pdfCH04.pdf
CH04.pdf
 
Threads
ThreadsThreads
Threads
 
Os
OsOs
Os
 
Sucet os module_2_notes
Sucet os module_2_notesSucet os module_2_notes
Sucet os module_2_notes
 
process and thread.pptx
process and thread.pptxprocess and thread.pptx
process and thread.pptx
 
P-Threads
P-ThreadsP-Threads
P-Threads
 
Operating System 4 1193308760782240 2
Operating System 4 1193308760782240 2Operating System 4 1193308760782240 2
Operating System 4 1193308760782240 2
 
Operating System 4
Operating System 4Operating System 4
Operating System 4
 
Chapter 6 os
Chapter 6 osChapter 6 os
Chapter 6 os
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Intro To .Net Threads
Intro To .Net ThreadsIntro To .Net Threads
Intro To .Net Threads
 
Topic 4- processes.pptx
Topic 4- processes.pptxTopic 4- processes.pptx
Topic 4- processes.pptx
 
WEEK07operatingsystemdepartmentofsoftwareengineering.pptx
WEEK07operatingsystemdepartmentofsoftwareengineering.pptxWEEK07operatingsystemdepartmentofsoftwareengineering.pptx
WEEK07operatingsystemdepartmentofsoftwareengineering.pptx
 
Introto netthreads-090906214344-phpapp01
Introto netthreads-090906214344-phpapp01Introto netthreads-090906214344-phpapp01
Introto netthreads-090906214344-phpapp01
 
Posix threads(asha)
Posix threads(asha)Posix threads(asha)
Posix threads(asha)
 
Chapter 3 chapter reading task
Chapter 3 chapter reading taskChapter 3 chapter reading task
Chapter 3 chapter reading task
 
Networking threads
Networking threadsNetworking threads
Networking threads
 
Multi threaded programming
Multi threaded programmingMulti threaded programming
Multi threaded programming
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 

Último

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
SanaAli374401
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 

Último (20)

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 

Operating System Chapter 4 Multithreaded programming

  • 1. MULTI-THREADED PROGRAMMING Reported by: N ikko D elumen M ary J oy T apar
  • 2. 4.1 OVERVIEW Thread – basic unit of CPU utilization; it comprises a thread ID, a program counter, a register set, and a stack.
  • 3.
  • 4. thread thread Single-threaded process Multi-threaded process
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10. 4.2.1 Many-to-One Model User thread Kernel thread
  • 11. 4.2.2 One-to-One Model User thread Kernel thread
  • 12. Two-level Model User thread Kernel thread
  • 13. 4.2.3 Many-to-Many Model User thread Kernel thread
  • 14. 4.3 THREAD LIBRARIES A Thread Library provides the programmer an API for creating and managing threads.
  • 15.
  • 16. 4.3.1 PTHREADS Pthreads refers to the POSIX standard (IEEE 1003.1c) defining an API for thread creation and synchronization.
  • 17. #include <pthread.h> #include <stdio.h> int sum; void *runner(void *param); int main(int argc, char *argv[]) { pthread.t tid; pthread_attr_t attr; if (argc !=2){ fprintf(stderr,”usage: a.out(integer value>”); return -1; } if (atoi (argv[1]) < 0) { fprintf(stderr,”%d must be >= 0”, atoi (argv[1])); return -1; } pthread_attr_init(&attr); pthread_create (&tid, &attr, runner, argv[1]); pthread_join (tid,NULL); printf(“sum=%d”, sum); } void *runner(void *param) { int i, upper = atoi(param) sum = 0; for (i=1; i<=upper; i++) sum += I; pthread_exit (0); }
  • 18. 4.3.2 WIN32 THREADS The technique of creating threads using Win32 thread library is similar to the Pthreads technique in several ways.
  • 19. #include <windows.h> #include <stdio.h> DWORD Sum; DWORD WINAPI Summation(LPVOID Param) { DWORD UPPER = *(DWORD*) Param; for (DWORD I = 0; I <= Upper; i++) Sum += I; return 0; } int main (int argc, char *argv[]) { DWORD ThreadId; HANDLE ThreadHandle; int Param; if (argc !=2){ fprintf(stderr,”An integer parameter is required”); return – 1; } Param=atoi(argv[1]); if (Param<0){ fprintf(stderr,”An integer >= 0 is required); return – 1; }
  • 20. ThreadHandle=CreateThread( NULL, 0, Summation, &Param, 0, &ThreadId); if (ThreadHandle != NULL){ WaitForSingleObject(ThreadHandle, INFINITE); CloseHandle(ThreadHandle); printf(“sum = %d”,Sum); } }
  • 21. 4.3.3 JAVA THREADS Threads are fundamental model of program execution in a Java program, and the Java language and it’s API provide a rich set of features for the creation and management of threads.
  • 22. class Sum { private int sum; public int getSum() { return sum; } public void setSum (int sum) { this.sum = sum; } } class Summation implements Runnable { private int upper; private Sum sumValue; public Summation (int upper, Sum sumValue) { this.upper = upper; this.sumValue = sumValue } public void run() { int sum = 0; for (int i=0; i<= upper; i++) sum+=I; sumValue.setSum(sum); } }
  • 23. public class Driver { public static void main(String[]args){ if(args.length >0){ if (Integer.parseInt (args[0])<0) System.err.printIn(args[0]+” must be >= 0.”); else{ Sum sumObject = new Sum(); int upper = Integer.parseInt(args[0]); Thread thrd = new Thread(new Summation (upper, sumObject)); thrd.start(); try { thrd.join(); System.out.printIn (“The sum of “+upper+” is “+sumObject.getSum()); } catch (InterruptedException ie) { } } } else System.err.println(“Usage: Summation <integer value>”); } }
  • 24. 4.4.3 THREADING ISSUES In this section, we discuss some of the issues to consider with multithreaded programs.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30. 4.4.4 THREAD POOLS Limits the number of threads that exist at anyone point. This is particularly important on systems that cannot support a large number of concurrent threads. Example: DWORD WINAPI PoolFunction (AVOID Param) { /** * this function runs as a separate thread. **/ }
  • 31.
  • 32. 4.4.5 THREAD-SPECIFIC DATA In a transaction-processing system, we might service each transaction in a separate thread. User thread Lightweight process Kernel thread Figure 4.9 Lightweight process (LWP) LWP k
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38. Some of these flags are listed below: The set of open file is shared. CLONE_FILES Signal handlers are shared. CLONE_SIGHAND The same memory space is shared. CLONE_VM File-system information is shared. CLONE_FS MEANING FLAG
  • 39.
  • 40.