This commit is contained in:
2023-10-23 13:07:05 +02:00
parent 4f9ed8483e
commit 667dae6f1a
26 changed files with 469 additions and 45 deletions

20
SCR/TP05/ex1.1.c Normal file
View File

@@ -0,0 +1,20 @@
#define _GNU_SOURCE
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
int main(int argc, char *argv[])
{
int t[2];
int size;
assert(pipe(t) == 0);
size = fcntl(t[0],F_GETPIPE_SZ);
printf("size = %d\n",size);
return 0;
}

24
SCR/TP05/ex1.2.c Normal file
View File

@@ -0,0 +1,24 @@
#define _GNU_SOURCE
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
// ls -i -l /tmp >> log
//
int main(int argc, char *argv[])
{
int redirection = open("./log",O_WRONLY | O_APPEND | O_CREAT , 0644);
assert(redirection >= 0);
// dup2 ferme 1, et duplique redirection
dup2(redirection,1);
close(redirection);
execlp("ls","ls","-i","-l","/tmp",NULL);
assert(0);
return 0;
}

76
SCR/TP05/prime.c Normal file
View File

@@ -0,0 +1,76 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
void filter(int p)
{
while(1){
int n;
ssize_t nb_read = read(0,&n,sizeof(int));
if (nb_read <= 0)
break;
if ((n%p)!=0)
write(1,&n,sizeof(int));
}
}
void seq(int n)
{
for (int i = 2; i <=n; i++)
write(1,&i,sizeof(int));
}
void chef(void)
{
while(1){
int p;
pid_t f;
int t[2];
ssize_t nb_read = read(0,&p,sizeof(int));
if (nb_read <= 0)
break;
printf("new prime %d\n",p);
pipe(t);
f = fork();
if (f == 0){
close(t[0]);
dup2(t[1],1);
close(t[1]);
filter(p);
exit(0);
}
close(t[1]);
dup2(t[0],0);
close(t[0]);
}
}
int main(int argc, char *argv[])
{
pid_t p;
struct sigaction sa = {0};
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_NOCLDWAIT;
sigaction(SIGCHLD,&sa,NULL);
int t[2];
int N = strtol(argv[1],NULL,0);
pipe(t);
p = fork();
if (p == 0){
close(t[0]);
dup2(t[1],1);
close(t[1]);
seq(N);
exit(0);
}
close(t[1]);
dup2(t[0],0);
close(t[0]);
chef();
return 0;
}

View File

@@ -2,62 +2,102 @@
#include <stdlib.h>
#include <pthread.h>
#include <assert.h>
#include <time.h>
#define ELIMINE -1
typedef struct duel{
int participant1;
int participant2;
int* liste;
int debut;
int tailleGroupe;
} duel;
void *tournois(void *thread_id) {
int id = *((int *) thread_id);
printf("Hello from thread %d\n", id);
void affichage(int* liste, int capacite){
int i;
for(i=0; i<capacite; i++){
if (liste[i] != ELIMINE){
printf("%d ",liste[i]);
}
}
printf("\n");
}
int* creerListe(int taille){
int* liste = (int*) malloc(taille*sizeof(int));
int i;
for (i=0; i<taille; i++){
liste[i] = (rand()%100);
}
return liste;
}
int plusPetit(int* liste, int debut, int nb){
int plusGrand = -1;
int i;
int participant1 =- 1;
int indiceParticipant1;
for (i=0; i<nb; i++){
if (liste[debut+i] != ELIMINE){
if (participant1 == -1){
participant1 = liste[debut+i];
indiceParticipant1 = i;
}
else{
if (liste[debut+i] > participant1){
return debut + indiceParticipant1;
}
else{
return debut + i;
}
}
}
}
return -1;
}
void *tournois(void *argument) {
duel duo = *((duel*) argument);
int i;
int perdant = plusPetit(duo.liste, duo.debut, duo.tailleGroupe);
duo.liste[perdant] = -1;
return NULL;
}
int puissance2(int n){
for (int p=1; p<n; p*=2){}
int p;
for (p=1; p<n; p*=2){}
if (p==n){
return 1;
}
return 0;
}
duel* Formercouple(participant*, nbParticipant){
int i;
int c;
duel couple[nbParticipant/2];
for (i=0; i<nbParticipant; i++){
if (participant[])
}
}
int main(int argc, char** argv[]) {
int participant[argc];
int nbDuel = argc/2;
int nbParticipant = argc;
pthread_t threads[nbDuel];
int nbParticipant = argc-1;
int nbDuel = nbParticipant/2;
int* listeParticipant;
assert(puissance2(argc)==1);
assert(puissance2(nbParticipant)==1);
srand(time(NULL));
listeParticipant = creerListe(nbParticipant);
affichage(listeParticipant, nbParticipant);
for (int i = 0; i < argc; i++){
participant->nom = argv[i];
participant->id = i;
}
while (nbDuel > 1){
for (int i = 0; i < nbDuel; i++){
duel[nbDuel] tabDuel;
participant participant2;
for (int x=0, y=0, )
participant duel[] = {participant[i], participant[i+1]};
assert( pthread_create(&threads[i], NULL, tournois, &participant[i]) == 0);
while (nbDuel>=1){
pthread_t threads[nbDuel];
duel argument[nbDuel];
int i;
for (i=0; i<nbDuel; i++){
argument[i].liste = listeParticipant;
argument[i].debut = i*(nbParticipant/nbDuel);
argument[i].tailleGroupe = nbParticipant/nbDuel;
}
for (int i = 0; i < NUM_THREADS; i++){
for (i=0; i<nbDuel; i++){
assert( pthread_create(&threads[i], NULL, tournois, &argument[i]) == 0);
}
for (i=0; i<nbDuel; i++){
assert( pthread_join(threads[i], NULL) == 0);
}
nbParticipant /= 2;
affichage(listeParticipant, nbParticipant);
nbDuel /= 2;
}

60
SCR/TP06/ex3.c Normal file
View File

@@ -0,0 +1,60 @@
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <assert.h>
#include <time.h>
typedef struct structure{
int debut;
int fin;
int saut;
int* resultat;
} structure;
static inline double tstamp(void)
{
struct timespec tv;
clock_gettime(CLOCK_REALTIME, &tv);
return tv.tv_sec + tv.tv_nsec * 1.0e-9;
}
void *somme(void *argument) {
structure data = *((structure *) argument);
for (; data.debut<=data.fin; data.debut += data.saut){
*data.resultat += data.debut;
}
return NULL;
}
int main(int argc, char* argv[]) {
assert(argc-1 == 2);
int limite = (int) strtol(argv[1], NULL, 0);
int nbThread = (int) strtol(argv[2], NULL, 0);
int sommeFinal = 0;
double startTime, endTime;
int resultatThread[nbThread];
pthread_t threads[nbThread];
structure argument[nbThread];
for (int i = 0; i < nbThread; i++){
argument[i].debut = i;
argument[i].fin = limite;
argument[i].saut = nbThread;
resultatThread[i] = 0;
argument[i].resultat = &resultatThread[i];
}
startTime = tstamp();
for (int i = 0; i < nbThread; i++){
assert( pthread_create(&threads[i], NULL, somme, &argument[i]) == 0);
}
for (int i = 0; i < nbThread; i++){
assert( pthread_join(threads[i], NULL) == 0);
sommeFinal += *argument[i].resultat;
}
endTime = tstamp();
printf("somme : %d\ntemps : %lf\n", sommeFinal, endTime-startTime);
return EXIT_SUCCESS;
}

BIN
SCR/TP06/exe Executable file

Binary file not shown.