tp04
This commit is contained in:
parent
3c2432be24
commit
010fba24e0
BIN
ASR3.1/TP04 processus recouvrement/a.out
Executable file
BIN
ASR3.1/TP04 processus recouvrement/a.out
Executable file
Binary file not shown.
47
ASR3.1/TP04 processus recouvrement/ouvrir.c
Normal file
47
ASR3.1/TP04 processus recouvrement/ouvrir.c
Normal file
@ -0,0 +1,47 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
typedef struct assoc {
|
||||
char * ext;
|
||||
char * exe;
|
||||
} assoc;
|
||||
|
||||
|
||||
|
||||
int main(int argc, char const *argv[]){
|
||||
|
||||
|
||||
assoc assocs [] = {
|
||||
{"pdf","/usr/bin/xpdf"},
|
||||
{"html","/usr/bin/firefox"},
|
||||
{"c","/usr/bin/vim"}
|
||||
/* etc */
|
||||
};
|
||||
|
||||
|
||||
if (argc < 2){
|
||||
printf("Usage : %s <file>\n", argv[2]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
|
||||
char * extension = strrchr (argv[1], '.') + 1; // pour récupérer la chaine suivant (+1)
|
||||
|
||||
if (extension == NULL){
|
||||
printf ("Pas d'extensions \n");
|
||||
} else {
|
||||
printf ("extention : %s\n", extension);
|
||||
}
|
||||
|
||||
|
||||
for (int i = 0; i < assoc; i++){
|
||||
if (strcmp (extension, assocs[i]) == 0){
|
||||
printf("ok\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return EXIT_FAILURE;
|
||||
}
|
57
ASR3.1/TP04 processus recouvrement/sum.c
Normal file
57
ASR3.1/TP04 processus recouvrement/sum.c
Normal file
@ -0,0 +1,57 @@
|
||||
#include <stdio.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <time.h>
|
||||
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_partielle(long int * sommes,long int n,long int N,long int M)
|
||||
{
|
||||
long int S = 0;
|
||||
for(long int i = n;i<=N;i+=M)
|
||||
S+=i;
|
||||
|
||||
//printf("%d = %ld\n",getpid(),S);
|
||||
sommes[n-1] = S;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
long int N,
|
||||
M,
|
||||
S=0;
|
||||
long int * sommes = NULL;
|
||||
double start,end;
|
||||
|
||||
start = tstamp();
|
||||
assert(argc == 3);
|
||||
N=strtol(argv[1],NULL,0);
|
||||
M=strtol(argv[2],NULL,0);
|
||||
|
||||
sommes = (long int *) mmap(NULL,M*sizeof(long int),
|
||||
PROT_WRITE,MAP_ANONYMOUS|MAP_SHARED,-1,0);
|
||||
|
||||
for (long int i = 0;i<M;i++){
|
||||
if (fork() == 0){
|
||||
|
||||
somme_partielle(sommes,i+1,N,M);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
}
|
||||
for(long int i = 0;i<M;i++)
|
||||
wait(NULL);
|
||||
|
||||
for(long int i = 0;i<M;i++)
|
||||
S+=sommes[i];
|
||||
end = tstamp();
|
||||
printf("time = %lf somme = %ld\n",end - start,S);
|
||||
assert (S == N*(N+1)/2);
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user