Commit 5be5d85e429b380d0b0bb5fe8721c6dd1e55e58b
1 parent
cb716d77
tp processus
Showing
58 changed files
with
1102 additions
and
19 deletions
Show diff stats
... | ... | @@ -0,0 +1,17 @@ |
1 | +.PHONY: clean,real-clean | |
2 | +LD = gcc | |
3 | +CC = gcc | |
4 | +CFLAGS = -Wall -W -Werror | |
5 | +SRC=$(wildcard *.c) | |
6 | +OBJ=$(SRC:.c=.o) | |
7 | +prog : $(OBJ) | |
8 | + $(LD) -o $@ $^ | |
9 | + | |
10 | +%.o : %.c | |
11 | + $(CC) $(CFLAGS) -c $^ | |
12 | + | |
13 | +clean : | |
14 | + rm -f *.o | |
15 | + | |
16 | +real-clean : clean | |
17 | + rm -f prog | |
0 | 18 | \ No newline at end of file | ... | ... |
... | ... | @@ -0,0 +1,31 @@ |
1 | +#include <stdio.h> | |
2 | +#include <stdlib.h> | |
3 | +#include <sys/wait.h> | |
4 | +#include <unistd.h> | |
5 | + | |
6 | +int main(){ | |
7 | + int cr, cpt=0; | |
8 | + if((cr=fork())<0){ | |
9 | + perror("erreur exec du fork"); | |
10 | + exit(1); | |
11 | + } | |
12 | + for(;;){ | |
13 | + if(cr ==0){ | |
14 | + printf("fils"); | |
15 | + sleep(1); | |
16 | + } else { | |
17 | + int r, pid; | |
18 | + printf("pere"); | |
19 | + sleep(2); | |
20 | + cpt++; | |
21 | + if (cpt==5) { | |
22 | + exit(0); | |
23 | + } | |
24 | + if((pid= wait(&r)) <0){ | |
25 | + perror("erreur exec du wait"); | |
26 | + exit(1); | |
27 | + } | |
28 | + } | |
29 | + } | |
30 | + return 0; | |
31 | +} | ... | ... |
No preview for this file type
No preview for this file type
... | ... | @@ -0,0 +1,111 @@ |
1 | +#include <stdio.h> | |
2 | +#include <stdlib.h> | |
3 | +#include <sys/wait.h> | |
4 | +#include <unistd.h> | |
5 | +/* | |
6 | +int main(){ | |
7 | + int cr, ent=0; | |
8 | + if((cr=fork())<0){ | |
9 | + perror("erreur exec du fork"); | |
10 | + exit(1); | |
11 | + } | |
12 | + if(cr == 0){ | |
13 | + printf("dans le fils, val de ent : %d\n", ent); | |
14 | + ent ++; | |
15 | + printf("dans le fils, val de ent après incrémentation: %d\n", ent); | |
16 | + } else { | |
17 | + int r, pid; | |
18 | + printf("dans le père, val de ent : %d\n", ent); | |
19 | + ent++; | |
20 | + printf("dans le père, val de ent après incrémentation: %d\n", ent); | |
21 | + if((pid= wait(&r)) <0){ | |
22 | + perror("erreur exec du wait"); | |
23 | + exit(1); | |
24 | + } | |
25 | + } | |
26 | + return 0; | |
27 | +} | |
28 | + | |
29 | +int main(){ | |
30 | + int fork1, fork2; | |
31 | + if((fork1=fork())<0){ | |
32 | + perror("erreur exec du fork"); | |
33 | + exit(1); | |
34 | + } | |
35 | + if(fork1 == 0){ | |
36 | + printf("dans le fils1, pid: %d\n", getpid()); | |
37 | + sleep(2); | |
38 | + } | |
39 | + | |
40 | + else { | |
41 | + if((fork2=fork())<0){ | |
42 | + perror("erreur exec du fork"); | |
43 | + exit(1); | |
44 | + } | |
45 | + if(fork2 == 0){ | |
46 | + printf("dans le fils2, pid: %d\n", getpid()); | |
47 | + sleep(4); | |
48 | + } | |
49 | + else{ | |
50 | + int r, pid; | |
51 | + printf("dans le père, val de ent : %d\n", getpid()); | |
52 | + if((pid= wait(&r)) <0){ | |
53 | + perror("erreur exec du wait"); | |
54 | + exit(1); | |
55 | + } | |
56 | + if((pid= wait(&r)) <0){ | |
57 | + perror("erreur exec du wait"); | |
58 | + exit(1); | |
59 | + } | |
60 | + } | |
61 | + } | |
62 | + return 0; | |
63 | +} | |
64 | +*/ | |
65 | +int main(){ | |
66 | + int fils, petitfils; | |
67 | + if((fils=fork())<0){ | |
68 | + perror("erreur exec du fork"); | |
69 | + exit(1); | |
70 | + } | |
71 | + if(fils == 0){ | |
72 | + sleep(2); | |
73 | + if((petitfils=fork())<0){ | |
74 | + perror("erreur exec du fork"); | |
75 | + exit(1); | |
76 | + } | |
77 | + if(petitfils == 0){ | |
78 | + sleep(4); | |
79 | + printf("dans le petitfils, pid: %d\n", getpid()); | |
80 | + for(;;){} | |
81 | + exit(8); | |
82 | + } | |
83 | + else{ | |
84 | + int r, pid; | |
85 | + printf("dans le fils, val de ent : %d\n", getpid()); | |
86 | + if((pid= wait(&r)) <0){ | |
87 | + perror("erreur exec du wait"); | |
88 | + exit(1); | |
89 | + } | |
90 | + if(WIFEXITED(r)){ | |
91 | + printf("Mort normale du fils: %d\n", WEXITSTATUS(r)); | |
92 | + }else if( WIFSIGNALED(r)){ | |
93 | + printf("Fils mort avec le code: %d\n", WTERMSIG(r)); | |
94 | + } | |
95 | + exit(2); | |
96 | + } | |
97 | + } else { | |
98 | + int r, pid; | |
99 | + printf("dans le père, val de ent : %d\n", getpid()); | |
100 | + if((pid= wait(&r)) <0){ | |
101 | + perror("erreur exec du wait"); | |
102 | + exit(1); | |
103 | + } | |
104 | + if(WIFEXITED(r)){ | |
105 | + printf("Mort normale du fils: %d\n", WEXITSTATUS(r)); | |
106 | + }else if( WIFSIGNALED(r)){ | |
107 | + printf("Fils mort avec le code: %d\n", WTERMSIG(r)); | |
108 | + } | |
109 | + } | |
110 | + return 0; | |
111 | +} | ... | ... |
No preview for this file type
... | ... | @@ -0,0 +1,17 @@ |
1 | +.PHONY: clean,real-clean | |
2 | +LD = gcc | |
3 | +CC = gcc | |
4 | +CFLAGS = -Wall -W -Werror | |
5 | +SRC=$(wildcard *.c) | |
6 | +OBJ=$(SRC:.c=.o) | |
7 | +prog : $(OBJ) | |
8 | + $(LD) -o $@ $^ | |
9 | + | |
10 | +%.o : %.c | |
11 | + $(CC) $(CFLAGS) -c $^ | |
12 | + | |
13 | +clean : | |
14 | + rm -f *.o | |
15 | + | |
16 | +real-clean : clean | |
17 | + rm -f prog | |
0 | 18 | \ No newline at end of file | ... | ... |
No preview for this file type
... | ... | @@ -0,0 +1,55 @@ |
1 | +#include <stdio.h> | |
2 | +#include <stdlib.h> | |
3 | +#include <sys/wait.h> | |
4 | +#include <unistd.h> | |
5 | + | |
6 | +int main(){ | |
7 | + int fork1, fork2, fork3; | |
8 | + if((fork1=fork())<0){ | |
9 | + perror("erreur exec du fork"); | |
10 | + exit(1); | |
11 | + } | |
12 | + if(fork1 == 0){ | |
13 | + if(execlp("who","who",NULL)==-1){ | |
14 | + perror("echec execlp"); | |
15 | + exit(1); | |
16 | + } | |
17 | + }else{ | |
18 | + int pid, r; | |
19 | + if((pid= wait(&r)) <0){ | |
20 | + perror("erreur exec du wait"); | |
21 | + exit(1); | |
22 | + } | |
23 | + if((fork2=fork())<0){ | |
24 | + perror("erreur exec du fork"); | |
25 | + exit(1); | |
26 | + } | |
27 | + if(fork2 == 0){ | |
28 | + if(execlp("ps","ps","-x",NULL)==-1){ | |
29 | + perror("echec execlp"); | |
30 | + exit(1); | |
31 | + } | |
32 | + }else{ | |
33 | + if((pid= wait(&r)) <0){ | |
34 | + perror("erreur exec du wait"); | |
35 | + exit(1); | |
36 | + } | |
37 | + if((fork3=fork())<0){ | |
38 | + perror("erreur exec du fork"); | |
39 | + exit(1); | |
40 | + } | |
41 | + if(fork3 == 0){ | |
42 | + if(execlp("ls","ls","-la",NULL)==-1){ | |
43 | + perror("echec execlp"); | |
44 | + exit(1); | |
45 | + } | |
46 | + }else{ | |
47 | + if((pid= wait(&r)) <0){ | |
48 | + perror("erreur exec du wait"); | |
49 | + exit(1); | |
50 | + } | |
51 | + } | |
52 | + } | |
53 | + } | |
54 | + return 0; | |
55 | +} | ... | ... |
... | ... | @@ -0,0 +1 @@ |
1 | +-la | ... | ... |
No preview for this file type
No preview for this file type
... | ... | @@ -0,0 +1,69 @@ |
1 | +#include <stdio.h> | |
2 | +#include <stdlib.h> | |
3 | +#include <sys/wait.h> | |
4 | +#include <unistd.h> | |
5 | + | |
6 | + | |
7 | +int main(int argc, char * argv[]){ | |
8 | + if(argc != 2){ | |
9 | + return 1; | |
10 | + } | |
11 | + int fils1, fils2; | |
12 | + int entierCourant; | |
13 | + int tube[2]; | |
14 | + if(pipe(tube)<0){ | |
15 | + | |
16 | + perror("erreur pipe"); | |
17 | + exit(1); | |
18 | + } | |
19 | + | |
20 | + if((fils1=fork())<0){ | |
21 | + perror("erreur exec du fork"); | |
22 | + exit(1); | |
23 | + } | |
24 | + if(fils1 == 0){ | |
25 | + close(tube[0]); //fils 1 ne lit pas | |
26 | + printf("entiers:\n"); | |
27 | + scanf("%d",&entierCourant); | |
28 | + while(entierCourant != 0){ | |
29 | + if(entierCourant%2==0){ | |
30 | + write(tube[1], &entierCourant, sizeof(int)); | |
31 | + } | |
32 | + scanf("%d",&entierCourant); | |
33 | + } | |
34 | + close(tube[1]); | |
35 | + } | |
36 | + | |
37 | + else { | |
38 | + if((fils2=fork())<0){ | |
39 | + perror("erreur exec du fork"); | |
40 | + exit(1); | |
41 | + } | |
42 | + if(fils2 == 0){ | |
43 | + close(tube[1]); //fils 2 n'écrit pas | |
44 | + while(read(tube[0],&entierCourant,sizeof(int))==sizeof(int)){ | |
45 | + if(entierCourant > atoi(argv[1])){ | |
46 | + printf("%d \n",entierCourant); | |
47 | + } | |
48 | + } | |
49 | + close(tube[0]); | |
50 | + } | |
51 | + else{ | |
52 | + int r, pid; | |
53 | + | |
54 | + close(tube[1]); | |
55 | + close(tube[0]); | |
56 | + | |
57 | + if((pid= wait(&r)) <0){ | |
58 | + perror("erreur exec du wait"); | |
59 | + exit(1); | |
60 | + } | |
61 | + if((pid= wait(&r)) <0){ | |
62 | + perror("erreur exec du wait"); | |
63 | + exit(1); | |
64 | + } | |
65 | + printf("\nDans le père, traitement fini\n"); | |
66 | + } | |
67 | + } | |
68 | + return 0; | |
69 | +} | ... | ... |
No preview for this file type
... | ... | @@ -0,0 +1,11 @@ |
1 | +#include <stdio.h> | |
2 | +#include <stdlib.h> | |
3 | +#include <unistd.h> | |
4 | + | |
5 | +int main(int argc, char * argv[]){ | |
6 | + int n1,n2; | |
7 | + n1=atoi(argv[1]); | |
8 | + n2=atoi(argv[2]); | |
9 | + printf("PID : %d, PPID : %d, %d * %d = %d\n", getpid(), getppid(), n1, n2, n1*n2); | |
10 | + return 0; | |
11 | +} | ... | ... |
... | ... | @@ -0,0 +1,56 @@ |
1 | +#include <stdio.h> | |
2 | +#include <stdlib.h> | |
3 | +#include <sys/wait.h> | |
4 | +#include <unistd.h> | |
5 | + | |
6 | +int main(int argc, char * argv[]){ | |
7 | + if(argc != 3){ | |
8 | + return 1; | |
9 | + } | |
10 | + int fork1, fork2; | |
11 | + if((fork1=fork())<0){ | |
12 | + perror("erreur exec du fork"); | |
13 | + exit(1); | |
14 | + } | |
15 | + if(fork1 == 0){ | |
16 | + if(execlp("./somme","somme",argv[1],argv[2],NULL)==-1){ | |
17 | + perror("echec execlp"); | |
18 | + exit(1); | |
19 | + } | |
20 | + } | |
21 | + | |
22 | + else { | |
23 | + if((fork2=fork())<0){ | |
24 | + perror("erreur exec du fork"); | |
25 | + exit(1); | |
26 | + } | |
27 | + if(fork2 == 0){ | |
28 | + sleep(2); | |
29 | + if(execlp("./produit","produit",argv[1],argv[2],NULL)==-1){ | |
30 | + perror("echec execlp"); | |
31 | + exit(1); | |
32 | + } | |
33 | + } | |
34 | + else{ | |
35 | + int r, pid; | |
36 | + if((pid= wait(&r)) <0){ | |
37 | + perror("erreur exec du wait"); | |
38 | + exit(1); | |
39 | + }else if(pid==fork1){ | |
40 | + printf("Somme effectuée\n"); | |
41 | + }else if(pid==fork2){ | |
42 | + printf("Produit effectuée\n"); | |
43 | + } | |
44 | + | |
45 | + if((pid= wait(&r)) <0){ | |
46 | + perror("erreur exec du wait"); | |
47 | + exit(1); | |
48 | + }else if(pid==fork1){ | |
49 | + printf("Somme effectuée\n"); | |
50 | + }else if(pid==fork2){ | |
51 | + printf("Produit effectuée\n"); | |
52 | + } | |
53 | + } | |
54 | + } | |
55 | + return 0; | |
56 | +} | ... | ... |
No preview for this file type
No preview for this file type
... | ... | @@ -0,0 +1,41 @@ |
1 | +#include <stdio.h> | |
2 | +#include <stdlib.h> | |
3 | +#include <sys/wait.h> | |
4 | +#include <unistd.h> | |
5 | +#include <sys/types.h> | |
6 | + | |
7 | +int main(){ | |
8 | + | |
9 | + int desc; | |
10 | + if((desc=open("dir2.txt",0666))<0){ | |
11 | + if((desc=creat("dir2.txt",0666))==-1){ | |
12 | + perror("creation fichier impossible"); | |
13 | + exit(1); | |
14 | + } | |
15 | + }else { | |
16 | + if(lseek(desc,0,SEEK_END)==-1){ | |
17 | + perror("creation fichier impossible"); | |
18 | + exit(1); | |
19 | + } | |
20 | + } | |
21 | + close(1); | |
22 | + dup(desc); | |
23 | + int fork1; | |
24 | + if((fork1=fork())<0){ | |
25 | + perror("erreur exec du fork"); | |
26 | + exit(1); | |
27 | + } | |
28 | + if(fork1 == 0){ | |
29 | + if(execlp("echo","echo","-la",NULL)==-1){ | |
30 | + perror("echec execlp"); | |
31 | + exit(1); | |
32 | + } | |
33 | + }else{ | |
34 | + int pid, r; | |
35 | + if((pid= wait(&r)) <0){ | |
36 | + perror("erreur exec du wait"); | |
37 | + exit(1); | |
38 | + } | |
39 | + } | |
40 | + return 0; | |
41 | +} | ... | ... |
No preview for this file type
... | ... | @@ -0,0 +1,11 @@ |
1 | +#include <stdio.h> | |
2 | +#include <stdlib.h> | |
3 | +#include <unistd.h> | |
4 | + | |
5 | +int main(int argc, char * argv[]){ | |
6 | + int n1,n2; | |
7 | + n1=atoi(argv[1]); | |
8 | + n2=atoi(argv[2]); | |
9 | + printf("PID : %d, PPID : %d, %d + %d = %d\n", getpid(), getppid(), n1, n2, n1+n2); | |
10 | + return 0; | |
11 | +} | ... | ... |
No preview for this file type
... | ... | @@ -0,0 +1,57 @@ |
1 | +#include <stdio.h> | |
2 | +#include <stdlib.h> | |
3 | +#include <sys/wait.h> | |
4 | +#include <unistd.h> | |
5 | + | |
6 | + | |
7 | +int main(int argc, char * argv[]){ | |
8 | + int fils1, fils2; | |
9 | + int tube[2]; | |
10 | + if(pipe(tube)<0){ | |
11 | + perror("erreur pipe"); | |
12 | + exit(1); | |
13 | + } | |
14 | + | |
15 | + if((fils1=fork())<0){ | |
16 | + perror("erreur exec du fork"); | |
17 | + exit(1); | |
18 | + } | |
19 | + if(fils1 == 0){ | |
20 | + close(tube[0]); //fils 1 ne lit pas | |
21 | + dup2(tube[1],1); | |
22 | + if(execlp("cat","cat","/etc/passwd",NULL)==-1){ | |
23 | + perror("echec execlp"); | |
24 | + exit(1); | |
25 | + } | |
26 | + } else { | |
27 | + if((fils2=fork())<0){ | |
28 | + perror("erreur exec du fork"); | |
29 | + exit(1); | |
30 | + } | |
31 | + if(fils2 == 0){ | |
32 | + close(tube[1]); //fils 2 n'écrit pas | |
33 | + dup2(tube[0],0); | |
34 | + if(execlp("wc","ws",NULL)==-1){ | |
35 | + perror("echec execlp"); | |
36 | + exit(1); | |
37 | + } | |
38 | + | |
39 | + } else{ | |
40 | + int r, pid; | |
41 | + | |
42 | + close(tube[1]); | |
43 | + close(tube[0]); | |
44 | + | |
45 | + if((pid= wait(&r)) <0){ | |
46 | + perror("erreur exec du wait"); | |
47 | + exit(1); | |
48 | + } | |
49 | + if((pid= wait(&r)) <0){ | |
50 | + perror("erreur exec du wait"); | |
51 | + exit(1); | |
52 | + } | |
53 | + printf("\nDans le père, traitement fini\n"); | |
54 | + } | |
55 | + } | |
56 | + return 0; | |
57 | +} | ... | ... |
... | ... | @@ -0,0 +1,37 @@ |
1 | +#include <stdio.h> | |
2 | +#include <stdlib.h> | |
3 | +#include <sys/wait.h> | |
4 | +#include <unistd.h> | |
5 | + | |
6 | +/*int main(){ | |
7 | + printf("lancement de la commande ls\n"); | |
8 | + if(execlp("ls","ls","-l",NULL)==-1){ | |
9 | + perror("echec execlp"); | |
10 | + exit(1); | |
11 | + } | |
12 | + return 0; | |
13 | +}*/ | |
14 | + | |
15 | +int main(){ | |
16 | + int fils; | |
17 | + char fich[256]; | |
18 | + printf("nom du fichier ?\n"); | |
19 | + scanf("%s",fich); | |
20 | + if((fils=fork())<0){ | |
21 | + perror("erreur exec du fork"); | |
22 | + exit(1); | |
23 | + } | |
24 | + if(fils == 0){ | |
25 | + if(execlp("wc","ws","-l",fich,NULL)==-1){ | |
26 | + perror("echec execlp"); | |
27 | + exit(1); | |
28 | + } | |
29 | + } else { | |
30 | + int r, pid; | |
31 | + if((pid= wait(&r)) <0){ | |
32 | + perror("erreur exec du wait"); | |
33 | + exit(1); | |
34 | + } | |
35 | + } | |
36 | + return 0; | |
37 | +} | ... | ... |
No preview for this file type
No preview for this file type
... | ... | @@ -0,0 +1,99 @@ |
1 | +#include <stdio.h> | |
2 | +#include <stdlib.h> | |
3 | +#include <sys/wait.h> | |
4 | +#include <unistd.h> | |
5 | +#include <pthread.h> | |
6 | +#include <signal.h> | |
7 | + | |
8 | + | |
9 | +#define ATTENTE 3 | |
10 | +int vitesse_train =0; | |
11 | +int conducteur_actif =1; | |
12 | +pthread_mutex_t mutex; | |
13 | + | |
14 | +void hand(int sig){ | |
15 | + printf("Veuiller bouger\n"); | |
16 | +} | |
17 | + | |
18 | +void * conducteur_action(void* arg){ | |
19 | + while(1){ | |
20 | + sleep(ATTENTE); | |
21 | + pthread_mutex_lock(&mutex); | |
22 | + if(conducteur_actif==0){ | |
23 | + kill(getppid(),SIGINT); | |
24 | + sleep(3); | |
25 | + if(conducteur_actif==0){ | |
26 | + kill(getppid(),SIGKILL); | |
27 | + } | |
28 | + } | |
29 | + conducteur_actif=0; | |
30 | + pthread_mutex_unlock(&mutex); | |
31 | + } | |
32 | +} | |
33 | + | |
34 | +int main(int argc, char * argv[]){ | |
35 | + int fils1; | |
36 | + int action_courante=0; | |
37 | + int tube[2]; | |
38 | + pthread_mutex_init(&mutex,NULL); | |
39 | + if(pipe(tube)<0){ | |
40 | + perror("erreur pipe"); | |
41 | + exit(1); | |
42 | + } | |
43 | + | |
44 | + if((fils1=fork())<0){ | |
45 | + perror("erreur exec du fork"); | |
46 | + exit(1); | |
47 | + } | |
48 | + if(fils1 == 0){ | |
49 | + close(tube[1]); //fils n'écrit pas | |
50 | + int variation_v; | |
51 | + | |
52 | + pthread_t tid1; | |
53 | + pthread_attr_t attr; | |
54 | + pthread_attr_init(&attr); | |
55 | + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); | |
56 | + pthread_create(&tid1, &attr, conducteur_action, NULL); | |
57 | + while(read(tube[0],&variation_v,sizeof(int))==sizeof(int)){ | |
58 | + | |
59 | + pthread_mutex_lock(&mutex); | |
60 | + conducteur_actif = 1; | |
61 | + pthread_mutex_unlock(&mutex); | |
62 | + | |
63 | + vitesse_train = vitesse_train + variation_v; | |
64 | + printf("Vitesse actuelle: %d\n",vitesse_train); | |
65 | + } | |
66 | + close(tube[0]); | |
67 | + } else { | |
68 | + | |
69 | + struct sigaction action; | |
70 | + sigset_t ens; | |
71 | + sigemptyset(&ens); | |
72 | + action.sa_handler = hand; | |
73 | + action.sa_mask =ens; | |
74 | + action.sa_flags = SA_RESTART; | |
75 | + sigaction(SIGINT,&action,NULL); | |
76 | + | |
77 | + close(tube[0]); //pere 1 ne lit pas | |
78 | + printf("Action:\n"); | |
79 | + | |
80 | + scanf("%d",&action_courante); | |
81 | + while(action_courante != -2){ | |
82 | + if((action_courante<2) && (action_courante>-2)){ | |
83 | + write(tube[1], &action_courante, sizeof(int)); | |
84 | + } | |
85 | + scanf("%d",&action_courante); | |
86 | + } | |
87 | + vitesse_train=0; | |
88 | + close(tube[1]); | |
89 | + | |
90 | + int r, pid; | |
91 | + if((pid= wait(&r)) <0){ | |
92 | + perror("erreur exec du wait"); | |
93 | + exit(1); | |
94 | + } | |
95 | + printf("\nDans le père, train arrêté, vitesse: %d\n",vitesse_train); | |
96 | + | |
97 | + } | |
98 | + return 0; | |
99 | +} | ... | ... |
... | ... | @@ -0,0 +1,84 @@ |
1 | +#include <stdio.h> | |
2 | +#include <pthread.h> | |
3 | +#include <stdlib.h> | |
4 | +#define MAX 10 | |
5 | + | |
6 | +int tab[MAX]; | |
7 | +int nb=0; | |
8 | +pthread_mutex_t mutex; | |
9 | + | |
10 | +void lire() { | |
11 | + int i; | |
12 | + printf("saisir %d entiers\n", MAX); | |
13 | + for (i=0; i<MAX ; i++) scanf("%d", &tab[i]); | |
14 | +} | |
15 | +void affiche(){ | |
16 | + int i; | |
17 | + printf("entiers saisis :"); | |
18 | + for (i=0; i<MAX ; i++) printf("%d ",tab [i]); | |
19 | + printf("\n"); | |
20 | +} | |
21 | +void * moyenne(void* arg){ | |
22 | + int i, moy=tab[0]; | |
23 | + for (i=1; i<MAX; i++) moy = moy + tab[i]; | |
24 | + printf("moyenne des entiers = %f\n", (float)moy/MAX); | |
25 | +} | |
26 | +void * supSeuil(void* arg){ | |
27 | + int i; | |
28 | + int seuil =*(int*)arg; | |
29 | + pthread_mutex_lock(&mutex); | |
30 | + for (i=0; i<MAX; i++) if (tab[i]>seuil) nb++; | |
31 | + printf("fin de fonction supSeuil : nb = %d\n", nb); | |
32 | + pthread_mutex_unlock(&mutex); | |
33 | +} | |
34 | +void * infSeuil(void* arg){ | |
35 | + int i; | |
36 | + int seuil =*(int*)arg; | |
37 | + pthread_mutex_lock(&mutex); | |
38 | + for (i=0; i<MAX; i++) if (tab[i]<seuil) nb++; | |
39 | + printf("fin de fonction infSeuil : nb = %d\n", nb); | |
40 | + pthread_mutex_unlock(&mutex); | |
41 | +} | |
42 | + | |
43 | +void minMax(int *min, int *max) { | |
44 | + int i; | |
45 | + *min=tab[0]; | |
46 | + *max=tab[0]; | |
47 | + for (i=1; i<MAX; i++) { | |
48 | + if (tab[i]<*min) *min=tab[i]; | |
49 | + else if (tab[i]>*max) *max=tab[i]; | |
50 | + } | |
51 | +} | |
52 | + | |
53 | +int main(){ | |
54 | + int seuilInf, seuilSup, min, max; | |
55 | + pthread_t tid1, tid2, tid3; | |
56 | + pthread_attr_t attr; | |
57 | + pthread_attr_init(&attr); | |
58 | + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); | |
59 | + | |
60 | + | |
61 | + pthread_mutex_init(&mutex,NULL); | |
62 | + | |
63 | + | |
64 | + lire(); | |
65 | + affiche(); | |
66 | + | |
67 | + pthread_create(&tid1, &attr, moyenne, NULL); | |
68 | + | |
69 | + | |
70 | + | |
71 | + printf("saisir les seuils sup et inf : "); | |
72 | + scanf("%d%d", &seuilSup, &seuilInf); | |
73 | + int * p_seuilInf = (int *) malloc(sizeof(int)); | |
74 | + *p_seuilInf = seuilInf; | |
75 | + int * p_seuilSupp = (int *) malloc(sizeof(int)); | |
76 | + *p_seuilSupp = seuilSup; | |
77 | + pthread_create(&tid2, &attr, supSeuil,(void *) p_seuilSupp); | |
78 | + pthread_create(&tid3, &attr, infSeuil,(void *) p_seuilInf); | |
79 | + | |
80 | + minMax(&min, &max); | |
81 | + printf("min = %d, max = %d\n", min, max); | |
82 | + pthread_exit(NULL); | |
83 | + return(0); | |
84 | +} | ... | ... |
... | ... | @@ -0,0 +1,96 @@ |
1 | +#include <stdio.h> | |
2 | +#include <pthread.h> | |
3 | +#include <stdlib.h> | |
4 | +#define MAX 10 | |
5 | + | |
6 | +int tab[MAX]; | |
7 | +int nb=0; | |
8 | +pthread_mutex_t mutex; | |
9 | +struct minMax { | |
10 | + int *min; | |
11 | + int *max; | |
12 | + }; | |
13 | + | |
14 | +void lire() { | |
15 | + int i; | |
16 | + printf("saisir %d entiers\n", MAX); | |
17 | + for (i=0; i<MAX ; i++) scanf("%d", &tab[i]); | |
18 | +} | |
19 | +void affiche(){ | |
20 | + int i; | |
21 | + printf("entiers saisis :"); | |
22 | + for (i=0; i<MAX ; i++) printf("%d ",tab [i]); | |
23 | + printf("\n"); | |
24 | +} | |
25 | +void * moyenne(void* arg){ | |
26 | + int i, moy=tab[0]; | |
27 | + for (i=1; i<MAX; i++) moy = moy + tab[i]; | |
28 | + printf("moyenne des entiers = %f\n", (float)moy/MAX); | |
29 | +} | |
30 | +void * supSeuil(void* arg){ | |
31 | + int i; | |
32 | + int seuil =*(int*)arg; | |
33 | + for (i=0; i<MAX; i++){ | |
34 | + if (tab[i]>seuil){ | |
35 | + pthread_mutex_lock(&mutex); | |
36 | + nb++; | |
37 | + pthread_mutex_unlock(&mutex); | |
38 | + } | |
39 | + } | |
40 | + | |
41 | +} | |
42 | +void * infSeuil(void* arg){ | |
43 | + int i; | |
44 | + int seuil =*(int*)arg; | |
45 | + | |
46 | + for (i=0; i<MAX; i++){ | |
47 | + if (tab[i]<seuil){ | |
48 | + pthread_mutex_lock(&mutex); | |
49 | + nb++; | |
50 | + pthread_mutex_unlock(&mutex); | |
51 | + } | |
52 | + } | |
53 | +} | |
54 | + | |
55 | +void * minMax(void * arg) { | |
56 | + int i; | |
57 | + struct minMax str_minMax = *(struct minMax *)arg; | |
58 | + *str_minMax.min=tab[0]; | |
59 | + *str_minMax.max=tab[0]; | |
60 | + for (i=1; i<MAX; i++) { | |
61 | + if (tab[i]<*str_minMax.min) *str_minMax.min=tab[i]; | |
62 | + else if (tab[i]>*str_minMax.max) *str_minMax.max=tab[i]; | |
63 | + } | |
64 | +} | |
65 | + | |
66 | +int main(){ | |
67 | + int seuilInf, seuilSup, min, max; | |
68 | + pthread_t tid1, tid2, tid3, tid4; | |
69 | + | |
70 | + pthread_mutex_init(&mutex,NULL); | |
71 | + | |
72 | + lire(); | |
73 | + affiche(); | |
74 | + | |
75 | + pthread_create(&tid1, NULL, moyenne, NULL); | |
76 | + | |
77 | + printf("saisir les seuils sup et inf : "); | |
78 | + scanf("%d%d", &seuilSup, &seuilInf); | |
79 | + | |
80 | + pthread_join(tid1,NULL); | |
81 | + pthread_create(&tid2, NULL, supSeuil,(void *) &seuilSup); | |
82 | + pthread_create(&tid3, NULL, infSeuil,(void *) &seuilInf); | |
83 | + pthread_join(tid2,NULL); | |
84 | + pthread_join(tid3,NULL); | |
85 | + | |
86 | + struct minMax str_minMax; | |
87 | + | |
88 | + str_minMax.min = &min; | |
89 | + str_minMax.max = &max; | |
90 | + pthread_create(&tid4, NULL, minMax,(void *) &str_minMax); | |
91 | + | |
92 | + pthread_join(tid4,NULL); | |
93 | + printf("min = %d, max = %d\nnb=%d", *str_minMax.min, *str_minMax.max, nb); | |
94 | + pthread_exit(NULL); | |
95 | + return(0); | |
96 | +} | ... | ... |
No preview for this file type
No preview for this file type
No preview for this file type
... | ... | @@ -0,0 +1,41 @@ |
1 | +#include "hachage_simple.h" | |
2 | +#include "liste_double.h" | |
3 | +#include <stdio.h> | |
4 | +#include <stdlib.h> | |
5 | +#include <assert.h> | |
6 | +#include <stdbool.h> | |
7 | + | |
8 | +void init_table (struct table* T, fonction_hachage* H){ | |
9 | + /* int i = 0; | |
10 | + for(i=0; i<N;i++){ | |
11 | + init_liste_double (&T->tab[i].L); | |
12 | + } | |
13 | + T->hash = H;*/ | |
14 | +} | |
15 | + | |
16 | +void clear_table (struct table* T){ | |
17 | + /*int i = 0; | |
18 | + for(i=0; i<N;i++){ | |
19 | + clear_liste_double (&T->tab[i].L); | |
20 | + }*/ | |
21 | +} | |
22 | + | |
23 | +void enregistrer_table (struct table* T, double d){ | |
24 | + /* int i; | |
25 | + i=T->hash(d); | |
26 | + ajouter_en_tete_liste_double(&T->tab[i].L,d);*/ | |
27 | +} | |
28 | + | |
29 | +bool rechercher_table (struct table* T, double d){ | |
30 | + /*int i; | |
31 | + i= T->hash(d); | |
32 | + recherche_liste_double(&T->tab[i].L,d);*/ | |
33 | +} | |
34 | + | |
35 | +void imprimer_table (struct table* T){ | |
36 | + /*int i = 0; | |
37 | + for(i=0; i<N;i++){ | |
38 | + imprimer_liste_double (&T->tab[i].L); | |
39 | + } */ | |
40 | +} | |
41 | + | ... | ... |
... | ... | @@ -0,0 +1,22 @@ |
1 | +/* hachage_simple.h */ | |
2 | +#include "liste_double.h" | |
3 | +#include <stdbool.h> | |
4 | + | |
5 | +struct alveole { | |
6 | + struct liste_double L; | |
7 | +}; | |
8 | + | |
9 | +typedef int fonction_hachage (double); | |
10 | + | |
11 | +#define N 10 | |
12 | +struct table { | |
13 | + struct alveole tab [N]; | |
14 | + fonction_hachage* hash; | |
15 | +}; | |
16 | + | |
17 | +extern void init_table (struct table*, fonction_hachage*); | |
18 | +extern void clear_table (struct table*); | |
19 | +extern void enregistrer_table (struct table*, double); | |
20 | +extern bool rechercher_table (struct table*, double); | |
21 | +extern void imprimer_table (struct table*); | |
22 | + | ... | ... |
No preview for this file type
No preview for this file type
SD/TP6_Hachage/liste_double.c renamed to SD/TP6_Hachage/V1/liste_double.c
SD/TP6_Hachage/liste_double.h renamed to SD/TP6_Hachage/V1/liste_double.h
1 | 1 | #if ! defined (LISTE_DOUBLE_H) |
2 | 2 | #define LISTE_DOUBLE_H 1 |
3 | 3 | |
4 | +#include <stdbool.h> | |
5 | + | |
4 | 6 | /********************************************************************** |
5 | 7 | * IMPLANTATION |
6 | 8 | * |
... | ... | @@ -38,6 +40,8 @@ struct liste_double |
38 | 40 | |
39 | 41 | extern void init_liste_double (struct liste_double*); |
40 | 42 | |
43 | + | |
44 | +extern bool recherche_liste_double (struct liste_double* , double ); | |
41 | 45 | /* |
42 | 46 | * Destructeur |
43 | 47 | */ | ... | ... |
No preview for this file type
No preview for this file type
... | ... | @@ -0,0 +1,24 @@ |
1 | +/* main.c */ | |
2 | +#include <stdio.h> | |
3 | +#include "hachage_simple.h" | |
4 | + | |
5 | +int hachage_basique (double d) | |
6 | +{ | |
7 | + return (int)d % N; | |
8 | +} | |
9 | + | |
10 | +int main () | |
11 | +{ | |
12 | + struct table T; | |
13 | + double x; | |
14 | + init_table (&T, &hachage_basique); | |
15 | + | |
16 | + scanf ("%lf", &x); | |
17 | + while (x != -1){ | |
18 | + enregistrer_table (&T, x); | |
19 | + imprimer_table (&T); | |
20 | + scanf ("%lf", &x); | |
21 | + } | |
22 | + clear_table (&T); | |
23 | + return 0; | |
24 | +} | ... | ... |
No preview for this file type
No preview for this file type
No preview for this file type
... | ... | @@ -0,0 +1,136 @@ |
1 | +#include <stdio.h> | |
2 | +#include <stdlib.h> | |
3 | +#include <assert.h> | |
4 | +#include "abr.h" | |
5 | + | |
6 | + | |
7 | +static struct abr* new_feuille (int x){ | |
8 | + | |
9 | + struct abr* F; | |
10 | + | |
11 | + F = (struct abr*)malloc (sizeof (struct abr)); | |
12 | + F->gauche = NIL; | |
13 | + F->valeur = x; | |
14 | + F->droit = NIL; | |
15 | + return F; | |
16 | +} | |
17 | + | |
18 | + | |
19 | +struct abr* ajout_abr_rec (int x, struct abr* A){ | |
20 | + if (A == NIL) | |
21 | + return new_feuille (x); | |
22 | + else if (x < A->valeur) | |
23 | + A->gauche = ajout_abr_rec (x, A->gauche); | |
24 | + else | |
25 | + A->droit = ajout_abr_rec (x, A->droit); | |
26 | + return A; | |
27 | +} | |
28 | + | |
29 | +int max( int a, int b){ | |
30 | + | |
31 | + if(a>b){ | |
32 | + return a; | |
33 | + } | |
34 | + return b; | |
35 | +} | |
36 | + | |
37 | +int hauteur_abr( struct abr* A){ | |
38 | + | |
39 | + if(A == NIL){ | |
40 | + return -1; | |
41 | + } | |
42 | + return max(hauteur_abr(A->gauche),hauteur_abr(A->droit))+1; | |
43 | + | |
44 | +} | |
45 | + | |
46 | + | |
47 | +bool recherche_abr_rec (int x, struct abr* A){ | |
48 | + bool b = false; | |
49 | + if (A == NIL){ | |
50 | + return b; | |
51 | + } else if ( x == A->valeur ){ | |
52 | + return true; | |
53 | + } else if (x < A->valeur){ | |
54 | + b=recherche_abr_rec (x, A->gauche); | |
55 | + } else{ | |
56 | + b=recherche_abr_rec (x, A->droit); | |
57 | + } | |
58 | + return b; | |
59 | +} | |
60 | + | |
61 | + | |
62 | +static void affiche_abr (struct abr* A){ | |
63 | + if( A!= NIL){ | |
64 | + affiche_abr(A->gauche); | |
65 | + printf("- %d ", A-> valeur); | |
66 | + affiche_abr(A->droit); | |
67 | + } | |
68 | + | |
69 | +} | |
70 | + | |
71 | + | |
72 | +void imprime_abr (struct abr* A){ | |
73 | + printf("Valeurs de l'arbre: \n"); | |
74 | + affiche_abr(A); | |
75 | + printf(" -\n"); | |
76 | +} | |
77 | + | |
78 | + | |
79 | +static void affiche_abrV2 (struct abr* A, int profondeur){ | |
80 | + if( A!= NIL){ | |
81 | + int i = 0; | |
82 | + for(i=0; i<=profondeur; i++){ | |
83 | + printf("\t"); | |
84 | + } | |
85 | + | |
86 | + printf("%d\n ", A-> valeur); | |
87 | + affiche_abrV2(A->droit, profondeur+1); | |
88 | + affiche_abrV2(A->gauche, profondeur+1); | |
89 | + } | |
90 | + | |
91 | +} | |
92 | + | |
93 | +void imprime_abrV2 (struct abr* A){ | |
94 | + printf("Valeurs de l'arbre: \n"); | |
95 | + affiche_abrV2(A, 0); | |
96 | + printf("\n"); | |
97 | +} | |
98 | + | |
99 | +void clear_abr (struct abr* A) | |
100 | +{ | |
101 | + if (A != NIL) | |
102 | + { clear_abr (A->gauche); | |
103 | + clear_abr (A->droit); | |
104 | + free (A); | |
105 | + } | |
106 | +} | |
107 | + | |
108 | +void imprimeArbreDot( struct abr* A, FILE* f){ | |
109 | + if(A->gauche != NIL){ | |
110 | + fprintf(f,"%d -> %d [label=\"gauche\"];\n", A->valeur, A->gauche->valeur); | |
111 | + imprimeArbreDot(A->gauche,f); | |
112 | + } | |
113 | + if(A->droit != NIL){ | |
114 | + fprintf(f,"%d -> %d [label=\"droit\"];\n", A->valeur, A->droit->valeur); | |
115 | + imprimeArbreDot(A->droit,f); | |
116 | + } | |
117 | +} | |
118 | + | |
119 | + | |
120 | +void imprimeDot( struct abr* A){ | |
121 | + FILE* f; | |
122 | + f = fopen("ABR.dot", "w"); | |
123 | + fprintf(f,"digraph G {\n"); | |
124 | + if(A==NIL){ | |
125 | + fprintf(f,"NIL\n"); | |
126 | + }else if (A->gauche == NIL && A->droit == NIL){ | |
127 | + fprintf(f,"%d\n", A->valeur); | |
128 | + }else { | |
129 | + imprimeArbreDot(A,f); | |
130 | + } | |
131 | + fprintf(f,"}\n"); | |
132 | + fclose(f); | |
133 | + system("dot -Tpdf ABR.dot -GRANKDIR=LR -o ABR.pdf"); | |
134 | + system("evince ABR.pdf &"); | |
135 | +} | |
136 | + | ... | ... |
... | ... | @@ -0,0 +1,29 @@ |
1 | +#if ! defined (ABR_H) | |
2 | +#define ABR_H 1 | |
3 | + | |
4 | + | |
5 | +#include <stdbool.h> | |
6 | + | |
7 | +struct abr { | |
8 | + struct abr* gauche; | |
9 | + int valeur; | |
10 | + struct abr* droit; | |
11 | +}; | |
12 | + | |
13 | +#define NIL (struct abr*)0 | |
14 | + | |
15 | + | |
16 | +extern struct abr* ajout_abr_rec (int, struct abr*); | |
17 | + | |
18 | +/* Calcule la hauteur d'un abr*/ | |
19 | +extern int hauteur_abr( struct abr*); | |
20 | + | |
21 | +extern bool recherche_abr_rec (int, struct abr*); | |
22 | + | |
23 | +extern void imprime_abr (struct abr*); | |
24 | + | |
25 | +extern void imprime_abrV2 (struct abr*); | |
26 | + | |
27 | +extern void clear_abr (struct abr*); | |
28 | + | |
29 | +#endif | ... | ... |
No preview for this file type
SD/TP6_Hachage/hachage_simple.c
1 | -#include "hachage_simple.h" | |
2 | -#include "liste_double.h" | |
3 | 1 | #include <stdio.h> |
4 | 2 | #include <stdlib.h> |
5 | 3 | #include <assert.h> |
6 | 4 | #include <stdbool.h> |
7 | - | |
5 | +#include "hachage_simple.h" | |
6 | +#include "abr.h" | |
8 | 7 | |
9 | 8 | |
10 | 9 | void init_table (struct table* T, fonction_hachage* H){ |
11 | - int i = 0; | |
10 | + int i = 0; | |
12 | 11 | for(i=0; i<N;i++){ |
13 | - init_liste_double (&T->tab[N].L); | |
12 | + (T->tab[i].L) = NIL; | |
14 | 13 | } |
14 | + T->hash = H; | |
15 | 15 | } |
16 | 16 | |
17 | 17 | void clear_table (struct table* T){ |
18 | - int i = 0; | |
18 | + int i = 0; | |
19 | 19 | for(i=0; i<N;i++){ |
20 | - clear_liste_double (&T->tab[N].L); | |
20 | + clear_abr(T->tab[i].L); | |
21 | 21 | } |
22 | - | |
23 | - | |
24 | 22 | } |
25 | 23 | |
26 | 24 | void enregistrer_table (struct table* T, double d){ |
27 | - | |
25 | + int i; | |
26 | + i=T->hash(d); | |
27 | + T->tab[i].L = ajout_abr_rec(i, T->tab[i].L); | |
28 | 28 | } |
29 | 29 | |
30 | 30 | bool rechercher_table (struct table* T, double d){ |
31 | - return true; | |
31 | + int i; | |
32 | + i= T->hash(d); | |
33 | + recherche_abr_rec(i,T->tab[i].L); | |
32 | 34 | } |
33 | 35 | |
34 | 36 | void imprimer_table (struct table* T){ |
35 | - | |
37 | + int i = 0; | |
38 | + for(i=0; i<N;i++){ | |
39 | + imprime_abr(T->tab[i].L); | |
40 | + } | |
36 | 41 | } |
37 | 42 | ... | ... |
SD/TP6_Hachage/hachage_simple.h
SD/TP6_Hachage/hachage_simple.h.gch
No preview for this file type
SD/TP6_Hachage/hachage_simple.o
No preview for this file type
SD/TP6_Hachage/liste_double.o deleted
No preview for this file type
SD/TP6_Hachage/main.c
... | ... | @@ -9,16 +9,16 @@ int hachage_basique (double d) |
9 | 9 | |
10 | 10 | int main () |
11 | 11 | { |
12 | - | |
13 | 12 | struct table T; |
14 | 13 | double x; |
15 | 14 | init_table (&T, &hachage_basique); |
16 | - /*scanf ("%lf", &x); | |
15 | + | |
16 | + scanf ("%lf", &x); | |
17 | 17 | while (x != -1){ |
18 | 18 | enregistrer_table (&T, x); |
19 | - imprimer_table (&T); | |
20 | - scanf ("%lf", &x); | |
19 | + imprimer_table (&T); | |
20 | + scanf ("%lf", &x); | |
21 | 21 | } |
22 | - clear_table (&T);*/ | |
22 | + clear_table (&T); | |
23 | 23 | return 0; |
24 | 24 | } | ... | ... |
SD/TP6_Hachage/main.o
No preview for this file type
SD/TP6_Hachage/prog
No preview for this file type