SlideShare a Scribd company logo
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…

More Related Content

What's hot

Disk scheduling
Disk schedulingDisk scheduling
Disk scheduling
NEERAJ BAGHEL
 
Sorting
SortingSorting
Android activity lifecycle
Android activity lifecycleAndroid activity lifecycle
Android activity lifecycle
Soham Patel
 
Evolution of os
Evolution of osEvolution of os
Evolution of os
janani thirupathi
 
Memory Management in OS
Memory Management in OSMemory Management in OS
Memory Management in OS
vampugani
 
Operating system services 9
Operating system services 9Operating system services 9
Operating system services 9myrajendra
 
Chapter 2 - Operating System Structures
Chapter 2 - Operating System StructuresChapter 2 - Operating System Structures
Chapter 2 - Operating System StructuresWayne Jones Jnr
 
Files in java
Files in javaFiles in java
Threads (operating System)
Threads (operating System)Threads (operating System)
Threads (operating System)
Prakhar Maurya
 
OOP Introduction with java programming language
OOP Introduction with java programming languageOOP Introduction with java programming language
OOP Introduction with java programming language
Md.Al-imran Roton
 
Input output organization
Input output organizationInput output organization
Input output organizationabdulugc
 
Interface in java
Interface in javaInterface in java
Interface in java
PhD Research Scholar
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
Vineeta Garg
 
Process management os concept
Process management os conceptProcess management os concept
Process management os concept
priyadeosarkar91
 
OS - Process Concepts
OS - Process ConceptsOS - Process Concepts
OS - Process Concepts
Mukesh Chinta
 
30326851 -operating-system-unit-1-ppt
30326851 -operating-system-unit-1-ppt30326851 -operating-system-unit-1-ppt
30326851 -operating-system-unit-1-ppt
raj732723
 
Dma transfer
Dma transferDma transfer
Dma transfergmnithya
 
Daa notes 1
Daa notes 1Daa notes 1
Daa notes 1
smruti sarangi
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in JavaJin Castor
 
Os Threads
Os ThreadsOs Threads
Os Threads
Salman Memon
 

What's hot (20)

Disk scheduling
Disk schedulingDisk scheduling
Disk scheduling
 
Sorting
SortingSorting
Sorting
 
Android activity lifecycle
Android activity lifecycleAndroid activity lifecycle
Android activity lifecycle
 
Evolution of os
Evolution of osEvolution of os
Evolution of os
 
Memory Management in OS
Memory Management in OSMemory Management in OS
Memory Management in OS
 
Operating system services 9
Operating system services 9Operating system services 9
Operating system services 9
 
Chapter 2 - Operating System Structures
Chapter 2 - Operating System StructuresChapter 2 - Operating System Structures
Chapter 2 - Operating System Structures
 
Files in java
Files in javaFiles in java
Files in java
 
Threads (operating System)
Threads (operating System)Threads (operating System)
Threads (operating System)
 
OOP Introduction with java programming language
OOP Introduction with java programming languageOOP Introduction with java programming language
OOP Introduction with java programming language
 
Input output organization
Input output organizationInput output organization
Input output organization
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Process management os concept
Process management os conceptProcess management os concept
Process management os concept
 
OS - Process Concepts
OS - Process ConceptsOS - Process Concepts
OS - Process Concepts
 
30326851 -operating-system-unit-1-ppt
30326851 -operating-system-unit-1-ppt30326851 -operating-system-unit-1-ppt
30326851 -operating-system-unit-1-ppt
 
Dma transfer
Dma transferDma transfer
Dma transfer
 
Daa notes 1
Daa notes 1Daa notes 1
Daa notes 1
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
 
Os Threads
Os ThreadsOs Threads
Os Threads
 

Similar to Operating System Chapter 4 Multithreaded programming

OS Module-2.pptx
OS Module-2.pptxOS Module-2.pptx
OS Module-2.pptx
bleh23
 
CH04.pdf
CH04.pdfCH04.pdf
CH04.pdf
ImranKhan880955
 
Os
OsOs
Sucet os module_2_notes
Sucet os module_2_notesSucet os module_2_notes
Sucet os module_2_notes
SRINIVASUNIVERSITYEN
 
process and thread.pptx
process and thread.pptxprocess and thread.pptx
process and thread.pptx
HamzaxTv
 
P-Threads
P-ThreadsP-Threads
P-Threads
Bharathwaj_v
 
Operating System 4 1193308760782240 2
Operating System 4 1193308760782240 2Operating System 4 1193308760782240 2
Operating System 4 1193308760782240 2mona_hakmy
 
Operating System 4
Operating System 4Operating System 4
Operating System 4tech2click
 
Multithreaded Programming in oprating system
Multithreaded Programming in oprating  systemMultithreaded Programming in oprating  system
Multithreaded Programming in oprating system
YOGENDRAMS
 
Multithreading
MultithreadingMultithreading
Multithreadingbackdoor
 
Intro To .Net Threads
Intro To .Net ThreadsIntro To .Net Threads
Intro To .Net Threads
rchakra
 
Topic 4- processes.pptx
Topic 4- processes.pptxTopic 4- processes.pptx
Topic 4- processes.pptx
DanishMahmood23
 
WEEK07operatingsystemdepartmentofsoftwareengineering.pptx
WEEK07operatingsystemdepartmentofsoftwareengineering.pptxWEEK07operatingsystemdepartmentofsoftwareengineering.pptx
WEEK07operatingsystemdepartmentofsoftwareengineering.pptx
babayaga920391
 
Introto netthreads-090906214344-phpapp01
Introto netthreads-090906214344-phpapp01Introto netthreads-090906214344-phpapp01
Introto netthreads-090906214344-phpapp01Aravindharamanan S
 
Posix threads(asha)
Posix threads(asha)Posix threads(asha)
Posix threads(asha)Nagarajan
 
Chapter 3 chapter reading task
Chapter 3 chapter reading taskChapter 3 chapter reading task
Chapter 3 chapter reading task
Grievous Humorist-Ilham
 
Networking threads
Networking threadsNetworking threads
Networking threads
Nilesh Pawar
 
Multi threaded programming
Multi threaded programmingMulti threaded programming
Multi threaded programming
AnyapuPranav
 

Similar to 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
 
Multithreaded Programming in oprating system
Multithreaded Programming in oprating  systemMultithreaded Programming in oprating  system
Multithreaded Programming in oprating system
 
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
 

Recently uploaded

Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 

Recently uploaded (20)

Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 

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.