*
This commit is contained in:
62
SCR3.1/devoir27/TaF27
Normal file
62
SCR3.1/devoir27/TaF27
Normal file
@@ -0,0 +1,62 @@
|
||||
Créez un répertoire cntr_r3.05, et pour chaque exercice un sous-répertoire (Ex1,
|
||||
Ex2, etc.) pour y placer vos réponses.
|
||||
|
||||
Les exercices sont indépendants. Vous rendrez une archive compressée à la fin de
|
||||
l'épreuve sur le site :
|
||||
|
||||
http://www.iut-fbleau.fr/site/site/DEVOIR
|
||||
|
||||
# EX1
|
||||
1) Ecrivez un programme gdf1.c qui implante le diagramme de processus
|
||||
conforme à la sortie suivante de la commande ps :
|
||||
|
||||
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
|
||||
0 S 1000 30539 28785 0 80 0 - 1039 hrtime pts/0 00:00:00 a.out
|
||||
1 S 1000 30542 30539 0 80 0 - 1039 hrtime pts/0 00:00:00 \_ a.out
|
||||
1 S 1000 30544 30542 0 80 0 - 1039 hrtime pts/0 00:00:00 \_ a.out
|
||||
1 S 1000 30546 30544 0 80 0 - 1039 hrtime pts/0 00:00:00 | \_ a.out
|
||||
1 S 1000 30545 30542 0 80 0 - 1039 hrtime pts/0 00:00:00 \_ a.out
|
||||
|
||||
2) Expliquez (dans gdf2.txt) ce que provoque kill -TERM 30544.
|
||||
|
||||
3) Modifiez votre programme (gdf3.c) pour que le processus correspondant à 30546 utilise un
|
||||
tube pour communiquer son PID au le processus correspondant à 30539.
|
||||
|
||||
# EX2
|
||||
Le sous-repertoire 'src/' de votre répértoire de travail contient le source du
|
||||
programme 'cachetst.c'. Ce programme peut être utilisé pour tester certaines
|
||||
propriétés de la mémoire cache. Il crée une liste de structures et effectue
|
||||
plusieurs passages par cette liste en faisant un test sur chaque élément.
|
||||
|
||||
La longueur de la liste et le nombre de passages (itérations) doivent être
|
||||
donnsés sur la ligne de commande. Compilez et lancez ce programme avec par
|
||||
exemple les valeurs '1000' '1000' sur la ligne de commande pour voir ce qu'il
|
||||
calcule. Augmentez ces valeurs en prenant '10000' '100000' pour voir que le
|
||||
temps de calcul est assez long.
|
||||
|
||||
Votre travail consiste à rajouter une partie de code à ce programme (appelez le
|
||||
'vcachetst.c') pour qu'il affiche le nombre de tests actuellement
|
||||
passés (la valeur de la variable 'z') après un ctrl + z depuis le terminal.
|
||||
|
||||
Mettez le source du programme 'vcachetst.c' dans le sous-repertoire 'src/'
|
||||
|
||||
# EX3
|
||||
Écrire un programme où un processus père crée deux fils ; chaque fils attend un
|
||||
couple d'entiers (x, y) du père et le renvoie dans l'ordre croissant. (combien
|
||||
faut-il de tubes ?)
|
||||
|
||||
1. Le père prend quatre entiers a, b, c, d sur la ligne de commande,
|
||||
|
||||
2. il ordonne les couples (a1 , b1) et (c1, d1) grâce à ses deux fils (le père
|
||||
donne le travail à **ses deux fils** avant de récupèrer le résultat)
|
||||
|
||||
3. Puis il ordonne les couples (a1, c1) et (b1, d1) grâce à ses deux fils, et
|
||||
il obtient les couples (a2, c2) et (b2, d2).
|
||||
|
||||
4. Finalement, il ordonne le couple (c2 , b2) à l'aide d'un fils Il obtient le.
|
||||
couple (c3, b3) .
|
||||
|
||||
5. Il affiche (a2 , c3 , b3 , d2).
|
||||
|
||||
On vieillera à fermer les descripteurs de fichiers inutilisés, à ne pas
|
||||
dupliquer du code inutiliement.
|
17
SCR3.1/devoir27/ex1/gdf1.c
Normal file
17
SCR3.1/devoir27/ex1/gdf1.c
Normal file
@@ -0,0 +1,17 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
int main(void){
|
||||
pid_t x, x1, x2, x3;
|
||||
x1 = fork();
|
||||
if (x1 == 0){
|
||||
x2=fork();
|
||||
if (x2 == 0)
|
||||
x = fork();
|
||||
x3=fork();
|
||||
}
|
||||
}
|
||||
|
||||
|
1
SCR3.1/devoir27/ex1/gdf2.txt
Normal file
1
SCR3.1/devoir27/ex1/gdf2.txt
Normal file
@@ -0,0 +1 @@
|
||||
La commande "kill -TERM 30544" provoque l'arrêt forcé du processus 30544, à cause du signal de terminaison envoyé
|
38
SCR3.1/devoir27/ex1/gdf3.c
Normal file
38
SCR3.1/devoir27/ex1/gdf3.c
Normal file
@@ -0,0 +1,38 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <assert.h>
|
||||
#include <fcntl.h>
|
||||
#define BUF_SIZE 128
|
||||
|
||||
int main(void){
|
||||
pid_t x, x1, x2, x3;
|
||||
int p[2];
|
||||
char buf;
|
||||
assert(pipe(p) != -1);
|
||||
x1 = fork();
|
||||
if (x1 == 0){
|
||||
x2=fork();
|
||||
if (x2 == 0){
|
||||
x = fork();
|
||||
if (x == 0){
|
||||
close(p[0]);
|
||||
dup2(p[1], 1);
|
||||
close(p[1]);
|
||||
printf("%d", getpid());
|
||||
}
|
||||
x3=fork();
|
||||
}
|
||||
sleep(1);
|
||||
}
|
||||
if (x1 > 0){
|
||||
close(p[1]);
|
||||
while(read(p[0], &buf, sizeof(char)) > 0)
|
||||
write(1, &buf, sizeof(char));
|
||||
}
|
||||
close(p[0]);
|
||||
printf("\n");
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
|
19
SCR3.1/devoir27/ex2/helpers.c
Normal file
19
SCR3.1/devoir27/ex2/helpers.c
Normal file
@@ -0,0 +1,19 @@
|
||||
#include "helpers.h"
|
||||
#include <signal.h>
|
||||
#include <time.h>
|
||||
|
||||
int set_signal_handler(int signo, void (*handler)(int)) {
|
||||
struct sigaction sa;
|
||||
sa.sa_handler = handler; // call `handler` on signal
|
||||
sigemptyset(&sa.sa_mask); // don't block other signals in handler
|
||||
sa.sa_flags = SA_RESTART; // restart system calls
|
||||
return sigaction(signo, &sa, NULL);
|
||||
}
|
||||
|
||||
double tstamp(void) {
|
||||
struct timespec tv;
|
||||
clock_gettime(CLOCK_REALTIME, &tv);
|
||||
return tv.tv_sec + tv.tv_nsec * 1.0e-9;
|
||||
}
|
||||
|
||||
|
7
SCR3.1/devoir27/ex2/helpers.h
Normal file
7
SCR3.1/devoir27/ex2/helpers.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#ifndef _HELPERS_H
|
||||
#define _HELPERS_H
|
||||
|
||||
int set_signal_handler(int signo, void (*handler)(int));
|
||||
double tstamp(void);
|
||||
|
||||
#endif
|
95
SCR3.1/devoir27/ex2/vcachetest.c
Normal file
95
SCR3.1/devoir27/ex2/vcachetest.c
Normal file
@@ -0,0 +1,95 @@
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <x86intrin.h>
|
||||
#include <signal.h>
|
||||
#include "helpers.c"
|
||||
#include "helpers.h"
|
||||
|
||||
|
||||
#define PLSIZE 63
|
||||
|
||||
|
||||
|
||||
struct lst {
|
||||
struct lst * next;
|
||||
long int payload[PLSIZE];
|
||||
} l;
|
||||
|
||||
|
||||
struct lst * mkws(int size)
|
||||
{
|
||||
int i,j;
|
||||
struct lst * ws0,*ws;
|
||||
|
||||
if (size == 0)
|
||||
return NULL;
|
||||
|
||||
srand(time(0));
|
||||
ws = calloc(1,sizeof(struct lst));
|
||||
ws->next = NULL;
|
||||
|
||||
for (i=0;i<PLSIZE;i++)
|
||||
ws->payload[i]=(rand())%2;
|
||||
|
||||
for (j=1;j<size;j++) {
|
||||
ws0=ws;
|
||||
ws = calloc(1,sizeof(struct lst));
|
||||
if (ws==NULL) {
|
||||
fprintf(stderr,"Can't add element...\n");
|
||||
exit(1);
|
||||
}
|
||||
for (i=0;i<PLSIZE;i++)
|
||||
ws->payload[i]=(rand())%2;
|
||||
ws->next = ws0;
|
||||
}
|
||||
return ws;
|
||||
}
|
||||
|
||||
long int z = 0;
|
||||
|
||||
void kill_prg(int){
|
||||
printf("Nombre de tests effectués: %ld\n", z);
|
||||
}
|
||||
|
||||
int main(int argc, char ** argv)
|
||||
{
|
||||
struct lst * ws, * ws0;
|
||||
|
||||
long long int start, end, dif;
|
||||
|
||||
long int i, x, size, it;
|
||||
|
||||
if (argc != 3) {
|
||||
printf("Usage: %s <list size> <iterations>\n",argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
set_signal_handler(SIGTSTP, &kill_prg);
|
||||
size=strtol(argv[1],NULL,0);
|
||||
|
||||
ws = mkws(size);
|
||||
if (ws == NULL) {
|
||||
printf("NIL list created...\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
it = strtol(argv[2],NULL,0);
|
||||
|
||||
start = __rdtsc();
|
||||
for (i = 0; i < it; i++) {
|
||||
ws0 = ws;
|
||||
while (ws0 != NULL) {
|
||||
x = ws0->payload[3];
|
||||
x++;
|
||||
z++;
|
||||
ws0 = ws0->next;
|
||||
}
|
||||
}
|
||||
end = __rdtsc();
|
||||
|
||||
dif = end - start;
|
||||
printf("Cycles/element = %lf\n", ((double) dif)/((double) size*it));
|
||||
exit(0);
|
||||
}
|
86
SCR3.1/devoir27/ex3/ex3.c
Normal file
86
SCR3.1/devoir27/ex3/ex3.c
Normal file
@@ -0,0 +1,86 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
int ordre(int a, int b){
|
||||
if (a < b)
|
||||
return 1;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv){
|
||||
if (argc != 5){
|
||||
printf("Usage : %s <entier1> <entier2> <entier3> <entier4>\n", argv[0]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
pid_t x1, x2;
|
||||
int p1[2], p2[2],tmp, a, b, c, d;
|
||||
int x3 = strtod(argv[1], NULL), x4 = strtod(argv[2], NULL);
|
||||
pipe(p1);
|
||||
pipe(p2);
|
||||
x1=fork();
|
||||
x2=fork();
|
||||
if (x1 > 0){
|
||||
close(p1[0]);
|
||||
write(p1[1],&x3, sizeof(int));
|
||||
sleep(2);
|
||||
write(p1[1],(int*) &x4, sizeof(int));
|
||||
x3 = strtod(argv[3], NULL);
|
||||
x4 = strtod(argv[4], NULL);
|
||||
write(p2[1], &x3, sizeof(int));
|
||||
sleep(2);
|
||||
write(p2[1], &x3, sizeof(int));
|
||||
close(p1[1]);
|
||||
close(p2[1]);
|
||||
dup(p1[0]);
|
||||
read(p1[0], &a, sizeof(int));
|
||||
sleep(2);
|
||||
read(p1[0], &b, sizeof(int));
|
||||
close(p1[0]);
|
||||
dup(p2[0]);
|
||||
read(p2[0], &c, sizeof(int));
|
||||
sleep(2);
|
||||
read(p2[0], &d, sizeof(int));
|
||||
close(p2[0]);
|
||||
printf("a = %d, b = %d, c = %d, d = %d\n", a, b, c, d);
|
||||
}
|
||||
if (x1 == 0){
|
||||
close(p1[1]);
|
||||
read(p1[0], &x3, sizeof(int));
|
||||
sleep(2);
|
||||
read(p1[0], &x4, sizeof(int));
|
||||
if (!ordre(x3, x4)){
|
||||
tmp = x4;
|
||||
x4 = x3;
|
||||
x3 = tmp;
|
||||
}
|
||||
printf("x3 = %d, x4 = %d", x3, x4);
|
||||
close(p1[0]);
|
||||
write(p1[1], &x3, sizeof(int));
|
||||
sleep(2);
|
||||
write(p1[1], &x4, sizeof(int));
|
||||
}
|
||||
if (x2 == 0){
|
||||
close(p2[1]);
|
||||
read(p2[0], &x3, sizeof(int));
|
||||
sleep(2);
|
||||
read(p2[0], &x4, sizeof(int));
|
||||
ordre(x3, x4);
|
||||
if (!ordre(x3, x4)){
|
||||
tmp = x3;
|
||||
x3 = x4;
|
||||
x4 = x3;
|
||||
}
|
||||
dup(p2[1]);
|
||||
close(p2[0]);
|
||||
write(p2[1], &x3, sizeof(int));
|
||||
sleep(2);
|
||||
write(p2[1], &x4, sizeof(int));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
87
SCR3.1/devoir27/src/cachetst.c
Normal file
87
SCR3.1/devoir27/src/cachetst.c
Normal file
@@ -0,0 +1,87 @@
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <x86intrin.h>
|
||||
#include <signal.h>
|
||||
|
||||
#define PLSIZE 63
|
||||
|
||||
|
||||
|
||||
struct lst {
|
||||
struct lst * next;
|
||||
long int payload[PLSIZE];
|
||||
} l;
|
||||
|
||||
|
||||
struct lst * mkws(int size)
|
||||
{
|
||||
int i,j;
|
||||
struct lst * ws0,*ws;
|
||||
|
||||
if (size == 0)
|
||||
return NULL;
|
||||
|
||||
srand(time(0));
|
||||
ws = calloc(1,sizeof(struct lst));
|
||||
ws->next = NULL;
|
||||
|
||||
for (i=0;i<PLSIZE;i++)
|
||||
ws->payload[i]=(rand())%2;
|
||||
|
||||
for (j=1;j<size;j++) {
|
||||
ws0=ws;
|
||||
ws = calloc(1,sizeof(struct lst));
|
||||
if (ws==NULL) {
|
||||
fprintf(stderr,"Can't add element...\n");
|
||||
exit(1);
|
||||
}
|
||||
for (i=0;i<PLSIZE;i++)
|
||||
ws->payload[i]=(rand())%2;
|
||||
ws->next = ws0;
|
||||
}
|
||||
return ws;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char ** argv)
|
||||
{
|
||||
struct lst * ws, * ws0;
|
||||
|
||||
long long int start, end, dif;
|
||||
|
||||
long int i, x, z = 0, size, it;
|
||||
|
||||
if (argc != 3) {
|
||||
printf("Usage: %s <list size> <iterations>\n",argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
size=strtol(argv[1],NULL,0);
|
||||
|
||||
ws = mkws(size);
|
||||
if (ws == NULL) {
|
||||
printf("NIL list created...\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
it = strtol(argv[2],NULL,0);
|
||||
|
||||
start = __rdtsc();
|
||||
for (i = 0; i < it; i++) {
|
||||
ws0 = ws;
|
||||
while (ws0 != NULL) {
|
||||
x = ws0->payload[3];
|
||||
x++;
|
||||
z++;
|
||||
ws0 = ws0->next;
|
||||
}
|
||||
}
|
||||
end = __rdtsc();
|
||||
|
||||
dif = end - start;
|
||||
printf("Cycles/element = %lf\n", ((double) dif)/((double) size*it));
|
||||
exit(0);
|
||||
}
|
19
SCR3.1/devoir27/src/helpers.c
Normal file
19
SCR3.1/devoir27/src/helpers.c
Normal file
@@ -0,0 +1,19 @@
|
||||
#include "helpers.h"
|
||||
#include <signal.h>
|
||||
#include <time.h>
|
||||
|
||||
int set_signal_handler(int signo, void (*handler)(int)) {
|
||||
struct sigaction sa;
|
||||
sa.sa_handler = handler; // call `handler` on signal
|
||||
sigemptyset(&sa.sa_mask); // don't block other signals in handler
|
||||
sa.sa_flags = SA_RESTART; // restart system calls
|
||||
return sigaction(signo, &sa, NULL);
|
||||
}
|
||||
|
||||
double tstamp(void) {
|
||||
struct timespec tv;
|
||||
clock_gettime(CLOCK_REALTIME, &tv);
|
||||
return tv.tv_sec + tv.tv_nsec * 1.0e-9;
|
||||
}
|
||||
|
||||
|
7
SCR3.1/devoir27/src/helpers.h
Normal file
7
SCR3.1/devoir27/src/helpers.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#ifndef _HELPERS_H
|
||||
#define _HELPERS_H
|
||||
|
||||
int set_signal_handler(int signo, void (*handler)(int));
|
||||
double tstamp(void);
|
||||
|
||||
#endif
|
95
SCR3.1/devoir27/src/vcachetest.c
Normal file
95
SCR3.1/devoir27/src/vcachetest.c
Normal file
@@ -0,0 +1,95 @@
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <x86intrin.h>
|
||||
#include <signal.h>
|
||||
#include "helpers.c"
|
||||
#include "helpers.h"
|
||||
|
||||
|
||||
#define PLSIZE 63
|
||||
|
||||
|
||||
|
||||
struct lst {
|
||||
struct lst * next;
|
||||
long int payload[PLSIZE];
|
||||
} l;
|
||||
|
||||
|
||||
struct lst * mkws(int size)
|
||||
{
|
||||
int i,j;
|
||||
struct lst * ws0,*ws;
|
||||
|
||||
if (size == 0)
|
||||
return NULL;
|
||||
|
||||
srand(time(0));
|
||||
ws = calloc(1,sizeof(struct lst));
|
||||
ws->next = NULL;
|
||||
|
||||
for (i=0;i<PLSIZE;i++)
|
||||
ws->payload[i]=(rand())%2;
|
||||
|
||||
for (j=1;j<size;j++) {
|
||||
ws0=ws;
|
||||
ws = calloc(1,sizeof(struct lst));
|
||||
if (ws==NULL) {
|
||||
fprintf(stderr,"Can't add element...\n");
|
||||
exit(1);
|
||||
}
|
||||
for (i=0;i<PLSIZE;i++)
|
||||
ws->payload[i]=(rand())%2;
|
||||
ws->next = ws0;
|
||||
}
|
||||
return ws;
|
||||
}
|
||||
|
||||
long int z = 0;
|
||||
|
||||
void kill_prg(int){
|
||||
printf("Nombre de tests effectués: %ld\n", z);
|
||||
}
|
||||
|
||||
int main(int argc, char ** argv)
|
||||
{
|
||||
struct lst * ws, * ws0;
|
||||
|
||||
long long int start, end, dif;
|
||||
|
||||
long int i, x, size, it;
|
||||
|
||||
if (argc != 3) {
|
||||
printf("Usage: %s <list size> <iterations>\n",argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
set_signal_handler(SIGTSTP, &kill_prg);
|
||||
size=strtol(argv[1],NULL,0);
|
||||
|
||||
ws = mkws(size);
|
||||
if (ws == NULL) {
|
||||
printf("NIL list created...\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
it = strtol(argv[2],NULL,0);
|
||||
|
||||
start = __rdtsc();
|
||||
for (i = 0; i < it; i++) {
|
||||
ws0 = ws;
|
||||
while (ws0 != NULL) {
|
||||
x = ws0->payload[3];
|
||||
x++;
|
||||
z++;
|
||||
ws0 = ws0->next;
|
||||
}
|
||||
}
|
||||
end = __rdtsc();
|
||||
|
||||
dif = end - start;
|
||||
printf("Cycles/element = %lf\n", ((double) dif)/((double) size*it));
|
||||
exit(0);
|
||||
}
|
Reference in New Issue
Block a user