TP6
This commit is contained in:
parent
458013fe74
commit
82cf22952d
30
SCR3.1/TP6/ex1.c
Normal file
30
SCR3.1/TP6/ex1.c
Normal file
@ -0,0 +1,30 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <pthread.h>
|
||||
#include <assert.h>
|
||||
|
||||
#define NUM_THREADS 16
|
||||
|
||||
void *thread(void *thread_id)
|
||||
{
|
||||
int id = *((int*) thread_id);
|
||||
printf("Hello from thread %d\n", id);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int th[NUM_THREADS];
|
||||
pthread_t threads[NUM_THREADS];
|
||||
for (int i = 0; i < NUM_THREADS; i++){
|
||||
th[i] = i;
|
||||
assert( pthread_create(&threads[i], NULL, thread, &th[i]) == 0);
|
||||
}
|
||||
|
||||
for (int i = 0; i < NUM_THREADS; i++)
|
||||
assert( pthread_join(threads[i], NULL) == 0);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
57
SCR3.1/TP6/max.c
Normal file
57
SCR3.1/TP6/max.c
Normal file
@ -0,0 +1,57 @@
|
||||
#include <unistd.h>
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <pthread.h>
|
||||
|
||||
struct numbers {
|
||||
int n1;
|
||||
int n2;
|
||||
int max;
|
||||
};
|
||||
|
||||
typedef struct numbers numbers;
|
||||
|
||||
void * max(void * n){
|
||||
numbers *comp = (numbers *) n;
|
||||
if (comp->n1 > comp->n2)
|
||||
comp->max = comp->n1;
|
||||
else
|
||||
comp->max = comp->n2;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv){
|
||||
if (argc != 9){
|
||||
printf("Usage : %s <n1> <n2> <n3> <n4> <n5> <n6> <n7> <n8>\n", argv[0]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
pthread_t threads[4];
|
||||
numbers duels[4];
|
||||
for(int i = 0; i < 4; i++){
|
||||
duels[i].n1 = (int) strtod(argv[2*i+1], NULL);
|
||||
duels[i].n2 = (int) strtod(argv[2*(i+1)], NULL);
|
||||
duels[i].max = 0;
|
||||
pthread_create(&threads[i], NULL, max, (void*) (duels + i));
|
||||
}
|
||||
for (int i = 0; i < 4; i++){
|
||||
pthread_join(threads[i], NULL);
|
||||
}
|
||||
numbers demi_f[2];
|
||||
for (int i = 0;i < 2; i++){
|
||||
demi_f[i].n1 = duels[i*2].max;
|
||||
demi_f[i].n2 = duels[i*2+1].max;
|
||||
demi_f[i].max = 0;
|
||||
pthread_create(&threads[i], NULL, max, (void*) (duels + i));
|
||||
}
|
||||
for (int i = 0; i < 2;i++){
|
||||
pthread_join(threads[i], NULL);
|
||||
}
|
||||
|
||||
numbers finale = {0};
|
||||
finale.n1 = demi_f[0].max;
|
||||
finale.n2 = demi_f[1].max;
|
||||
pthread_create(&threads[0], NULL, max, (void*) (&finale));
|
||||
pthread_join(threads[0], NULL);
|
||||
printf("Le plus grand des huits nombres est : %d", finale.max);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
10
SCR3.1/TP6/stack/Makefile
Normal file
10
SCR3.1/TP6/stack/Makefile
Normal file
@ -0,0 +1,10 @@
|
||||
stack.o : stack.c
|
||||
gcc -c stack.c
|
||||
|
||||
main.o : main.c stack.h
|
||||
gcc -c main.c
|
||||
|
||||
main : main.o stack.o
|
||||
gcc -o main main.o stack.o
|
||||
|
||||
all : main
|
21
SCR3.1/TP6/stack/main.c
Normal file
21
SCR3.1/TP6/stack/main.c
Normal file
@ -0,0 +1,21 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include "stack.h"
|
||||
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
|
||||
int val;
|
||||
stack_t * s=stack_create(20);
|
||||
|
||||
for(int i=0;i<20; i++)
|
||||
stack_push(s,i);
|
||||
|
||||
for(int i=0;i<20; i++)
|
||||
stack_pop(s,&val);
|
||||
|
||||
return 0;
|
||||
}
|
32
SCR3.1/TP6/stack/stack.c
Normal file
32
SCR3.1/TP6/stack/stack.c
Normal file
@ -0,0 +1,32 @@
|
||||
#include "stack.h"
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
|
||||
struct stack_t {
|
||||
/*
|
||||
votre implantation
|
||||
|
||||
*/
|
||||
};
|
||||
|
||||
stack_t * stack_create( int max_size)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
int stack_destroy(stack_t * s)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
int stack_push(stack_t *s,int val)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
int stack_pop(stack_t *s,int * val)
|
||||
{
|
||||
|
||||
}
|
||||
|
12
SCR3.1/TP6/stack/stack.h
Normal file
12
SCR3.1/TP6/stack/stack.h
Normal file
@ -0,0 +1,12 @@
|
||||
#ifndef _STACK_INT_H
|
||||
#define _STACK_INT_H
|
||||
|
||||
typedef struct stack_t stack_t;
|
||||
|
||||
stack_t * stack_create(int max_size);
|
||||
int stack_destroy(stack_t *s);
|
||||
int stack_push(stack_t *s, int val);
|
||||
int stack_pop(stack_t *s,int *val);
|
||||
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user