SlideShare une entreprise Scribd logo
Mutex & Semaphore
 pthread_create arguments:
 thread: An opaque, unique identifier for the new thread returned by the subroutine.
 attr: An opaque attribute object that may be used to set thread attributes. You can
specify a thread attributes object, or NULL for the default values.
 start_routine: the C routine that the thread will execute once it is created.
 arg: A single argument that may be passed to start_routine. It must be passed by
reference as a pointer cast of type void. NULL may be used if no argument is to be
passed.
 NULL is used for pointers only as it is defined
as (void *) 0. It should not be used other than
pointers. If NULL is assigned to a pointer, then
pointer is pointing to nothing.
 0 (zero) is a value.
 NULL is a pointer. It’s a constant, made available through stdio.h.
 Whenever you see the NULL constant used, it’s as a pointer value. It can also be used to
typecast pointers, such as (int *)NULL.
 A pointer is a variable that holds a memory location.
 Zero is a value. It’s not a memory location, as only pointers can hold memory locations.
Further, pointers hold memory locations that reference something.
 Perhaps the element that causes the most confusion is the 0 escape character. That’s the
null character, which is used in C to terminate a string of text. The problem is that 0
translates into character value zero. In fact, you can use a zero directly (if you know how)
and it has the same effect. But that doesn’t mean that zero and NULL are the same thing,
especially given that NULL is one use of the word and “null character” is another.
 The int value 0 is 4 bytes long (on my
computer), which helps confirm it’s an int. But
the NULL value is 8 bytes long, which is the
size of an address.
 Mutex variables must be declared with type pthread_mutex_t, and must be
initialized before they can be used. There are two ways to initialize a mutex
variable:
 Statically, when it is declared. For example: pthread_mutex_t mymutex =
PTHREAD_MUTEX_INITIALIZER;
 Dynamically, with the pthread_mutex_init() routine. This method permits
setting mutex object attributes, attr.
 A mutex (named for "mutual exclusion") is a
binary semaphore with an ownership restriction:
it can be unlocked (the post operation) only by
whoever locked it (the wait operation). Thus a
mutex offers a somewhat stronger protection
than an ordinary semaphore.
 Un « Mutex » est un sémaphore binaire pouvant prendre un des deux états
 "lock" (verrouillé) ou "unlock" (déverrouillé): valeur de sémaphore 1 ou 0
 Un « Mutex » ne peut être partagé que par des threads d’un même
processus
 Un « Mutex » ne peut être verrouillé que par une seule thread à la fois.
 Une thread qui tente de verrouiller un « Mutex » déjà verrouillé est suspendu
jusqu'à ce que le « Mutex » soit déverrouillé.
 Un mutex est une variable de type « thread_mutex_t »
 Il existe une constante PTHREAD_MUTEX_INITIALIZER de ce type
 permettant une déclaration avec initialisation statique du mutex (avec les valeursde
comportement par défaut)
 pthread_mutex_t monMutex = THREAD_MUTEX_INITIALIZER;
 Un mutex peut également être initialisé par un appel de la primitive
 nt pthread_mutex_init(pthread_mutex_t *mutex,const pthread_mutexattr_t *mutexattr);
 vec une initialisation par défaut lorsque mutexattr vaut NULL
 ex : pthread_mutex_init(&monMutex, NULL);
 Un mutex peut être verrouillé par la primitive
 int pthread_mutex_lock(pthread_mutex_t *mutex);
 Si le mutex est déverrouillé il devient verrouillé
 Si le mutex est déjà verrouillé par une autre thread
la tentative de verrouillage suspend l'appelant
jusqu'à ce que le mutex soit déverrouillé.
 Un mutex peut être déverrouillé par la primitive
 int pthread_mutex_unlock(pthread_mutex_t *mutex);
 Si le mutex est déjà déverrouillé, cet appel n'a aucun effet (comportement
