This commit is contained in:
lefevre 2021-11-08 17:46:19 +01:00
parent 3c2432be24
commit 010fba24e0
3 changed files with 104 additions and 0 deletions

Binary file not shown.

View 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;
}

View 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;
}