commit fb76e225948cf2ae2a7162d917868c1489a1bdd5 Author: yolou Date: Thu Oct 23 14:28:03 2025 +0200 first commit diff --git a/TP_DEV3.1/Base de donnée/Vote.java b/TP_DEV3.1/Base de donnée/Vote.java new file mode 100644 index 0000000..0c3cc50 --- /dev/null +++ b/TP_DEV3.1/Base de donnée/Vote.java @@ -0,0 +1,96 @@ +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +import org.mariadb.jdbc.*; + +public class Vote { + +private static final String lien = "jdbc:mariadb://dwarves.iut-fbleau.fr/yolou"; +private static final String user = "yolou"; +private static final String mdp = "serikhanejunior"; + +public static void main(String[] args) { + + + + +if(args.length !=1 ){ + +System.err.println("Met un seul pays "); +System.exit(0); + + +} + +String pays = args[0]; + +try { + +Class.forName("org.mariadb.jdbc.Driver"); +}catch(ClassNotFoundException e) { + + System.err.println("Il y'a pas de DriverManager Maria DB"); +} + +String requete_sql = "Select pays_votant as votant, p.points" + +"FROM Points p " + +"JOIN Competiteur c ON p.competiteur_id = c.id " + +"JOIN Votant v ON p.votant_id = v.id " + +"WHERE c.pays = ? " + +"ORDER BY v.pays_votant"; + +String totalSql = +"SELECT SUM(p.points) " + +"FROM Points p " + +"JOIN Competiteur c ON p.competiteur_id = c.id " + +"WHERE c.pays = ?"; + +Connection cnx = DriverManager.getConnection(lien, user, mdp); +PreparedStatement pst = cnx.prepareStatement(requete_sql); +PreparedStatement pstTotal = cnx.prepareStatement(totalSql); + +pst.setString(1, pays); +try (ResultSet rs = pst.executeQuery()) { +while (rs.next()) { +String votant = rs.getString("votant"); +int pts = rs.getInt("points"); +System.out.printf("%-10s %d%n", votant, pts); +} +} + +pstTotal.setString(1, pays); +try (ResultSet rsT = pstTotal.executeQuery()) { +rsT.next(); +int total = rsT.getInt(1); +System.out.println("---------"); +System.out.println("Total " + total); +} + +} catch (SQLException e) { +System.err.println("Erreur SQL : " + e.getMessage()); +} + +} + + + + + + + + + + + + + + + + + + + +} \ No newline at end of file diff --git a/TP_DEV3.1/Base de donnée/sql.txt b/TP_DEV3.1/Base de donnée/sql.txt new file mode 100644 index 0000000..4ff18d0 --- /dev/null +++ b/TP_DEV3.1/Base de donnée/sql.txt @@ -0,0 +1,37 @@ +-- Table des pays + +CREATE TABLE Pays_vote ( +id INT AUTO_INCREMENT PRIMARY KEY, +nom VARCHAR(50) UNIQUE NOT NULL +); + +-- Table des votes + +CREATE TABLE Vote ( +id INT AUTO_INCREMENT PRIMARY KEY, +paysfrom_id INT NOT NULL, -- pays qui vote +paysto_id INT NOT NULL, -- pays qui reçoit +nb_points INT NOT NULL, +FOREIGN KEY (paysfrom_id) REFERENCES Pays_vote(id), +FOREIGN KEY (paysto_id) REFERENCES Pays_vote(id) +); + +-- Insertion des pays +insert into Pays_vote (nom) values ('Pays-Bas'), ('Italie'), ('Russie'); + +-- Récupération automatique des id (supposons) +-- Pays-Bas = 1, Italie = 2, Russie = 3 + +-- Insertion des votes (avec id) +VALUES +(2, 1, 5), -- Italie → Pays-Bas = 5 +(3, 1, 5), -- Russie → Pays-Bas = 5 +(1, 2, 16), -- Pays-Bas → Italie = 16 +(3, 2, 1), -- Russie → Italie = 1 +(1, 3, 5), -- Pays-Bas → Russie = 5 +(2, 3, 8); -- Italie → Russie = 8 + +Insert into Vote (paysfrom_id,paysto_id,nb_points) values (2,1,5), (3,1,5), (1,2,16), (3,2,1), (1,3,5), (2,3,8); + +javac -cp .:/export/documents/mariadb-client.jar Vote.java +java -cp .:/export/documents/mariadb-client.jar:. Vote Italie \ No newline at end of file diff --git a/TP_DEV3.1/ControllerSouris.java b/TP_DEV3.1/ControllerSouris.java new file mode 100644 index 0000000..1ec1997 --- /dev/null +++ b/TP_DEV3.1/ControllerSouris.java @@ -0,0 +1,18 @@ + + + +public class ControllerSouris extend Galerie { + + public ControllerSouris(){ + super(); + + } + + public void imageSuivante(){ + + indexImage = (indexImage+1)%listeimage.length(); + etiquetteImage.setIcon() + + + } +} diff --git a/TP_DEV3.1/Organisation du code/AppGalerie.java b/TP_DEV3.1/Organisation du code/AppGalerie.java new file mode 100644 index 0000000..a815001 --- /dev/null +++ b/TP_DEV3.1/Organisation du code/AppGalerie.java @@ -0,0 +1,72 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; +package fr.iutfbleau.projet; + +public class AppGalerie extends JFrame implements MouseListener{ + + + private String[] listeimage = {"image1.png","image2.png","image4.png"}; + private int IndexImage = 0; + private JPanel panneau = new JPanel(new BorderLayout()); + // private JFrame fenetre = new JFrame(); + private JLabel etiquetteImage = new JLabel(); + + public Galerie() { + + + + + super("Confirmation"); + + etiquetteImage.setHorizontalAlignment(SwingConstants.CENTER); + etiquetteImage.setIcon(new ImageIcon(listeimage[IndexImage])); + + panneau.add(etiquetteImage, BorderLayout.CENTER); + panneau.addMouseListener(this); + setContentPane(panneau); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + } + + public void mousePressed(MouseEvent e) {} // Appui sur le bouton de la souris + + public void mouseReleased(MouseEvent e) {} // Relâchement du bouton + + public void mouseEntered(MouseEvent e) {} // Souris entre dans le composant + + + public void mouseExited(MouseEvent e) {} // Souris sort du composant + + public void mouseClicked(MouseEvent e){ + + int largeurpanel = panneau.getWidth(); + + if(e.getX () > largeurpanel/2 ){ + + imageSuivante(); + + } + else{ + + imagePrecedente(); + } + } + + private void imageSuivante() { + IndexImage = (IndexImage + 1) % listeimage.length; + etiquetteImage.setIcon(new ImageIcon(listeimage[IndexImage])); + } + + private void imagePrecedente() { + IndexImage = (IndexImage - 1 + listeimage.length) % listeimage.length; + etiquetteImage.setIcon(new ImageIcon(listeimage[IndexImage])); + } + + public static void main(String[] args) { + + AppGalerie test = new Galerie(); + + test.setVisible(true); + } +} + diff --git a/TP_DEV3.1/Organisation du code/GalerieTest.java b/TP_DEV3.1/Organisation du code/GalerieTest.java new file mode 100644 index 0000000..c57a64e --- /dev/null +++ b/TP_DEV3.1/Organisation du code/GalerieTest.java @@ -0,0 +1,10 @@ +public class GalerieTest { + + + public static void main(String[] args) { + + AppGalerie test = new Galerie(); + + test.setVisible(true); + } +} diff --git a/TP_DEV3.1/Organisation du code/QuizzApp/src/fr/iutfbleau/projet/view/Question.java b/TP_DEV3.1/Organisation du code/QuizzApp/src/fr/iutfbleau/projet/view/Question.java new file mode 100644 index 0000000..6dbcc7a --- /dev/null +++ b/TP_DEV3.1/Organisation du code/QuizzApp/src/fr/iutfbleau/projet/view/Question.java @@ -0,0 +1,82 @@ +package fr.iutflbeau.projet.view; +import fr.iutfbleau.projet.controller.ValidationChoix; +import fr.iutfbleau.projet.modele.*; + + + +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; + + +public class FenetreQuizz extends JFrame implements ActionListener { + +private Question[] questionsquiz; +private ChoixUitlisateur choix; +private int index = 0; +private boolean valide = false; + +private JPanel zoneCentre new JPanel(new BorderLayout()); +private JButton boutonPrecedent = new JButton("Précédent"); +private JButton boutonSuivant = new JButton("Suivant"); +private JButton boutonValider = new JButton("Valider"); + +private EcranQuestion EcranDeBase; + +public FenetreQuizz(Question[] questionsquiz) { + + +super("QCM"); +this.questionsquiz = questionsquiz; +this.choix = new ChoixUitlisateur(questionsquiz.length); +this.setLayout(new BorderLayout()); +this.add(zoneCentre, BorderLayout.CENTER); + +boutonSuivant.addActionListener(this); +boutonPrecedent.addActionListener(this); +boutonValider.addActionListener(this); + +mettreAJourEcran(); + +this.setSize(520, 320); +this.setLocationRelativeTo(null); +this.setDefaultCloseOperation(EXIT_ON_CLOSE); + + + + + + +} + + +@Override +public void actionPerformed(ActionEvent e){ + +Object source = e.getSource(); + + +if(source == boutonPrecedent){ + +AllerPrecedent(); + +} + + if(source == boutonSuivant){ + + AllerSuivant(); +} + + if(source == boutonValider) { + + Valider(); +} + +} + + + + + + +} \ No newline at end of file diff --git a/TP_DEV3.1/Organisation du code/projet/AppGalerie.jar b/TP_DEV3.1/Organisation du code/projet/AppGalerie.jar new file mode 100644 index 0000000..bd52138 Binary files /dev/null and b/TP_DEV3.1/Organisation du code/projet/AppGalerie.jar differ diff --git a/TP_DEV3.1/Organisation du code/projet/build/fr/iutfbleau/projet/AppGalerie.class b/TP_DEV3.1/Organisation du code/projet/build/fr/iutfbleau/projet/AppGalerie.class new file mode 100644 index 0000000..b003976 Binary files /dev/null and b/TP_DEV3.1/Organisation du code/projet/build/fr/iutfbleau/projet/AppGalerie.class differ diff --git a/TP_DEV3.1/Organisation du code/projet/build/images/image1.png b/TP_DEV3.1/Organisation du code/projet/build/images/image1.png new file mode 100644 index 0000000..6b14bf3 Binary files /dev/null and b/TP_DEV3.1/Organisation du code/projet/build/images/image1.png differ diff --git a/TP_DEV3.1/Organisation du code/projet/build/images/image2.png b/TP_DEV3.1/Organisation du code/projet/build/images/image2.png new file mode 100644 index 0000000..c9b000d Binary files /dev/null and b/TP_DEV3.1/Organisation du code/projet/build/images/image2.png differ diff --git a/TP_DEV3.1/Organisation du code/projet/build/images/image4.png b/TP_DEV3.1/Organisation du code/projet/build/images/image4.png new file mode 100644 index 0000000..6aa02d1 Binary files /dev/null and b/TP_DEV3.1/Organisation du code/projet/build/images/image4.png differ diff --git a/TP_DEV3.1/Organisation du code/projet/projet.jar b/TP_DEV3.1/Organisation du code/projet/projet.jar new file mode 100644 index 0000000..bad102e Binary files /dev/null and b/TP_DEV3.1/Organisation du code/projet/projet.jar differ diff --git a/TP_DEV3.1/Organisation du code/projet/res/images/image1.png b/TP_DEV3.1/Organisation du code/projet/res/images/image1.png new file mode 100644 index 0000000..6b14bf3 Binary files /dev/null and b/TP_DEV3.1/Organisation du code/projet/res/images/image1.png differ diff --git a/TP_DEV3.1/Organisation du code/projet/res/images/image2.png b/TP_DEV3.1/Organisation du code/projet/res/images/image2.png new file mode 100644 index 0000000..c9b000d Binary files /dev/null and b/TP_DEV3.1/Organisation du code/projet/res/images/image2.png differ diff --git a/TP_DEV3.1/Organisation du code/projet/res/images/image4.png b/TP_DEV3.1/Organisation du code/projet/res/images/image4.png new file mode 100644 index 0000000..6aa02d1 Binary files /dev/null and b/TP_DEV3.1/Organisation du code/projet/res/images/image4.png differ diff --git a/TP_DEV3.1/Organisation du code/projet/src/fr/iutfbleau/projet/AppGalerie.java b/TP_DEV3.1/Organisation du code/projet/src/fr/iutfbleau/projet/AppGalerie.java new file mode 100644 index 0000000..6174782 --- /dev/null +++ b/TP_DEV3.1/Organisation du code/projet/src/fr/iutfbleau/projet/AppGalerie.java @@ -0,0 +1,72 @@ +package fr.iutfbleau.projet; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; +import java.net.URL; + +public class AppGalerie extends JFrame implements MouseListener { + + // 1) Chemins DANS LE JAR (=> classpath) + private String[] listeImage = { + "images/image1.png", + "images/image2.png", + "images/image4.png" + }; + private int indexImage = 0; + + private JPanel panneau = new JPanel(new BorderLayout()); + private JLabel etiquetteImage = new JLabel(); + + public AppGalerie() { + super("Confirmation"); + + etiquetteImage.setHorizontalAlignment(SwingConstants.CENTER); + etiquetteImage.setIcon(chargerIcone(listeImage[indexImage])); // <-- ICI + + panneau.add(etiquetteImage, BorderLayout.CENTER); + panneau.addMouseListener(this); + + setContentPane(panneau); + setSize(800, 600); + setLocationRelativeTo(null); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + } + + // === MouseListener === + @Override public void mouseClicked(MouseEvent e) { + int largeurPanel = panneau.getWidth(); + if (e.getX() >= largeurPanel / 2) imageSuivante(); + else imagePrecedente(); + } + @Override public void mousePressed(MouseEvent e) {} + @Override public void mouseReleased(MouseEvent e) {} + @Override public void mouseEntered(MouseEvent e) {} + @Override public void mouseExited(MouseEvent e) {} + + // === Navigation === + private void imageSuivante() { + indexImage = (indexImage + 1) % listeImage.length; + etiquetteImage.setIcon(chargerIcone(listeImage[indexImage])); // <-- ICI + } + private void imagePrecedente() { + indexImage = (indexImage - 1 + listeImage.length) % listeImage.length; + etiquetteImage.setIcon(chargerIcone(listeImage[indexImage])); // <-- ICI + } + + // === CHARGEMENT VIA CLASSPATH === + private ImageIcon chargerIcone(String cheminDansJar) { + URL url = getClass().getClassLoader().getResource(cheminDansJar); + if (url == null) { + // debug utile si un nom est faux + throw new IllegalArgumentException("Ressource introuvable: " + cheminDansJar); + } + return new ImageIcon(url); + } + + public static void main(String[] args) { + AppGalerie test = new AppGalerie(); + + test.setVisible(true); + } +} diff --git a/TP_DEV3.1/Organisation du code/test/AppGalerie.java b/TP_DEV3.1/Organisation du code/test/AppGalerie.java new file mode 100644 index 0000000..f5eccde --- /dev/null +++ b/TP_DEV3.1/Organisation du code/test/AppGalerie.java @@ -0,0 +1,70 @@ +package fr.iutfbleau.projet; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; +import java.net.URL; + +public class AppGalerie extends JFrame implements MouseListener { + + // 1) Chemins DANS LE JAR (=> classpath) + private String[] listeImage = { + "images/image1.png", + "images/image2.png", + "images/image4.png" + }; + private int indexImage = 0; + + private JPanel panneau = new JPanel(new BorderLayout()); + private JLabel etiquetteImage = new JLabel(); + + public AppGalerie() { + super("Confirmation"); + + etiquetteImage.setHorizontalAlignment(SwingConstants.CENTER); + etiquetteImage.setIcon(chargerIcone(listeImage[indexImage])); // <-- ICI + + panneau.add(etiquetteImage, BorderLayout.CENTER); + panneau.addMouseListener(this); + + setContentPane(panneau); + setSize(800, 600); + setLocationRelativeTo(null); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + } + + // === MouseListener === + @Override public void mouseClicked(MouseEvent e) { + int largeurPanel = panneau.getWidth(); + if (e.getX() >= largeurPanel / 2) imageSuivante(); + else imagePrecedente(); + } + @Override public void mousePressed(MouseEvent e) {} + @Override public void mouseReleased(MouseEvent e) {} + @Override public void mouseEntered(MouseEvent e) {} + @Override public void mouseExited(MouseEvent e) {} + + // === Navigation === + private void imageSuivante() { + indexImage = (indexImage + 1) % listeImage.length; + etiquetteImage.setIcon(chargerIcone(listeImage[indexImage])); // <-- ICI + } + private void imagePrecedente() { + indexImage = (indexImage - 1 + listeImage.length) % listeImage.length; + etiquetteImage.setIcon(chargerIcone(listeImage[indexImage])); // <-- ICI + } + + // === CHARGEMENT VIA CLASSPATH === + private ImageIcon chargerIcone(String cheminDansJar) { + URL url = getClass().getClassLoader().getResource(cheminDansJar); + if (url == null) { + // debug utile si un nom est faux + throw new IllegalArgumentException("Ressource introuvable: " + cheminDansJar); + } + return new ImageIcon(url); + } + + public static void main(String[] args) { + SwingUtilities.invokeLater(() -> new AppGalerie().setVisible(true)); + } +} diff --git a/TP_DEV3.1/Transitions/Galerie.class b/TP_DEV3.1/Transitions/Galerie.class new file mode 100644 index 0000000..6dbdb8e Binary files /dev/null and b/TP_DEV3.1/Transitions/Galerie.class differ diff --git a/TP_DEV3.1/Transitions/Galerie.java b/TP_DEV3.1/Transitions/Galerie.java new file mode 100644 index 0000000..f5070ef --- /dev/null +++ b/TP_DEV3.1/Transitions/Galerie.java @@ -0,0 +1,65 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; + +public class Galerie extends JFrame implements MouseListener{ + + + private String[] listeimage = {"image1.png","image2.png","image4.png"}; + private int IndexImage = 0; + private JPanel panneau = new JPanel(new BorderLayout()); + // private JFrame fenetre = new JFrame(); + private JLabel etiquetteImage = new JLabel(); + + public Galerie() { + + + + + super("Confirmation"); + + etiquetteImage.setHorizontalAlignment(SwingConstants.CENTER); + etiquetteImage.setIcon(new ImageIcon(listeimage[IndexImage])); + + panneau.add(etiquetteImage, BorderLayout.CENTER); + panneau.addMouseListener(this); + setContentPane(panneau); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + } + + public void mousePressed(MouseEvent e) {} // Appui sur le bouton de la souris + + public void mouseReleased(MouseEvent e) {} // Relâchement du bouton + + public void mouseEntered(MouseEvent e) {} // Souris entre dans le composant + + + public void mouseExited(MouseEvent e) {} // Souris sort du composant + + public void mouseClicked(MouseEvent e){ + + int largeurpanel = panneau.getWidth(); + + if(e.getX () > largeurpanel/2 ){ + + imageSuivante(); + + } + else{ + + imagePrecedente(); + } + } + + private void imageSuivante() { + IndexImage = (IndexImage + 1) % listeimage.length; + etiquetteImage.setIcon(new ImageIcon(listeimage[IndexImage])); + } + + private void imagePrecedente() { + IndexImage = (IndexImage - 1 + listeimage.length) % listeimage.length; + etiquetteImage.setIcon(new ImageIcon(listeimage[IndexImage])); + } + +} + diff --git a/TP_DEV3.1/Transitions/GalerieSimple.java b/TP_DEV3.1/Transitions/GalerieSimple.java new file mode 100644 index 0000000..33636bc --- /dev/null +++ b/TP_DEV3.1/Transitions/GalerieSimple.java @@ -0,0 +1,94 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; + +public class GalerieSimple extends JFrame { + + // Tableau des fichiers images + private String[] fichiers = { "img1.jpg", "img2.jpg", "img3.jpg", "img4.jpg" }; + private int indiceImage = 0; + + // Composants graphiques + private JLabel etiquetteImage = new JLabel(); + + public GalerieSimple() { + super("Galerie avec Confirmation"); + + // Affichage de la première image + etiquetteImage.setHorizontalAlignment(SwingConstants.CENTER); + etiquetteImage.setIcon(new ImageIcon(fichiers[indiceImage])); + + // Panneau principal + JPanel panneau = new JPanel(new BorderLayout()); + panneau.add(etiquetteImage, BorderLayout.CENTER); + + // Clic souris → suivant ou précédent + panneau.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + int largeur = panneau.getWidth(); + if (e.getX() >= largeur / 2) { + imageSuivante(); + } else { + imagePrecedente(); + } + } + }); + + // Fermeture personnalisée (on demande confirmation) + setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); + addWindowListener(new WindowAdapter() { + @Override + public void windowClosing(WindowEvent e) { + demanderConfirmation(); + } + }); + + // Paramètres de la fenêtre + setContentPane(panneau); + setSize(800, 600); + setLocationRelativeTo(null); + } + + private void imageSuivante() { + indiceImage = (indiceImage + 1) % fichiers.length; + etiquetteImage.setIcon(new ImageIcon(fichiers[indiceImage])); + } + + private void imagePrecedente() { + indiceImage = (indiceImage - 1 + fichiers.length) % fichiers.length; + etiquetteImage.setIcon(new ImageIcon(fichiers[indiceImage])); + } + + private void demanderConfirmation() { + // Fenêtre modale faite avec JDialog + JDialog dialogue = new JDialog(this, "Confirmation", true); + dialogue.setLayout(new BorderLayout()); + + JLabel message = new JLabel("Voulez-vous quitter ?", SwingConstants.CENTER); + JPanel panneauBoutons = new JPanel(); + + JButton boutonOui = new JButton("Oui"); + JButton boutonNon = new JButton("Non"); + + panneauBoutons.add(boutonOui); + panneauBoutons.add(boutonNon); + + dialogue.add(message, BorderLayout.CENTER); + dialogue.add(panneauBoutons, BorderLayout.SOUTH); + + boutonOui.addActionListener(ev -> System.exit(0)); + boutonNon.addActionListener(ev -> dialogue.dispose()); + + dialogue.setSize(300, 150); + dialogue.setLocationRelativeTo(this); + dialogue.setVisible(true); + } + + public static void main(String[] args) { + SwingUtilities.invokeLater(() -> { + GalerieSimple fenetre = new GalerieSimple(); + fenetre.setVisible(true); + }); + } +} diff --git a/TP_DEV3.1/Transitions/GalerieTest.class b/TP_DEV3.1/Transitions/GalerieTest.class new file mode 100644 index 0000000..3a6a45c Binary files /dev/null and b/TP_DEV3.1/Transitions/GalerieTest.class differ diff --git a/TP_DEV3.1/Transitions/GalerieTest.java b/TP_DEV3.1/Transitions/GalerieTest.java new file mode 100644 index 0000000..b988eb1 --- /dev/null +++ b/TP_DEV3.1/Transitions/GalerieTest.java @@ -0,0 +1,10 @@ +public class GalerieTest { + + + public static void main(String[] args) { + + Galerie test = new Galerie(); + + test.setVisible(true); + } +} diff --git a/TP_DEV3.1/Transitions/ListeImage.java b/TP_DEV3.1/Transitions/ListeImage.java new file mode 100644 index 0000000..2bb20bc --- /dev/null +++ b/TP_DEV3.1/Transitions/ListeImage.java @@ -0,0 +1,27 @@ +import java.awt.event.*; +import javax.swing.*; + + +public class ListeImage { + + private final String[] + + + + + + + + + + + + + + + + + + + +} \ No newline at end of file diff --git a/TP_DEV3.1/Transitions/image1.png b/TP_DEV3.1/Transitions/image1.png new file mode 100644 index 0000000..6b14bf3 Binary files /dev/null and b/TP_DEV3.1/Transitions/image1.png differ diff --git a/TP_DEV3.1/Transitions/image2.png b/TP_DEV3.1/Transitions/image2.png new file mode 100644 index 0000000..c9b000d Binary files /dev/null and b/TP_DEV3.1/Transitions/image2.png differ diff --git a/TP_DEV3.1/Transitions/image4.png b/TP_DEV3.1/Transitions/image4.png new file mode 100644 index 0000000..6aa02d1 Binary files /dev/null and b/TP_DEV3.1/Transitions/image4.png differ diff --git a/TP_DEV3.1/Transitions/src/galerie/app/MainCardLayout.java b/TP_DEV3.1/Transitions/src/galerie/app/MainCardLayout.java new file mode 100644 index 0000000..2ce1fca --- /dev/null +++ b/TP_DEV3.1/Transitions/src/galerie/app/MainCardLayout.java @@ -0,0 +1,15 @@ +package galerie.app; + +import galerie.model.ImageList; +import galerie.view.GalleryCardLayoutFrame; + +import javax.swing.*; + +public class MainCardLayout { + public static void main(String[] args) { + SwingUtilities.invokeLater(() -> { + ImageList model = new ImageList("img1.jpg","img2.jpg","img3.jpg","img4.jpg"); + new GalleryCardLayoutFrame(model).setVisible(true); + }); + } +} diff --git a/TP_DEV3.1/Transitions/src/galerie/app/MainReplacement.java b/TP_DEV3.1/Transitions/src/galerie/app/MainReplacement.java new file mode 100644 index 0000000..10b8555 --- /dev/null +++ b/TP_DEV3.1/Transitions/src/galerie/app/MainReplacement.java @@ -0,0 +1,15 @@ +package galerie.app; + +import galerie.model.ImageList; +import galerie.view.GalleryReplacementFrame; + +import javax.swing.*; + +public class MainReplacement { + public static void main(String[] args) { + SwingUtilities.invokeLater(() -> { + ImageList model = new ImageList("img1.jpg","img2.jpg","img3.jpg","img4.jpg"); + new GalleryReplacementFrame(model).setVisible(true); + }); + } +} diff --git a/TP_DEV3.1/Transitions/src/galerie/app/MainWithConfirmation.java b/TP_DEV3.1/Transitions/src/galerie/app/MainWithConfirmation.java new file mode 100644 index 0000000..9ac8f91 --- /dev/null +++ b/TP_DEV3.1/Transitions/src/galerie/app/MainWithConfirmation.java @@ -0,0 +1,31 @@ +package galerie.app; + +import galerie.model.ImageList; +import galerie.view.GalleryCardLayoutFrame; +import galerie.view.ConfirmOnCloseDialog; + +import javax.swing.*; +import java.awt.event.*; + +public class MainWithConfirmation { + public static void main(String[] args) { + SwingUtilities.invokeLater(() -> { + ImageList model = new ImageList("img1.jpg","img2.jpg","img3.jpg","img4.jpg"); + JFrame frame = new GalleryCardLayoutFrame(model); + + frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); + frame.addWindowListener(new WindowAdapter() { + @Override public void windowClosing(WindowEvent e) { + ConfirmOnCloseDialog dlg = new ConfirmOnCloseDialog(frame, + new ConfirmOnCloseDialog.ResultHandler() { + @Override public void onYes() { System.exit(0); } + @Override public void onNo() { } + }); + dlg.setVisible(true); + } + }); + + frame.setVisible(true); + }); + } +} diff --git a/TP_DEV3.1/Transitions/src/galerie/control/HalfClickNavigator.java b/TP_DEV3.1/Transitions/src/galerie/control/HalfClickNavigator.java new file mode 100644 index 0000000..2a18845 --- /dev/null +++ b/TP_DEV3.1/Transitions/src/galerie/control/HalfClickNavigator.java @@ -0,0 +1,26 @@ +package galerie.control; + +import java.awt.event.*; +import javax.swing.*; + +/** + * Rôle unique : convertir un clic gauche/droite (moitié de composant) + * en action "next" ou "previous". + */ +public class HalfClickNavigator extends MouseAdapter { + private final JComponent surface; + private final Runnable onNext; + private final Runnable onPrevious; + + public HalfClickNavigator(JComponent surface, Runnable onNext, Runnable onPrevious) { + this.surface = surface; + this.onNext = onNext; + this.onPrevious = onPrevious; + } + + @Override public void mouseClicked(MouseEvent e) { + int w = surface.getWidth(); + if (e.getX() >= w / 2) onNext.run(); + else onPrevious.run(); + } +} diff --git a/TP_DEV3.1/Transitions/src/galerie/model/ImageList.java b/TP_DEV3.1/Transitions/src/galerie/model/ImageList.java new file mode 100644 index 0000000..0a4c1cf --- /dev/null +++ b/TP_DEV3.1/Transitions/src/galerie/model/ImageList.java @@ -0,0 +1,36 @@ +package galerie.model; + +import javax.swing.*; + +public class ImageList { + private final String[] files; + private int index = 0; + + public ImageList(String... files) { + if (files == null || files.length == 0) { + throw new IllegalArgumentException("Il faut au moins une image."); + } + this.files = files; + } + + public Icon currentIcon() { + return new ImageIcon(files[index]); + } + + public void next() { + index = (index + 1) % files.length; + } + + + public void previous() { + index = (index - 1 + files.length) % files.length; + } + + public int size() { + return files.length; + } + + public Icon iconAt(int i) { + return new ImageIcon(files[i]); + } +} diff --git a/TP_DEV3.1/Transitions/src/galerie/view/ConfirmOnCloseDialog.java b/TP_DEV3.1/Transitions/src/galerie/view/ConfirmOnCloseDialog.java new file mode 100644 index 0000000..85cc186 --- /dev/null +++ b/TP_DEV3.1/Transitions/src/galerie/view/ConfirmOnCloseDialog.java @@ -0,0 +1,33 @@ +package galerie.view; + +import javax.swing.*; +import java.awt.*; + +public class ConfirmOnCloseDialog extends JDialog { + + public interface ResultHandler { void onYes(); void onNo(); } + + public ConfirmOnCloseDialog(JFrame parent, ResultHandler handler) { + super(parent, "Quitter l'application ?", true); + setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); + + JPanel content = new JPanel(new BorderLayout(10,10)); + content.setBorder(BorderFactory.createEmptyBorder(12,12,12,12)); + JLabel msg = new JLabel("Voulez-vous vraiment quitter ?", SwingConstants.CENTER); + + JPanel buttons = new JPanel(new FlowLayout(FlowLayout.CENTER)); + JButton yes = new JButton("Oui"); + JButton no = new JButton("Non"); + buttons.add(yes); + buttons.add(no); + + yes.addActionListener(e -> { handler.onYes(); dispose(); }); + no.addActionListener(e -> { handler.onNo(); dispose(); }); + + content.add(msg, BorderLayout.CENTER); + content.add(buttons, BorderLayout.SOUTH); + setContentPane(content); + pack(); + setLocationRelativeTo(parent); + } +} diff --git a/TP_DEV3.1/Transitions/src/galerie/view/GalleryCardLayoutFrame.java b/TP_DEV3.1/Transitions/src/galerie/view/GalleryCardLayoutFrame.java new file mode 100644 index 0000000..aafd3bd --- /dev/null +++ b/TP_DEV3.1/Transitions/src/galerie/view/GalleryCardLayoutFrame.java @@ -0,0 +1,30 @@ +package galerie.view; + +import galerie.model.ImageList; +import galerie.control.HalfClickNavigator; + +import javax.swing.*; +import java.awt.*; + +public class GalleryCardLayoutFrame extends JFrame { + private final CardLayout cards = new CardLayout(); + private final JPanel deck = new JPanel(cards); + + public GalleryCardLayoutFrame(ImageList images) { + super("Galerie — CardLayout"); + setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + setSize(800, 600); + setLocationRelativeTo(null); + + for (int i = 0; i < images.size(); i++) { + JLabel lab = new JLabel(images.iconAt(i), SwingConstants.CENTER); + lab.setVerticalAlignment(SwingConstants.CENTER); + deck.add(lab, "card" + i); + } + deck.addMouseListener(new HalfClickNavigator(deck, + () -> cards.next(deck), + () -> cards.previous(deck))); + + setContentPane(deck); + } +} diff --git a/TP_DEV3.1/Transitions/src/galerie/view/GalleryReplacementFrame.java b/TP_DEV3.1/Transitions/src/galerie/view/GalleryReplacementFrame.java new file mode 100644 index 0000000..5d99fed --- /dev/null +++ b/TP_DEV3.1/Transitions/src/galerie/view/GalleryReplacementFrame.java @@ -0,0 +1,39 @@ +package galerie.view; + +import galerie.model.ImageList; +import galerie.control.HalfClickNavigator; + +import javax.swing.*; +import java.awt.*; + +public class GalleryReplacementFrame extends JFrame { + private final ImageList images; + private final JLabel viewer = new JLabel("", SwingConstants.CENTER); + + public GalleryReplacementFrame(ImageList images) { + super("Galerie — Remplacement d'un seul JLabel"); + this.images = images; + setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + setSize(800, 600); + setLocationRelativeTo(null); + + viewer.setVerticalAlignment(SwingConstants.CENTER); + viewer.setIcon(images.currentIcon()); + + JPanel surface = new JPanel(new BorderLayout()); + surface.add(viewer, BorderLayout.CENTER); + surface.addMouseListener(new HalfClickNavigator(surface, this::next, this::previous)); + + setContentPane(surface); + } + + private void next() { + images.next(); + viewer.setIcon(images.currentIcon()); + } + + private void previous() { + images.previous(); + viewer.setIcon(images.currentIcon()); + } +} diff --git a/TP_DEV3.2/Genericite/Exo1.class b/TP_DEV3.2/Genericite/Exo1.class new file mode 100644 index 0000000..b2aca5d Binary files /dev/null and b/TP_DEV3.2/Genericite/Exo1.class differ diff --git a/TP_DEV3.2/Genericite/Exo1.java b/TP_DEV3.2/Genericite/Exo1.java new file mode 100644 index 0000000..3e6eebd --- /dev/null +++ b/TP_DEV3.2/Genericite/Exo1.java @@ -0,0 +1,69 @@ +import java.util.*; // pour ArrayList, List, Arrays, Comparator + + + +public class Exo1 { + + + + +public static void main (String[] args) { + +List liste1 = new ArrayList<>(); +liste1.add(9); +liste1.add(1); + + + +List liste2 = new ArrayList<>(); +liste2.add(15); +liste2.add(1.5f); +liste2.add(15000L); + + +List liste3 = new ArrayList<>(); +liste3.add(3.93f); +liste3.add(3.07f); + +System.out.println("liste 1 (Integer) ="+ liste1); +System.out.println("liste 2 (Number) ="+ liste2); +System.out.println("liste 3 (Float) ="+ liste3); + + +liste2.addAll(liste1); +liste2.addAll(liste3); + +System.out.println("Apres versement des liste 1 et 3 dans la 2" + liste2); + + + + + +} + + + + + + + +} + + + + + + + + + + + + + + + + + + + diff --git a/TP_DEV3.2/Genericite/Exodeux.class b/TP_DEV3.2/Genericite/Exodeux.class new file mode 100644 index 0000000..c10581d Binary files /dev/null and b/TP_DEV3.2/Genericite/Exodeux.class differ diff --git a/TP_DEV3.2/Genericite/Exodeux.java b/TP_DEV3.2/Genericite/Exodeux.java new file mode 100644 index 0000000..3f8540c --- /dev/null +++ b/TP_DEV3.2/Genericite/Exodeux.java @@ -0,0 +1,83 @@ +import java.util.*; // pour ArrayList, List, Arrays, Comparator + +public class Exodeux { + + +public static void main (String[] args) { + + + +AfficherTouslesArgs(args); +AfficherLes5erArgs(args); +trierEtAfficherNaturel(args); +trierEtAffiicherSansCasse(args); + +} + + +public static void AfficherTouslesArgs(String[] arguments){ + + +String TousLesArgsTransforme = Arrays.toString(arguments); +System.out.println("Voici tous les args " + TousLesArgsTransforme); + + +} + +public static void AfficherLes5erArgs(String[] arguments) { + +int limite = Math.min(5, arguments.length); +String[] premier = Arrays.copyOf(arguments,limite); +System.out.println("Voici les 5er arguments : "+Arrays.toString(premier)); + + + + +} + + + +public static void trierEtAfficherNaturel(String[] args) { + + +String[] copyArgument = Arrays.copyOf(args, args.length); + +Arrays.sort(copyArgument); + +String NaturelTriage = Arrays.toString(copyArgument); + +System.out.println("Tri naturel" + NaturelTriage); + + + + + +} + +public static void trierEtAffiicherSansCasse(String[] args){ + + + +String[] copyArgument = Arrays.copyOf(args, args.length); +Arrays.sort(copyArgument,String::compareToIgnoreCase); +String SansCasseTrierNickelChrome = Arrays.toString(copyArgument); +System.out.println("Tri sans casse naturel : "+ SansCasseTrierNickelChrome); + + + + + + +} + + + + + + + + + + + +} \ No newline at end of file diff --git a/TP_DEV3.2/Genericite/Exotrois.java b/TP_DEV3.2/Genericite/Exotrois.java new file mode 100644 index 0000000..22d92e5 --- /dev/null +++ b/TP_DEV3.2/Genericite/Exotrois.java @@ -0,0 +1,106 @@ +public class Exotrois { + + +public static plusFrequent(T[] tableau){ + + +T valeurGagnante = tableau[0]; +int frequenceGagnante = 0; +int premierIndexGagnant = tableau.length; + + + +for(int i=0;i couleurs = new LinkedList<>(); + + private static final int NB_COULEURS = 10; + private static final int LARGEUR = 60; + private static final int HAUTEUR = 80; + private static final int ESPACE = 8; + private static final int INCLINAISON = 15; // décalage pour le parallélogramme + + public Luminance() { + for (int i = 0; i < NB_COULEURS; i++) { + int r = random.nextInt(256); + int g = random.nextInt(256); + int b = random.nextInt(256); + couleurs.add(new Color(r, g, b)); + } + + int largeurFenetre = (LARGEUR + ESPACE) * NB_COULEURS + 40; + int hauteurFenetre = HAUTEUR + 60; + this.setPreferredSize(new Dimension(largeurFenetre, hauteurFenetre)); + addMouseListener(this); + } + + public int LuminenceApp(Color c) { + int rouge = c.getRed(); + int vert = c.getGreen(); + int bleu = c.getBlue(); + return (21 * rouge + 72 * vert + 7 * bleu) / 100; + } + + @Override + protected void paintComponent(Graphics pinceau) { + super.paintComponent(pinceau); + Graphics g = pinceau; + + int x = 20; + int y = 20; + + for (Color c : couleurs) { + g.setColor(c); + + // dessin d’un parallélogramme + int[] xs = {x + INCLINAISON, x + LARGEUR + INCLINAISON, x + LARGEUR, x}; + int[] ys = {y, y, y + HAUTEUR, y + HAUTEUR}; + Polygon p = new Polygon(xs, ys, 4); + + g.fillPolygon(p); + g.setColor(Color.DARK_GRAY); + g.drawPolygon(p); + + x = x + LARGEUR + ESPACE; + } + } + + private int indexAt(int mx, int my) { + int x = 20; + int y = 20; + + for (int i = 0; i < couleurs.size(); i++) { + int[] xs = {x + INCLINAISON, x + LARGEUR + INCLINAISON, x + LARGEUR, x}; + int[] ys = {y, y, y + HAUTEUR, y + HAUTEUR}; + Polygon p = new Polygon(xs, ys, 4); + + if (p.contains(mx, my)) { + return i; + } + + x = x + LARGEUR + ESPACE; + } + return -1; + } + + @Override + public void mouseClicked(MouseEvent e) { + int intervalle = indexAt(e.getX(), e.getY()); + if (intervalle >= 0) { + Color c = couleurs.get(intervalle); + System.out.println("Luminance = " + LuminenceApp(c) + + " (R = " + c.getRed() + " G = " + c.getGreen() + " B = " + c.getBlue() + ")"); + couleurs.remove(intervalle); + repaint(); + } + } + + @Override public void mousePressed(MouseEvent e) {} + @Override public void mouseReleased(MouseEvent e) {} + @Override public void mouseEntered(MouseEvent e) {} + @Override public void mouseExited(MouseEvent e) {} + + public static void main(String[] args) { + JFrame f = new JFrame("Luminence"); + f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + f.setContentPane(new Luminance()); + f.pack(); + f.setLocationRelativeTo(null); + f.setVisible(true); + } + } + \ No newline at end of file diff --git a/TP_DEV3.2/Liste/Maillon.java b/TP_DEV3.2/Liste/Maillon.java new file mode 100644 index 0000000..671335c --- /dev/null +++ b/TP_DEV3.2/Liste/Maillon.java @@ -0,0 +1,33 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; +import java.util.*; +import java.util.List; + + + +public class ChainedList implements List{ + + + + +public static class Node{ + + + + E value; + Node next; + Node(E v){ + + value = v; + } + + +} + + + + + + +} \ No newline at end of file diff --git a/TP_DEV3.2/Piles/Arithmetique.java b/TP_DEV3.2/Piles/Arithmetique.java new file mode 100644 index 0000000..b6bcfb7 --- /dev/null +++ b/TP_DEV3.2/Piles/Arithmetique.java @@ -0,0 +1,51 @@ +import java.util.Stack; + +public class Arithmetique { + + public static void main(String[] args) { + + // Exemple : 18 6 1 2 + 9 + / + 8 7 - * + // String[] args = {"18", "6", "1", "2", "+", "9", "+", "/", "+", "8", "7", "-", "*"}; + + Stack pile = new Stack<>(); + + for (String element : args) { + if (estNombre(element)) { + // Si c’est un nombre, on le met dans la pile + pile.push(Integer.parseInt(element)); + } else { + // Sinon c’est un opérateur on dépile 2 valeurs + int b = pile.pop(); + int a = pile.pop(); + int resultat = calculer(a, b, element); + // On empile le résultat + pile.push(resultat); + } + } + + // À la fin, il reste le résultat final dans la pile + System.out.println("Résultat = " + pile.pop()); + } + + // Vérifie si une chaîne est un nombre + public static boolean estNombre(String s) { + try { + Integer.parseInt(s); + return true; + } catch (NumberFormatException e) { + return false; + } + } + + // Calcule a (op) b + public static int calculer(int a, int b, String op) { + switch (op) { + case "+": return a + b; + case "-": return a - b; + case "*": return a * b; + case "/": return a / b; + default: + throw new IllegalArgumentException("Opérateur inconnu : " + op); + } + } +} diff --git a/TP_DEV3.2/Piles/Pile.java b/TP_DEV3.2/Piles/Pile.java new file mode 100644 index 0000000..8d1abcd --- /dev/null +++ b/TP_DEV3.2/Piles/Pile.java @@ -0,0 +1,20 @@ +public class Pile { + + + + + + + + + + + + + + + + + + +} \ No newline at end of file diff --git a/TP_DEV3.2/Recursivité/Appels.class b/TP_DEV3.2/Recursivité/Appels.class new file mode 100644 index 0000000..f35d7ba Binary files /dev/null and b/TP_DEV3.2/Recursivité/Appels.class differ diff --git a/TP_DEV3.2/Recursivité/Appels.java b/TP_DEV3.2/Recursivité/Appels.java new file mode 100644 index 0000000..4c04384 --- /dev/null +++ b/TP_DEV3.2/Recursivité/Appels.java @@ -0,0 +1,48 @@ +import java.io.*; +import java.lang.Thread; + +public class Appels { + + +public static int Factorielle (int n){ + +Thread.dumpStack(); + +if(n==0){ + + + return 1; +} +else { + +return n*Factorielle(n-1); + +} + +/*System.out.println() +*/ + +} + +public static void main(String[] args){ + + + +if(args.length != 1){ + +System.out.println("Usage : java Appel "); +return; + +} + + +int n = Integer.parseInt(args[0]); + +int resultat = Factorielle(n); + +System.out.println(n+"! = " + resultat); + +} + + +} \ No newline at end of file diff --git a/TP_DEV3.2/Recursivité/Fibonacci.java b/TP_DEV3.2/Recursivité/Fibonacci.java new file mode 100644 index 0000000..935fdb1 --- /dev/null +++ b/TP_DEV3.2/Recursivité/Fibonacci.java @@ -0,0 +1,19 @@ +public class Fibonacci { + + + + +public int Fibonacci (int a){ + + + + + + +} + + + + + +} \ No newline at end of file diff --git a/TP_DEV3.2/Recursivité/Tableaux.class b/TP_DEV3.2/Recursivité/Tableaux.class new file mode 100644 index 0000000..84854ff Binary files /dev/null and b/TP_DEV3.2/Recursivité/Tableaux.class differ diff --git a/TP_DEV3.2/Recursivité/Tableaux.java b/TP_DEV3.2/Recursivité/Tableaux.java new file mode 100644 index 0000000..3247475 --- /dev/null +++ b/TP_DEV3.2/Recursivité/Tableaux.java @@ -0,0 +1,82 @@ +public class Tableaux{ + +public static void remplir(String[] args, int[] tab, int i) { + // Cas de base + if (i == args.length) { + return; + } + + // Cas récursif + tab[i] = Integer.parseInt(args[i]); + remplir(args, tab, i + 1); +} + + + + + +public static void AfficheTableaux(int[] tab, int i){ + + if(i==tab.length){ + return; + } + + + +System.out.println(tab[i]); +AfficheTableaux(tab,i+1); + + +} + + + +public static void AfficheTableauxInverse(int[] tab, int i){ + if(i<0){ + return; + } + + +System.out.println(tab[i]); +AfficheTableauxInverse(tab, i-1); + + +} + +public static int MaxTab(int[] tab, int i){ + +if(i==tab.length-1){ + + +return tab[i]; + + +} + +int maxRestant=MaxTab(tab,i+1); +return Math.max(tab[i], maxRestant); + + +} +public static void main(String[] args){ + + +if(args.length <1){ + + System.out.println("Usage : java Tableaux "); + return; + +} + int[] tab = new int[args.length]; +remplir(args,tab,0); +AfficheTableaux(tab, 0); +AfficheTableauxInverse(tab,tab.length-1); +int max = MaxTab(tab,0); +System.out.println("Le maximum est :"+ max); + +} +} + + + + diff --git a/TP_SCR/TP_Fichiers/cp_stdio_ultra.c b/TP_SCR/TP_Fichiers/cp_stdio_ultra.c new file mode 100644 index 0000000..f103f96 --- /dev/null +++ b/TP_SCR/TP_Fichiers/cp_stdio_ultra.c @@ -0,0 +1,46 @@ +#include // pour printf, fprintf, fopen, fread, fwrite, fclose +#include // pour exit() + +int main(int argc, char *argv[]) { + FILE *source, *dest; // pointeurs de fichiers + char c; // variable pour stocker un octet lu + + // Vérifier que l'utilisateur a bien fourni deux arguments (source et destination) + if (argc != 3) { + fprintf(stderr, "Usage: %s \n", argv[0]); + exit(1); // quitte le programme avec code d'erreur + } + + // Ouvrir le fichier source en mode lecture binaire ("rb") + source = fopen(argv[1], "rb"); + if (source == NULL) { // si l'ouverture échoue + perror("Erreur ouverture source"); + exit(1); + } + + // Ouvrir ou créer le fichier destination en mode écriture binaire ("wb") + dest = fopen(argv[2], "wb"); + if (dest == NULL) { // si l'ouverture échoue + perror("Erreur ouverture destination"); + fclose(source); // on ferme le fichier source déjà ouvert + exit(1); + } + + // Lire un octet depuis le fichier source puis l'écrire dans le fichier destination + while (fread(&c, 1, 1, source) == 1) { + if (fwrite(&c, 1, 1, dest) != 1) { + perror("Erreur écriture"); + fclose(source); + fclose(dest); + exit(1); + } + } + + // Fermer les deux fichiers + fclose(source); + fclose(dest); + + // Indiquer que la copie s'est terminée correctement + printf("Copie terminée (méthode stdio).\n"); + return 0; // programme terminé sans erreur +} diff --git a/TP_SCR/TP_Fichiers/cp_syscall.c b/TP_SCR/TP_Fichiers/cp_syscall.c new file mode 100644 index 0000000..d2d8f43 --- /dev/null +++ b/TP_SCR/TP_Fichiers/cp_syscall.c @@ -0,0 +1,49 @@ +#include // pour perror() et printf() : fonctions d’affichage +#include // pour exit() : permet de quitter le programme en cas d’erreur +#include // pour open() : ouverture de fichiers +#include // pour read(), write(), close() : appels systèmes bas niveau + +int main() { + int source, dest; // "descripteurs de fichiers" : nombres entiers utilisés par le système + char c; // variable qui va contenir un caractère (1 octet) lu depuis le fichier + + // Ouvrir le fichier source en lecture seule (O_RDONLY) + source = open("source.txt", O_RDONLY); + if (source < 0) { // si la valeur < 0, c’est que l’ouverture a échoué + perror("Erreur ouverture source"); // affiche l’erreur système + exit(1); // quitte le programme avec un code d’erreur + } + + // Créer (si n’existe pas) ou ouvrir le fichier destination en écriture + // O_WRONLY = écriture seule + // O_CREAT = créer le fichier si n’existe pas + // O_TRUNC = vider le fichier s’il existe déjà + // 0644 = permissions du fichier (rw-r--r--) + dest = open("dest.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644); + if (dest < 0) { // si l’ouverture échoue + perror("Erreur ouverture destination"); + close(source); // on ferme quand même le fichier source ouvert + exit(1); + } + + // Lire et écrire le fichier octet par octet + // read(source, &c, 1) lit 1 octet du fichier source et le stocke dans c + // la boucle continue tant que read() lit bien 1 octet + while (read(source, &c, 1) == 1) { + // écrire cet octet dans le fichier destination + if (write(dest, &c, 1) != 1) { // si write() échoue + perror("Erreur écriture"); + close(source); + close(dest); + exit(1); + } + } + + // Fermer les deux fichiers + close(source); + close(dest); + + // Indiquer à l’utilisateur que tout s’est bien passé + printf("Copie terminée (méthode appels système).\n"); + return 0; // programme terminé correctement +} diff --git a/TP_SCR/TP_Fichiers/untitled b/TP_SCR/TP_Fichiers/untitled new file mode 100644 index 0000000..e69de29 diff --git a/TP_SCR/TP_Memoire/buf b/TP_SCR/TP_Memoire/buf new file mode 100755 index 0000000..43a2bc9 Binary files /dev/null and b/TP_SCR/TP_Memoire/buf differ diff --git a/TP_SCR/TP_Memoire/buf.c b/TP_SCR/TP_Memoire/buf.c new file mode 100644 index 0000000..4dbf387 --- /dev/null +++ b/TP_SCR/TP_Memoire/buf.c @@ -0,0 +1,9 @@ +#include "helpers.h" + +static char buffer[16 MB] = {0}; + +int main(int argc, char **argv) +{ + randomize(buffer, 16 MB); + return interlude(); +} diff --git a/TP_SCR/TP_Memoire/heap.c b/TP_SCR/TP_Memoire/heap.c new file mode 100644 index 0000000..196ecb9 --- /dev/null +++ b/TP_SCR/TP_Memoire/heap.c @@ -0,0 +1,8 @@ +#include "helpers.h" + +int main(int argc, char **argv) +{ + dirty(16 MB); + clean(32 MB); + return interlude(); +} diff --git a/TP_SCR/TP_Memoire/heap.o b/TP_SCR/TP_Memoire/heap.o new file mode 100644 index 0000000..69c3f67 Binary files /dev/null and b/TP_SCR/TP_Memoire/heap.o differ diff --git a/TP_SCR/TP_Memoire/helpers.c b/TP_SCR/TP_Memoire/helpers.c new file mode 100644 index 0000000..1a2799f --- /dev/null +++ b/TP_SCR/TP_Memoire/helpers.c @@ -0,0 +1,36 @@ +#include +#include +#include +#include +#include "helpers.h" + +#include + +void randomize(char *buf, size_t n) +{ + assert(buf); + memset(buf, rand() & 0xff, n); +} + +void clean(size_t b) +{ + for (; b > 0; b -= 1 KB) + calloc(1 KB, sizeof(char)); +} + +void dirty(size_t b) +{ + for (; b > 0; b -= 1 KB) + randomize(calloc(1 KB, sizeof(char)), 1 KB); +} + +int interlude(void) +{ + pid_t pid = getpid(); + printf("pid %i\n", (int)pid); + printf("------------------------------------------\n" + "go check /proc/%i/smaps; I'll wait...\n" + "press when you're done\n", pid); + fgetc(stdin); + return 0; +} diff --git a/TP_SCR/TP_Memoire/helpers.h b/TP_SCR/TP_Memoire/helpers.h new file mode 100644 index 0000000..d8a774a --- /dev/null +++ b/TP_SCR/TP_Memoire/helpers.h @@ -0,0 +1,13 @@ +#ifndef _HELPERS_H +#define _HELPERS_H +#include + +#define KB * 1024 +#define MB * 1024 * 1024 + +void randomize(char *buf, size_t n); +void clean(size_t n); +void dirty(size_t n); +int interlude(void); + +#endif diff --git a/TP_SCR/TP_Memoire/helpers.o b/TP_SCR/TP_Memoire/helpers.o new file mode 100644 index 0000000..6bfd5f5 Binary files /dev/null and b/TP_SCR/TP_Memoire/helpers.o differ diff --git a/TP_SCR/TP_Memoire/huge.c b/TP_SCR/TP_Memoire/huge.c new file mode 100644 index 0000000..046aea0 --- /dev/null +++ b/TP_SCR/TP_Memoire/huge.c @@ -0,0 +1,12 @@ +#include "helpers.h" + +int main(int argc, char **argv) +{ + char *under = malloc(96 KB); + randomize(under, 96 KB); + + char *over = malloc(256 KB); + randomize(over, 256 KB); + + return interlude(); +} diff --git a/TP_SCR/TP_Memoire/memoire.c b/TP_SCR/TP_Memoire/memoire.c new file mode 100644 index 0000000..7cfecd5 --- /dev/null +++ b/TP_SCR/TP_Memoire/memoire.c @@ -0,0 +1,27 @@ +/* adresses virtuelles d'un processus */ + +#include +#include +#include +#include +#include + +int t[1000] = {[0 ... 999] = 2}; + +int main(int argc, char * argv[]) +{ + int i=3; + static int j = 3; + char * m = (char*)malloc(1); + printf("je suis le pid %d\n\n",getpid()); + /* ------- Affichage des adresses --------*/ + printf("main\t\t=\t%p\n",main); + printf("gettimeofday\t=\t%p\n",gettimeofday); + printf("&argc\t\t=\t%p\n",&argc); + printf("&i\t\t=\t%p\n",&i); + printf("&j\t\t=\t%p\n",&j); + printf("t\t\t=\t%p\n",t); + printf("m\t\t=\t%p\n",m); + + getchar(); +} \ No newline at end of file diff --git a/TP_SCR/TP_Memoire/mmap.c b/TP_SCR/TP_Memoire/mmap.c new file mode 100644 index 0000000..ac62144 --- /dev/null +++ b/TP_SCR/TP_Memoire/mmap.c @@ -0,0 +1,38 @@ +#include "helpers.h" +#include +#include +#include + + +int main(int argc, char **argv) +{ + /* inert map (never modified) */ + char *inert = mmap(NULL, 16 KB, + PROT_READ|PROT_WRITE, + MAP_ANONYMOUS|MAP_PRIVATE, + -1, 0); + /* anonymous, private mmap */ + char *anon_priv = mmap(NULL, 32 KB, + PROT_READ|PROT_WRITE, + MAP_ANONYMOUS|MAP_PRIVATE, + -1, 0); + randomize(anon_priv, 32 KB); + + /* anonymous, shared map */ + char *anon_shared = mmap(NULL, 64 KB, + PROT_READ|PROT_WRITE, + MAP_ANONYMOUS|MAP_SHARED, + -1, 0); + randomize(anon_shared, 64 KB); + + /* private, file-backed map */ + int fd = open("data/256k", O_RDWR); + assert(fd >= 0); + char *file = mmap(NULL, 256 KB, + PROT_READ|PROT_WRITE, + MAP_PRIVATE, + fd, 0); + randomize(file, 128 KB); + + return interlude(); +} diff --git a/TP_SCR/TP_Memoire/null.c b/TP_SCR/TP_Memoire/null.c new file mode 100644 index 0000000..fbd46f7 --- /dev/null +++ b/TP_SCR/TP_Memoire/null.c @@ -0,0 +1,6 @@ +#include "helpers.h" + +int main(int argc, char **argv) +{ + return interlude(); +} diff --git a/TP_SCR/TP_Memoire/stack.c b/TP_SCR/TP_Memoire/stack.c new file mode 100644 index 0000000..700eef1 --- /dev/null +++ b/TP_SCR/TP_Memoire/stack.c @@ -0,0 +1,8 @@ +#include "helpers.h" + +int main (int argc, char **argv) +{ + char buf[28 KB] = {0}; + randomize(buf, 28 KB); + return interlude(); +} diff --git a/TP_SCR/TP_Memoire/tp-reponses-yolou.txt b/TP_SCR/TP_Memoire/tp-reponses-yolou.txt new file mode 100644 index 0000000..96ecb34 --- /dev/null +++ b/TP_SCR/TP_Memoire/tp-reponses-yolou.txt @@ -0,0 +1,180 @@ +TP1 MEMOIRE - YOLOU + +1. Ce que je dois comprendre sur le code le processus possède une mémoire qui contient des adresses virtuelles +vdsco ce sont des appels systeme y'en a que 4 get of day c'est un appel systeme, le prgramme considere que c'est un appel systeme mais ca n'utilise pas l'organisation du noyaux (sauvegarde, rechargement etc...) + +[yolou@salle223-10 TP_Memoire]$ ./a.out +je suis le pid 23540 + +main = 0x559308d20179 +gettimeofday = 0x7f08d7032870 +&argc = 0x7ffd6bca825c +&i = 0x7ffd6bca826c +&j = 0x559308d24000 +t = 0x559308d23060 +m = 0x559320a82310 +^Z +[1]+ Stopped ./a.out +[yolou@salle223-10 TP_Memoire]$ cat /proc/23540/maps + +adresse début adresse fin perms offset devices inode chemin +559308d1f000-559308d20000 r--p 00000000 00:3e (périphèrique) 70729069 /export/home/info-but24/yolou/BUT2/TP_SCR/TP_Memoire/a.out +559308d20000-559308d21000 r-xp 00001000 00:3e 70729069 /export/home/info-but24/yolou/BUT2/TP_SCR/TP_Memoire/a.out +559308d21000-559308d22000 r--p 00002000 00:3e 70729069 /export/home/info-but24/yolou/BUT2/TP_SCR/TP_Memoire/a.out +559308d22000-559308d23000 r--p 00002000 00:3e 70729069 /export/home/info-but24/yolou/BUT2/TP_SCR/TP_Memoire/a.out +559308d23000-559308d25000 rw-p 00003000 00:3e 70729069 /export/home/info-but24/yolou/BUT2/TP_SCR/TP_Memoire/a.out +559320a82000-559320aa3000 rw-p 00000000 00:00 0 [heap] +7f08d6c00000-7f08d6c24000 r--p 00000000 103:05 6028 /usr/lib/libc.so.6 +7f08d6c24000-7f08d6d96000 r-xp 00024000 103:05 6028 /usr/lib/libc.so.6 +7f08d6d96000-7f08d6e05000 r--p 00196000 103:05 6028 /usr/lib/libc.so.6 +7f08d6e05000-7f08d6e09000 r--p 00204000 103:05 6028 /usr/lib/libc.so.6 +7f08d6e09000-7f08d6e0b000 rw-p 00208000 103:05 6028 /usr/lib/libc.so.6 +7f08d6e0b000-7f08d6e13000 rw-p 00000000 00:00 0 +7f08d6ffb000-7f08d7000000 rw-p 00000000 00:00 0 +7f08d702c000-7f08d7030000 r--p 00000000 00:00 0 [vvar] +7f08d7030000-7f08d7032000 r--p 00000000 00:00 0 [vvar_vclock] +7f08d7032000-7f08d7034000 r-xp 00000000 00:00 0 [vdso] +7f08d7034000-7f08d7035000 r--p 00000000 103:05 6019 /usr/lib/ld-linux-x86-64.so.2 (=> mappage dans le disque dur, quand on invoque le printf etc c'est ici qu'il vient ressourcer) +7f08d7035000-7f08d705f000 r-xp 00001000 103:05 6019 /usr/lib/ld-linux-x86-64.so.2 +7f08d705f000-7f08d706d000 r--p 0002b000 103:05 6019 /usr/lib/ld-linux-x86-64.so.2 +7f08d706d000-7f08d706f000 r--p 00039000 103:05 6019 /usr/lib/ld-linux-x86-64.so.2 +7f08d706f000-7f08d7070000 rw-p 0003b000 103:05 6019 /usr/lib/ld-linux-x86-64.so.2 +7f08d7070000-7f08d7071000 rw-p 00000000 00:00 0 +7ffd6bc89000-7ffd6bcaa000 rw-p 00000000 00:00 0 [stack] +ffffffffff600000-ffffffffff601000 --xp 00000000 00:00 0 [vsyscall] + + +Bibliotheque dynamique dln windows / +Bibl. standard C libsc : + +. La libc contient les fonctions comme printf, malloc, exit... + +. PLusieurs segment comme : + + . r-xp = code éxécutable de la bibliothèque + . r-p = données en lecture seule + . rw-p = données globales de la libc. + +[vvar] : + +. Virtual Variables : zone créée par le noyau pour partager certaines infos comme l'horloge entre le noyau et ton programme +. Lecture seule. + +id-linux (linker dynamique) : + +Le chargeur dynamique : c'est lui qui charge les biblio partagé comme libc en mémoire +Il est obligatoire pour éxécuter un programme avec des bibliothèques dynamiques + +[stack] (pile) : + +La pile du processus +Sert a stocker : variable locales, adresse de retour des fonctions, parametres. +Grandit quand tu appelles des fonctions imbriquées. + + +Adresse-debut/fin = adresses virtuelles +il y a les perms : r-xp = lecture + execution -> segment code +r-p = lecture seul -> constantes +rw-p = lecture/écriture -> variables globales initialisées +Virtual Dymanic Shared Object => Petite zone pour accelerer certains appels systeme (ex gettimeofday) = vsdo + +Heap(tas) : + +. zone utilisée pour les allocations dynamiques(malloc,callocfree) +. grandit ou rétrécit selon les besoins. + + +2) + +Apres avoir fait la commande pmap -x , j'observe que il y'a plusieurs adresses : + +Addresss -> l'adresse virtuelle oû commence ce segment mémoire +Kbytes -> taille totale du segments (réservée) +RSS (Resident Set Size) -> combien de mémoire de ce segment est réellement physqiuement en RAM (le reste peut etre sur disque ou pas encore utilisé) pour savoir si elle sont mappé physiquement +Dirty -> nombre de Kilo octet modifiés (non partagés, spécifiques à mon processus) +Mode -> permission du segment : +r, w, x, s ou p : partagé (shared) ou prié (private) +Mapping -> d'oû vient ce segment (exécutable a.out, biblliothèque, [stack], [heap], [anone] pour anonyme) + +Typiquement avec le prgramme du premier exo : + +113416: ./a.out +Address Kbytes RSS Dirty Mode Mapping +000055c06db68000 4 4 0 r---- a.out +000055c06db69000 4 4 0 r-x-- a.out +000055c06db6a000 4 4 0 r---- a.out +000055c06db6b000 4 4 4 r---- a.out +000055c06db6c000 8 8 8 rw--- a.out +000055c091c49000 132 4 4 rw--- [ anon ] +00007f0f90000000 144 144 0 r---- libc.so.6 +00007f0f90024000 1480 1060 0 r-x-- libc.so.6 +00007f0f90196000 444 128 0 r---- libc.so.6 +00007f0f90205000 16 16 16 r---- libc.so.6 +00007f0f90209000 8 8 8 rw--- libc.so.6 +00007f0f9020b000 32 20 20 rw--- [ anon ] +00007f0f903f9000 20 12 12 rw--- [ anon ] +00007f0f9042b000 16 0 0 r---- [ anon ] +00007f0f9042f000 8 0 0 r---- [ anon ] +00007f0f90431000 8 8 0 r-x-- [ anon ] +00007f0f90433000 4 4 0 r---- ld-linux-x86-64.so.2 +00007f0f90434000 168 168 0 r-x-- ld-linux-x86-64.so.2 +00007f0f9045e000 56 56 0 r---- ld-linux-x86-64.so.2 +00007f0f9046c000 8 8 8 r---- ld-linux-x86-64.so.2 +00007f0f9046e000 4 4 4 rw--- ld-linux-x86-64.so.2 +00007f0f9046f000 4 4 4 rw--- [ anon ] +00007ffc6a1a9000 132 12 12 rw--- [ stack ] +ffffffffff600000 4 0 0 --x-- [ anon ] +---------------- ------- ------- ------- +total kB 2712 1680 100 + + +1. a.out (mon prgm) + +Aplusieurs lignes avec a.out : + +r-x -> segment code exécutable +r-- -> données en lecture seule (constates) +rw- -> données globales modifiables + +2. [anone] (anonyme) + +Memoire réservée par le processus sans fichier associé +Ici ca peut etre des petit malloc + +3. libc.so.6 + +La bibliothèque standard du C (printf,malloc, ...). +.r-x -> code éxécutable de la libc +. rw- -> données de la libc + +4. Id-linux x86-64.so.2 + +Le chargeur dynamique qui charge les bibliothèque +Lui aussi a code (r-x) et données (rw-). + +total kB : 2712 + +Le processus occupe environ 2.7 Mo au total. + +Sur ces 2712 Ko, 1680 Ko sont effectivement en RAM (RSS). + +Le reste peut etre reservé mais pas encore utilisé. + +Généralement, le programme est découpé en segments mémoire : code, données, heap, pile, bibliothéques, mappings, anonymes. + +La commande pmap -x permet de voir : + +combien est réservé (Kbvtes) le processus, +cobmien est vraiment en RAM (RSS), +combien est spécifique au processus (Dirty) +et pour finir l'origine du segment (Mapping) + +Ca illustre le cours le cours sur l'organisation mémoire d'un processus et la gestion par Linux via mémoire vituelle + MMU (MMU pour memory management unit), un composant informatique responsable de l'accès à la mémoire demandée par le processeur). + +Pour le code buf : + +Pour les segments "buf" (l'executable) + +Plusierus petites lignes de 4kB avec buf : + +r-x -> code éxécutable \ No newline at end of file diff --git a/TP_SCR/TP_SIGNAUX/parexec b/TP_SCR/TP_SIGNAUX/parexec new file mode 100755 index 0000000..2797536 Binary files /dev/null and b/TP_SCR/TP_SIGNAUX/parexec differ diff --git a/TP_SCR/TP_SIGNAUX/parexec.c b/TP_SCR/TP_SIGNAUX/parexec.c new file mode 100644 index 0000000..5220177 --- /dev/null +++ b/TP_SCR/TP_SIGNAUX/parexec.c @@ -0,0 +1,36 @@ +#include +#include +#include +#include +#include +#include + +int main(int argc, char* argv[]) +{ + // TODO + assert(argc >= 2); + +// char prog = argv[1]; + +for(int i=2; i tuer toutes les autres et arrêter. + */ + +#include // fprintf, perror +#include // atoi, calloc, free, EXIT_* +#include // fork, execlp, usleep +#include // wait, WIFSIGNALED, WTERMSIG +#include // kill +#include // errno + +int main(int argc, char *argv[]) { + // Vérifie la syntaxe minimale : prog + N + au moins 1 argument pour prog + if (argc < 4) { + fprintf(stderr, "Usage: %s prog N arg1 [arg2 ...]\n", argv[0]); + return EXIT_FAILURE; + } + + char *prog = argv[1]; // le programme à lancer (ex: "./rebours") + int N = atoi(argv[2]); // nombre maximum d'enfants en parallèle + if (N <= 0) { // N doit être strictement positif + fprintf(stderr, "N doit être > 0\n"); + return EXIT_FAILURE; + } + + int next = 3; // index du prochain argument à donner à prog (argv[3] au début) + int running = 0; // nombre d'enfants actuellement en cours d'exécution + int stop = 0; // =1 si on a détecté une fin anormale -> on arrête de relancer + + // Tableau des PIDs des enfants "actifs" (taille N). 0 signifie "case libre". + pid_t *slots = calloc((size_t)N, sizeof(pid_t)); + if (!slots) { perror("calloc"); return EXIT_FAILURE; } + + // ===== 1) Lancer tout de suite jusqu'à N enfants (ou jusqu'à épuisement des args) ===== + while (!stop && running < N && next < argc) { + pid_t pid = fork(); // crée un nouveau processus + if (pid == 0) { // dans l'enfant + execlp(prog, prog, argv[next], NULL); // remplace l'enfant par "prog argi" + _exit(127); // si exec échoue, sortir avec code 127 + } + if (pid < 0) { // fork a échoué dans le parent + perror("fork"); + stop = 1; // on arrête les lancements + break; + } + // Ici on est dans le parent et fork a réussi : on mémorise le PID dans une case libre 2 5 4 3 + for (int i = 0; i < N; ++i) { + if (slots[i] == 0) { // 0 = case libre + slots[i] = pid; // on range ce PID + break; + } + } + running++; // on a un enfant de plus + next++; // prochain argument à utiliser + } + + // ===== 2) Boucle principale : attendre les fins, réagir, et (éventuellement) relancer ===== + while (running > 0) { // tant qu'il reste des enfants actifs + int status = 0; + pid_t pid = wait(&status); // attendre qu'UN enfant se termine + if (pid < 0) { // erreur (souvent EINTR). On simplifie : on réessaie. + if (errno == EINTR){ + continue; + } + perror("wait"); + break; + } + + // Retirer ce PID du tableau des actifs (libérer sa case) + for (int i = 0; i < N; ++i) { + if (slots[i] == pid) { // on a trouvé la case de cet enfant + slots[i] = 0; // on marque la case vide + break; + } + } + running--; // un enfant en moins + + // ===== Q4 : si l'enfant s'est terminé à cause d'un SIGNAL ===== + if (WIFSIGNALED(status)) { + stop = 1; // ne plus lancer de nouveaux enfants + // D'abord demander gentiment aux autres de s'arrêter + for (int i = 0; i < N; ++i) { + if (slots[i] > 0) kill(slots[i], SIGTERM); + } + usleep(100000); // petite pause (100 ms) + // Puis forcer l'arrêt si certains sont encore vivants + for (int i = 0; i < N; ++i) { + if (slots[i] > 0) kill(slots[i], SIGKILL); + } + // on n'ajoute PAS de relance ici (stop=1) + } + + // ===== Relancer si : pas d'arrêt global ET il reste des arguments à lancer ===== + if (!stop && next < argc) { + pid_t npid = fork(); // crée un nouvel enfant + if (npid == 0) { // dans le nouvel enfant + execlp(prog, prog, argv[next], NULL); // lance "prog arg" + _exit(127); // si exec échoue + } + if (npid < 0) { // fork a échoué + perror("fork"); + stop = 1; // arrêter les relances + } else { + // ranger ce nouveau PID dans une case libre + for (int i = 0; i < N; ++i) { + if (slots[i] == 0) { slots[i] = npid; break; } + } + running++; // un enfant de plus en cours + next++; // passera à l'argument suivant lors de la prochaine relance + } + } + } + + free(slots); // libérer la mémoire + if (stop) return 1; // échec +return 0; // succès +} diff --git a/TP_SCR/TP_SIGNAUX/rebours b/TP_SCR/TP_SIGNAUX/rebours new file mode 100755 index 0000000..ce19bf6 Binary files /dev/null and b/TP_SCR/TP_SIGNAUX/rebours differ diff --git a/TP_SCR/TP_SIGNAUX/rebours.c b/TP_SCR/TP_SIGNAUX/rebours.c new file mode 100644 index 0000000..10114d8 --- /dev/null +++ b/TP_SCR/TP_SIGNAUX/rebours.c @@ -0,0 +1,19 @@ +#include +#include +#include +#include + +int main(int argc, char* argv[]) +{ + assert(argc == 2); + + int value = strtol(argv[1], NULL, 0); + pid_t p = getpid(); + printf("%d: debut\n",p); + for(int i = value; i>0; i--){ + printf("%d: %d\n",p,i); + sleep(1); + } + printf("%d: fin\n",p); + return EXIT_SUCCESS; +} diff --git a/TP_SCR/TP_Threads/a.out b/TP_SCR/TP_Threads/a.out new file mode 100755 index 0000000..7aaa120 Binary files /dev/null and b/TP_SCR/TP_Threads/a.out differ diff --git a/TP_SCR/TP_Threads/document-reponse-tp-thread-yolou.c b/TP_SCR/TP_Threads/document-reponse-tp-thread-yolou.c new file mode 100644 index 0000000..510a800 --- /dev/null +++ b/TP_SCR/TP_Threads/document-reponse-tp-thread-yolou.c @@ -0,0 +1,49 @@ +#include +#include +#include +#include + +#define NUM_THREADS 16 + +void *thread(void *thread_id) { + long id = ((long ) thread_id); + printf("Hello from thread %ld\n", id); + + return (void*)(2*id); +} + +int main() { + pthread_t threads[NUM_THREADS]; + int t[NUM_THREADS]; + long res; + for (long i = 0; i < NUM_THREADS; i++){ +// t[i] = i; + assert( pthread_create(&threads[i], NULL, thread, (void*)i) == 0); + } + + for (int i = 0; i < NUM_THREADS; i++){ + assert( pthread_join(threads[i],(void *)&res) == 0); + printf("%ld\n",res); + } + + return EXIT_SUCCESS; +} + + +// 1.L'execution est-elle correcte ? +// Non la version est fause +/*On voit parfois des IDS faux/repetés selon les exécutions. + +2. Si ce n'est pas le cas, expliquez pourquoi + +Probleme : +(Parce qu'on passe l'adresse d'une variable partagée (&i) qui change pendant la creation des threads. +Les threads lisent la meme adresse, pas uenc opie de la valeur -> condition de course + duree de vie/visibilité non garanties) +&i est l'adresse de la meme variable i de la bouche (une seule variable, qui change de valeur). +Les threads démarrent en parrallèle et lisent tous la meme adresse.h +Comme i continue de s'incrementer, le contenu vu par les threads depends du timing -> plusieurs threads peuvent lire la meme valeur, ou tous lire la valeur finale(16); +Resultat : sorties incorrects / pas vraiment deterministes(certains IDs manquent, d'autres répétes).*/ + + +// Exercie 2 : + diff --git a/TP_SCR/TP_Threads/exo2somme.c b/TP_SCR/TP_Threads/exo2somme.c new file mode 100644 index 0000000..e2e9c3b --- /dev/null +++ b/TP_SCR/TP_Threads/exo2somme.c @@ -0,0 +1,29 @@ +#include +#include +#include +#include + + +int main(int argc, char *argv[]){ + + +if(argc != 3) { + +fprintf(stderr, "Usage :%s \n",argv[0]); +return 1; + + +} + long N = strtol(argv[1], NULL,0); + long M = strtol(argv[2], NULL,0); + + + + + + + + + + +} \ No newline at end of file diff --git a/TP_SCR/TP_Tubes/a.out b/TP_SCR/TP_Tubes/a.out new file mode 100755 index 0000000..5e0ecdf Binary files /dev/null and b/TP_SCR/TP_Tubes/a.out differ diff --git a/TP_SCR/TP_Tubes/ex1.c b/TP_SCR/TP_Tubes/ex1.c new file mode 100644 index 0000000..3934a06 --- /dev/null +++ b/TP_SCR/TP_Tubes/ex1.c @@ -0,0 +1,72 @@ +#include +#include +#include +#include + +int main(void) { + int p[2]; // tube : p[0] lecture, p[1] écriture + pid_t pf1, pf1f1, pf2; + + pipe(p); + + // --- créer Pf1 --- + pf1 = fork(); + if (pf1 == 0) { + // --- créer Pf1f1 --- + pf1f1 = fork(); + if (pf1f1 == 0) { + // Pf1f1 : lecteur + close(p[1]); // ne garde que la lecture + while (1) { + pid_t v; + read(p[0], &v, sizeof(v)); + int valeur = read(p[0], &v, sizeof(v)); + printf(" %d received %d\n", (int)getpid(), (int)v); + fflush(stdout); + sleep(1); + } + } + + close(p[0]); + close(p[1]); + while (1){ + pause(); + } + } + + // --- créer Pf2 --- + pf2 = fork(); + if (pf2 == 0) { + //Pf2 : écrivain + close(p[0]); // ne garde que l’écriture + pid_t cmoi = getpid(); + while (1) { + printf("%d sent %d\n", (int)cmoi, (int)cmoi); + fflush(stdout); + write(p[1], &cmoi, sizeof(cmoi)); + sleep(3); + } + } + + //P COMME PAPA + close(p[0]); + close(p[1]); + while (1){ + + pause(); + + } + printf(valeur); +} + + +// Question 3 : (Envoyez le signal SIGSTOP à Pf2, puis le signal SIGCONT à Pf2) +// Pour le premiere comportement quand je stop le processus pf2 avec un kill -STOP 211912 alors mon programme est comme en pause c'est normal car l'action du prcessus de pf1f1 dependait du tube qui relie donc comme il a rien a recevoir il attend. +// Puis quand j'utilise SIGCONT le programme se debloque car le pf2 n'est plus gélé donc le pf1f1 peut lire les informations qu'on lui donne. + +//(Envoyez le signal SIGSTOP à Pf1f1, puis le signal SIGCONT à Pf1f1.) +//Lors du moment ou j'ai envoyer ce signal le processus pf2 continue d'envoyé ses information au processus pf1f1 sauf que lui comme il est gelé ne reagit pas à sa "requete". +// Puis ainsi il rattrape tous ces requetes qui n'a pas pu repondre et du coup comme il est plus rapide que pf2 a la relance il a recup tous les pid possible comme dans le tube il etait rempli de ça donc il a lu les pid qui était la dans le tube. + + + diff --git a/TP_SCR/TP_Tubes/ex11.c b/TP_SCR/TP_Tubes/ex11.c new file mode 100644 index 0000000..19d4267 --- /dev/null +++ b/TP_SCR/TP_Tubes/ex11.c @@ -0,0 +1,45 @@ +#include + +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]){ + + + +if(argc != 3) { + +fprintf(stderr, "Usage :%s \n",argv[0]); +return 1; + + +} + +int maxv = atoll(argv[1]); +int n = atoi(argv[2]); + +int p[2]; +pipe(p); + + +for(int k=0; k