par
 défaut)
 Si le mutex est verrouillé, une des threads en attente obtient le mutex (qui
 reprend alors l'état verrouillé) et cette thread redevient active (elle n'est plus
 bloquée)
 L'opération est toujours non bloquante pour l'appelant
 Un sémaphore s :
 {Val(s): valeur qui doit toujours être initialisée,
 File(s): file d’attente qui va contenir les processus bloqués sur ce sémaphore }
 La valeur initiale d’un sémaphore ne doit jamais être négative.
 Primitive P(s):
 Debut
 Val(s) = Val(s) -1;
 Si Val(s) < 0 Alors Mettre le processus actif dans la file File(s);
 Fin
 Primitive V(s):
 Debut
 Val(s) = Val(s) +1;
 Si Val(s) <=0 Alors /* il y a au moins un processus bloqué dans File(s) */
 Les prototypes des fonctions et les types sont définis dans « semaphore.h »
 Un sémaphore est une variable de type « sem_t »
 Il est initialisé par un appel à la primitive :
 int sem_init(sem_t *sem, int pshared, unsigned int valeur);
 – si "phsahed" vaut 0 le sémaphore ne peut pas être partagé entre threads
de différents processus (partage uniquement au sein d'un même processus)
 – valeur définit la valeur initiale de ce sémaphore (positive ou nulle)
 Les deux opérations P et V sont implémentées par
 P : int sem_wait(sem_t *sem);
 V : int sem_post(sem_t *sem);
 avec les mêmes comportements que les primitives génériques P et V
 Il existe également une version non bloquante de la primitive P :
 int sem_trywait(sem_t *sem);
 qui retourne 0 lorsque la prise est possible (et non bloquante) et qui retourne
 EAGAIN sinon (dans le cas où l'appel normal serait bloquant)
Chapitre_6.pptx Système d'exploitation 2
Chapitre_6.pptx Système d'exploitation 2
Chapitre_6.pptx Système d'exploitation 2
Chapitre_6.pptx Système d'exploitation 2

Contenu connexe

Dernier

Ouvrez la porte ou prenez un mur (Agile Tour Genève 2024)
Ouvrez la porte ou prenez un mur (Agile Tour Genève 2024)Ouvrez la porte ou prenez un mur (Agile Tour Genève 2024)
Ouvrez la porte ou prenez un mur (Agile Tour Genève 2024)
Laurent Speyser
 
Le support de présentation des Signaux 2024
Le support de présentation des Signaux 2024Le support de présentation des Signaux 2024
Le support de présentation des Signaux 2024
UNITECBordeaux
 
De l'IA comme plagiat à la rédaction d'une « charte IA » à l'université
De l'IA comme plagiat à la rédaction d'une « charte IA » à l'universitéDe l'IA comme plagiat à la rédaction d'une « charte IA » à l'université
De l'IA comme plagiat à la rédaction d'une « charte IA » à l'université
Université de Franche-Comté
 
Le Comptoir OCTO - Qu’apporte l’analyse de cycle de vie lors d’un audit d’éco...
Le Comptoir OCTO - Qu’apporte l’analyse de cycle de vie lors d’un audit d’éco...Le Comptoir OCTO - Qu’apporte l’analyse de cycle de vie lors d’un audit d’éco...
Le Comptoir OCTO - Qu’apporte l’analyse de cycle de vie lors d’un audit d’éco...
OCTO Technology
 
Les écrans informatiques au fil du temps.pptx
Les écrans informatiques au fil du temps.pptxLes écrans informatiques au fil du temps.pptx
Les écrans informatiques au fil du temps.pptx
abderrahimbourimi
 
MongoDB in a scale-up: how to get away from a monolithic hell — MongoDB Paris...
MongoDB in a scale-up: how to get away from a monolithic hell — MongoDB Paris...MongoDB in a scale-up: how to get away from a monolithic hell — MongoDB Paris...
MongoDB in a scale-up: how to get away from a monolithic hell — MongoDB Paris...
Horgix
 

Dernier (6)

Ouvrez la porte ou prenez un mur (Agile Tour Genève 2024)
Ouvrez la porte ou prenez un mur (Agile Tour Genève 2024)Ouvrez la porte ou prenez un mur (Agile Tour Genève 2024)
Ouvrez la porte ou prenez un mur (Agile Tour Genève 2024)
 
Le support de présentation des Signaux 2024
Le support de présentation des Signaux 2024Le support de présentation des Signaux 2024
Le support de présentation des Signaux 2024
 
De l'IA comme plagiat à la rédaction d'une « charte IA » à l'université
De l'IA comme plagiat à la rédaction d'une « charte IA » à l'universitéDe l'IA comme plagiat à la rédaction d'une « charte IA » à l'université
De l'IA comme plagiat à la rédaction d'une « charte IA » à l'université
 
Le Comptoir OCTO - Qu’apporte l’analyse de cycle de vie lors d’un audit d’éco...
Le Comptoir OCTO - Qu’apporte l’analyse de cycle de vie lors d’un audit d’éco...Le Comptoir OCTO - Qu’apporte l’analyse de cycle de vie lors d’un audit d’éco...
Le Comptoir OCTO - Qu’apporte l’analyse de cycle de vie lors d’un audit d’éco...
 
Les écrans informatiques au fil du temps.pptx
Les écrans informatiques au fil du temps.pptxLes écrans informatiques au fil du temps.pptx
Les écrans informatiques au fil du temps.pptx
 
MongoDB in a scale-up: how to get away from a monolithic hell — MongoDB Paris...
MongoDB in a scale-up: how to get away from a monolithic hell — MongoDB Paris...MongoDB in a scale-up: how to get away from a monolithic hell — MongoDB Paris...
MongoDB in a scale-up: how to get away from a monolithic hell — MongoDB Paris...
 

En vedette

PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
Christy Abraham Joy
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
MindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
GetSmarter
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
Alireza Esmikhani
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
Project for Public Spaces & National Center for Biking and Walking
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
Erica Santiago
 

En vedette (20)

PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 

Chapitre_6.pptx Système d'exploitation 2

  • 2.  pthread_create arguments:  thread: An opaque, unique identifier for the new thread returned by the subroutine.  attr: An opaque attribute object that may be used to set thread attributes. You can specify a thread attributes object, or NULL for the default values.  start_routine: the C routine that the thread will execute once it is created.  arg: A single argument that may be passed to start_routine. It must be passed by reference as a pointer cast of type void. NULL may be used if no argument is to be passed.
  • 3.
  • 4.  NULL is used for pointers only as it is defined as (void *) 0. It should not be used other than pointers. If NULL is assigned to a pointer, then pointer is pointing to nothing.  0 (zero) is a value.
  • 5.  NULL is a pointer. It’s a constant, made available through stdio.h.  Whenever you see the NULL constant used, it’s as a pointer value. It can also be used to typecast pointers, such as (int *)NULL.  A pointer is a variable that holds a memory location.  Zero is a value. It’s not a memory location, as only pointers can hold memory locations. Further, pointers hold memory locations that reference something.  Perhaps the element that causes the most confusion is the 0 escape character. That’s the null character, which is used in C to terminate a string of text. The problem is that 0 translates into character value zero. In fact, you can use a zero directly (if you know how) and it has the same effect. But that doesn’t mean that zero and NULL are the same thing, especially given that NULL is one use of the word and “null character” is another.
  • 6.  The int value 0 is 4 bytes long (on my computer), which helps confirm it’s an int. But the NULL value is 8 bytes long, which is the size of an address.
  • 7.  Mutex variables must be declared with type pthread_mutex_t, and must be initialized before they can be used. There are two ways to initialize a mutex variable:  Statically, when it is declared. For example: pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER;  Dynamically, with the pthread_mutex_init() routine. This method permits setting mutex object attributes, attr.
  • 8.  A mutex (named for "mutual exclusion") is a binary semaphore with an ownership restriction: it can be unlocked (the post operation) only by whoever locked it (the wait operation). Thus a mutex offers a somewhat stronger protection than an ordinary semaphore.
  • 9.  Un « Mutex » est un sémaphore binaire pouvant prendre un des deux états  "lock" (verrouillé) ou "unlock" (déverrouillé): valeur de sémaphore 1 ou 0  Un « Mutex » ne peut être partagé que par des threads d’un même processus  Un « Mutex » ne peut être verrouillé que par une seule thread à la fois.  Une thread qui tente de verrouiller un « Mutex » déjà verrouillé est suspendu jusqu'à ce que le « Mutex » soit déverrouillé.
  • 10.  Un mutex est une variable de type « thread_mutex_t »  Il existe une constante PTHREAD_MUTEX_INITIALIZER de ce type  permettant une déclaration avec initialisation statique du mutex (avec les valeursde comportement par défaut)  pthread_mutex_t monMutex = THREAD_MUTEX_INITIALIZER;  Un mutex peut également être initialisé par un appel de la primitive  nt pthread_mutex_init(pthread_mutex_t *mutex,const pthread_mutexattr_t *mutexattr);  vec une initialisation par défaut lorsque mutexattr vaut NULL  ex : pthread_mutex_init(&monMutex, NULL);
  • 11.  Un mutex peut être verrouillé par la primitive  int pthread_mutex_lock(pthread_mutex_t *mutex);  Si le mutex est déverrouillé il devient verrouillé  Si le mutex est déjà verrouillé par une autre thread la tentative de verrouillage suspend l'appelant jusqu'à ce que le mutex soit déverrouillé.
  • 12.  Un mutex peut être déverrouillé par la primitive  int pthread_mutex_unlock(pthread_mutex_t *mutex);  Si le mutex est déjà déverrouillé, cet appel n'a aucun effet (comportement par  défaut)  Si le mutex est verrouillé, une des threads en attente obtient le mutex (qui  reprend alors l'état verrouillé) et cette thread redevient active (elle n'est plus  bloquée)  L'opération est toujours non bloquante pour l'appelant
  • 13.
  • 14.
  • 15.  Un sémaphore s :  {Val(s): valeur qui doit toujours être initialisée,  File(s): file d’attente qui va contenir les processus bloqués sur ce sémaphore }  La valeur initiale d’un sémaphore ne doit jamais être négative.  Primitive P(s):  Debut  Val(s) = Val(s) -1;  Si Val(s) < 0 Alors Mettre le processus actif dans la file File(s);  Fin  Primitive V(s):  Debut  Val(s) = Val(s) +1;  Si Val(s) <=0 Alors /* il y a au moins un processus bloqué dans File(s) */
  • 16.
  • 17.
  • 18.  Les prototypes des fonctions et les types sont définis dans « semaphore.h »  Un sémaphore est une variable de type « sem_t »  Il est initialisé par un appel à la primitive :  int sem_init(sem_t *sem, int pshared, unsigned int valeur);  – si "phsahed" vaut 0 le sémaphore ne peut pas être partagé entre threads de différents processus (partage uniquement au sein d'un même processus)  – valeur définit la valeur initiale de ce sémaphore (positive ou nulle)
  • 19.  Les deux opérations P et V sont implémentées par  P : int sem_wait(sem_t *sem);  V : int sem_post(sem_t *sem);  avec les mêmes comportements que les primitives génériques P et V  Il existe également une version non bloquante de la primitive P :  int sem_trywait(sem_t *sem);  qui retourne 0 lorsque la prise est possible (et non bloquante) et qui retourne  EAGAIN sinon (dans le cas où l'appel normal serait bloquant)