commit 7cdaf924cf0f5ae089ac5229d3647bbbe9ea479f Author: framery Date: Thu Oct 2 15:09:43 2025 +0200 SCR diff --git a/TP1/framery-grp03-tp01-reponses.txt b/TP1/framery-grp03-tp01-reponses.txt new file mode 100644 index 0000000..10a4c5b --- /dev/null +++ b/TP1/framery-grp03-tp01-reponses.txt @@ -0,0 +1,11 @@ +Exo 1: +On peut voir que les adresses qu'affiche le script vmap: +main -> à l'adresse du code +gettimeoftheday -> à l'adresse [vdso] - bibliothèque externe +&argc -> à l'adresse de la pile [stack] +&i -> à l'adresse de la pile [stack] +&j -> à l'adresse des données du code [data] +t -> à l'adresse des données du code [data]. +m -> à l'adresse du tas [heap] + +Exo 2: \ No newline at end of file diff --git a/TP1/scripts/Makefile b/TP1/scripts/Makefile new file mode 100644 index 0000000..f6ddfde --- /dev/null +++ b/TP1/scripts/Makefile @@ -0,0 +1,17 @@ +CFLAGS := -Wall -g -O0 +SRC=buf.c heap.c huge.c mmap.c null.c stack.c + +DEPENDHELPERS=helpers.o + +BINARIES=$(SRC:%.c=%) + +%.o : %c + gcc -c $+ + +$(BINARIES): % : %.o $(DEPENDHELPERS) + gcc -o $@ $+ + +all : $(BINARIES) + +clean: + rm -f *.o $(BINARIES) diff --git a/TP1/scripts/buf.c b/TP1/scripts/buf.c new file mode 100644 index 0000000..4dbf387 --- /dev/null +++ b/TP1/scripts/buf.c @@ -0,0 +1,9 @@ +#include "helpers.h" + +static char buffer[16 MB] = {0}; + +int main(int argc, char **argv) +{ + randomize(buffer, 16 MB); + return interlude(); +} diff --git a/TP1/scripts/data/256k b/TP1/scripts/data/256k new file mode 100644 index 0000000..f3e3746 Binary files /dev/null and b/TP1/scripts/data/256k differ diff --git a/TP1/scripts/heap.c b/TP1/scripts/heap.c new file mode 100644 index 0000000..196ecb9 --- /dev/null +++ b/TP1/scripts/heap.c @@ -0,0 +1,8 @@ +#include "helpers.h" + +int main(int argc, char **argv) +{ + dirty(16 MB); + clean(32 MB); + return interlude(); +} diff --git a/TP1/scripts/helpers.c b/TP1/scripts/helpers.c new file mode 100644 index 0000000..1a2799f --- /dev/null +++ b/TP1/scripts/helpers.c @@ -0,0 +1,36 @@ +#include +#include +#include +#include +#include "helpers.h" + +#include + +void randomize(char *buf, size_t n) +{ + assert(buf); + memset(buf, rand() & 0xff, n); +} + +void clean(size_t b) +{ + for (; b > 0; b -= 1 KB) + calloc(1 KB, sizeof(char)); +} + +void dirty(size_t b) +{ + for (; b > 0; b -= 1 KB) + randomize(calloc(1 KB, sizeof(char)), 1 KB); +} + +int interlude(void) +{ + pid_t pid = getpid(); + printf("pid %i\n", (int)pid); + printf("------------------------------------------\n" + "go check /proc/%i/smaps; I'll wait...\n" + "press when you're done\n", pid); + fgetc(stdin); + return 0; +} diff --git a/TP1/scripts/helpers.h b/TP1/scripts/helpers.h new file mode 100644 index 0000000..d8a774a --- /dev/null +++ b/TP1/scripts/helpers.h @@ -0,0 +1,13 @@ +#ifndef _HELPERS_H +#define _HELPERS_H +#include + +#define KB * 1024 +#define MB * 1024 * 1024 + +void randomize(char *buf, size_t n); +void clean(size_t n); +void dirty(size_t n); +int interlude(void); + +#endif diff --git a/TP1/scripts/huge.c b/TP1/scripts/huge.c new file mode 100644 index 0000000..046aea0 --- /dev/null +++ b/TP1/scripts/huge.c @@ -0,0 +1,12 @@ +#include "helpers.h" + +int main(int argc, char **argv) +{ + char *under = malloc(96 KB); + randomize(under, 96 KB); + + char *over = malloc(256 KB); + randomize(over, 256 KB); + + return interlude(); +} diff --git a/TP1/scripts/mmap.c b/TP1/scripts/mmap.c new file mode 100644 index 0000000..ac62144 --- /dev/null +++ b/TP1/scripts/mmap.c @@ -0,0 +1,38 @@ +#include "helpers.h" +#include +#include +#include + + +int main(int argc, char **argv) +{ + /* inert map (never modified) */ + char *inert = mmap(NULL, 16 KB, + PROT_READ|PROT_WRITE, + MAP_ANONYMOUS|MAP_PRIVATE, + -1, 0); + /* anonymous, private mmap */ + char *anon_priv = mmap(NULL, 32 KB, + PROT_READ|PROT_WRITE, + MAP_ANONYMOUS|MAP_PRIVATE, + -1, 0); + randomize(anon_priv, 32 KB); + + /* anonymous, shared map */ + char *anon_shared = mmap(NULL, 64 KB, + PROT_READ|PROT_WRITE, + MAP_ANONYMOUS|MAP_SHARED, + -1, 0); + randomize(anon_shared, 64 KB); + + /* private, file-backed map */ + int fd = open("data/256k", O_RDWR); + assert(fd >= 0); + char *file = mmap(NULL, 256 KB, + PROT_READ|PROT_WRITE, + MAP_PRIVATE, + fd, 0); + randomize(file, 128 KB); + + return interlude(); +} diff --git a/TP1/scripts/null.c b/TP1/scripts/null.c new file mode 100644 index 0000000..fbd46f7 --- /dev/null +++ b/TP1/scripts/null.c @@ -0,0 +1,6 @@ +#include "helpers.h" + +int main(int argc, char **argv) +{ + return interlude(); +} diff --git a/TP1/scripts/parse_smaps.py b/TP1/scripts/parse_smaps.py new file mode 100644 index 0000000..4dac000 --- /dev/null +++ b/TP1/scripts/parse_smaps.py @@ -0,0 +1,123 @@ +#!/usr/bin/env python +# +# Author: Craig Chi +# + +import sys +import os +import getopt +from collections import defaultdict, OrderedDict +from subprocess import check_output + +def usage(): + print(""" +usage: parse_smaps.py [-p process_name] [-t memory_type] [-h] [smaps_filename] + +example: parse_smaps.py /proc/12424/smaps + parse_smaps.py -p smbd + parse_smaps.py -p smbd -t Pss +""") + + +def print_header(mem_idx): + print('=' * 70) + for title in zip(*map(lambda x: x.split('_'), mem_idx.keys()), + ('', '= Total : library')): + print('{:>8} + {:>8} + {:>8} + {:>8} {}'.format(*title,)) + print('=' * 70) + + +def main(): + try: + opts, args = getopt.getopt(sys.argv[1:], 'p:t:ah', + ['process-name=', 'memory-type=', + 'all', 'help']) + except getopt.GetoptError as err: + print(err) + sys.exit(2) + + ps_name = '' + mem_type = '' + mem_idx = OrderedDict([ + ('Private_Clean', 0), + ('Private_Dirty', 1), + ('Shared_Clean', 2), + ('Shared_Dirty', 3) + ]) + for o, a in opts: + if o in ('-p', '--process-name'): + ps_name = a + elif o in ('-t', '--memory-type'): + mem_type = a + mem_idx = {a: 0} + else: + usage() + sys.exit(2) + + if (len(args) == 0 and ps_name == '') or len(args) > 1: + usage() + sys.exit(2) + + smaps_file = '' + if ps_name == '': + smaps_file = os.path.abspath(args[0]) + else: + try: + pids = check_output(['pidof', ps_name]).decode().strip().split() + if len(pids) > 1: + print('There are multiple pids:') + for i, p in enumerate(pids): + cmdline_file = '/proc/' + p + '/cmdline' + with open(cmdline_file, 'r') as cmdline: + line = next(cmdline) + print('[{}] {:>8}: {}'.format(i, p, line)) + num = input('Choose which one process you want (default=0): ') + num = int(num) if num != '' else 0 + pid = pids[num] + else: + pid = pids[0] + except Exception as err: + print(err) + sys.exit(1) + + smaps_file = '/proc/' + pid + '/smaps' + + mapinfo = defaultdict(lambda: [0] * len(mem_idx)) + total = [0] * len(mem_idx) + + with open(smaps_file, 'r') as smap: + for line in smap: + line_arr = line.split() + if '-' in line_arr[0]: + if len(line_arr) < 6: + filename = '[anonymous]' + else: + filename = os.path.basename(line_arr[-1]) + else: + line_arr[0] = line_arr[0].strip(':') + + if line_arr[0] in mem_idx: + mapinfo[filename][mem_idx[line_arr[0]]] += int(line_arr[1]) + total[mem_idx[line_arr[0]]] += int(line_arr[1]) + + if mem_type == '': + print_header(mem_idx) + + for filename, mem in sorted(mapinfo.items(), key=lambda x: -sum(x[1])): + print('{:>5} kB + {:>5} kB + {:>5} kB + {:>5} kB' + ' = {:>5} kB : {:<}'.format(*mem, sum(mem), filename)) + + print('=' * 70) + print('{:>5} kB + {:>5} kB + {:>5} kB + {:>5} kB' + ' = {:>5} kB : Total'.format(*total, sum(total))) + + else: + for filename, mem in sorted(mapinfo.items(), key=lambda x: -sum(x[1])): + print('{:>11} kB {:<}'.format(mem[0], filename)) + + print('=' * 30) + print('Total: {} kB'.format(total[0])) + + +if __name__ == '__main__': + main() diff --git a/TP1/scripts/stack.c b/TP1/scripts/stack.c new file mode 100644 index 0000000..700eef1 --- /dev/null +++ b/TP1/scripts/stack.c @@ -0,0 +1,8 @@ +#include "helpers.h" + +int main (int argc, char **argv) +{ + char buf[28 KB] = {0}; + randomize(buf, 28 KB); + return interlude(); +} diff --git a/TP1/scripts/tpex1.c b/TP1/scripts/tpex1.c new file mode 100644 index 0000000..d648757 --- /dev/null +++ b/TP1/scripts/tpex1.c @@ -0,0 +1,27 @@ +/* adresses virtuelles d'un processus */ + +#include +#include +#include +#include +#include + +int t[1000] = {[0 ... 999] = 2}; + +int main(int argc, char * argv[]) +{ + int i=3; + static int j = 3; + char * m = (char*)malloc(1); + printf("je suis le pid %d\n\n",getpid()); + /* ------- Affichage des adresses --------*/ + printf("main\t\t=\t%p\n",main); + printf("gettimeofday\t=\t%p\n",gettimeofday); + printf("&argc\t\t=\t%p\n",&argc); + printf("&i\t\t=\t%p\n",&i); + printf("&j\t\t=\t%p\n",&j); + printf("t\t\t=\t%p\n",t); + printf("m\t\t=\t%p\n",m); + + getchar(); +} diff --git a/TP1/scripts/vmap.py b/TP1/scripts/vmap.py new file mode 100644 index 0000000..ae3a643 --- /dev/null +++ b/TP1/scripts/vmap.py @@ -0,0 +1,159 @@ +#!/usr/bin/python +# coding=utf-8 + +"""Tool to analyze and display the contents of /proc//maps""" + +import re +import itertools +import argparse +from dataclasses import dataclass + +MAPS_LINE_RE = re.compile(r""" + (?P[0-9a-f]+)-(?P[0-9a-f]+)\s+ # Address + (?P\S+)\s+ # Permissions + (?P[0-9a-f]+)\s+ # Map offset + (?P\S+)\s+ # Device node + (?P\d+)\s+ # Inode + (?P.*)\s+ # Pathname +""", re.VERBOSE) + + +def human_bytes(size): + modifier = 1 + while size > 1024: + modifier *= 1024 + size /= 1024 + return "%.1f%s" % (size, { + 1024**0: 'b', + 1024**1: 'k', + 1024**2: 'M', + 1024**3: 'G', + 1024**4: 'T', + }.get(modifier, " x%d" % modifier)) + + +@dataclass +class Record: + addr_start: int + addr_end: int + perms: str + offset: int + dev: str + inode: int + pathname: str + + @property + def size(self): + return self.addr_end - self.addr_start + + @property + def human_size(self): + return human_bytes(self.size) + + @property + def readable(self): + return self.perms[0] == "r" + + @property + def writable(self): + return self.perms[1] == "w" + + @property + def executable(self): + return self.perms[2] == "x" + + @property + def shared(self): + return self.perms[3] == "s" + + @property + def private(self): + return self.perms[3] == "p" + + @classmethod + def parse(self, pid): + records = [] + with open("/proc/%d/maps" % pid) as fd: + for line in fd: + m = MAPS_LINE_RE.match(line) + if not m: + print("Skipping: %s" % line) + continue + addr_start, addr_end, perms, offset, dev, inode, pathname = m.groups() + addr_start = int(addr_start, 16) + addr_end = int(addr_end, 16) + offset = int(offset, 16) + records.append(Record( + addr_start=addr_start, + addr_end=addr_end, + perms=perms, + offset=offset, + dev=dev, + inode=inode, + pathname=pathname, + )) + return records + + @classmethod + def aggregate(self, records, only_used=False, only_private=False): + + named_records = {} + anonymous_records = [] + for record in records: + if only_private and not record.private: + continue + if only_used and not record.readable and not record.writable and not record.shared and not record.pathname: + continue + if record.pathname: + if record.pathname in named_records: + other = named_records[record.pathname] + named_records[record.pathname] = Record( + min(record.addr_start, other.addr_start), + max(record.addr_end, other.addr_end), + perms=''.join("?" if c1 != c2 else c1 for c1, c2 in zip(record.perms, other.perms)), + offset=0, + dev='', + inode='', + pathname=record.pathname, + ) + else: + named_records[record.pathname] = record + else: + anonymous_records.append(record) + + return list(sorted( + itertools.chain(anonymous_records, named_records.values()), + key=lambda r: r.size, + reverse=True, + )) + + + +if __name__ == '__main__': + parser = argparse.ArgumentParser() + parser.add_argument("pid", type=int, help="Process identifier (pid)") + parser.add_argument("--only-used", "-u", action="store_true", help="Only show used pages (non readable, writable, executable and private pages)") + parser.add_argument("--only-private", "-p", action="store_true", help="Only show private pages") + args = parser.parse_args() + + records = Record.parse(args.pid) + #records = Record.aggregate(records, only_used=args.only_used, only_private=args.only_private) + + print("\t".join([ + "% 16s" % "Start of range", + "% 16s" % "End of range", + "% 12s" % "Size", + "% 4s" % "Perms", + "Path", + ])) + for record in records: + print("\t".join([ + "%016x" % record.addr_start, + "%016x" % record.addr_end, + "% 12s" % record.human_size, + "% 4s" % record.perms, + record.pathname, + ])) + + print("") + print("Total: %s" % human_bytes(sum(r.size for r in records))) diff --git a/TP2/TP2 reponses b/TP2/TP2 reponses new file mode 100644 index 0000000..fb98ce0 --- /dev/null +++ b/TP2/TP2 reponses @@ -0,0 +1,6 @@ +avec les appels systemes: +modifier fichier +lire fichier +afficher +modifier +afficher \ No newline at end of file diff --git a/TP2/a.out b/TP2/a.out new file mode 100755 index 0000000..87a82f6 Binary files /dev/null and b/TP2/a.out differ diff --git a/TP2/coherence-test.txt b/TP2/coherence-test.txt new file mode 100644 index 0000000..f23912c --- /dev/null +++ b/TP2/coherence-test.txt @@ -0,0 +1 @@ +L'asr, c'est bien!! diff --git a/TP2/coherence.c b/TP2/coherence.c new file mode 100644 index 0000000..f9023e5 --- /dev/null +++ b/TP2/coherence.c @@ -0,0 +1,96 @@ +#include +#include +#include +#include +#include + +#define TESTFILE "coherence-test.txt" + +void write_asr_c_pas_bien() +{ + FILE* f = fopen(TESTFILE, "w"); + fprintf(f, "L'asr, c'est pas bien!\n"); + fclose(f); +} + +void write_asr_c_bien() +{ + FILE* f = fopen(TESTFILE, "w"); + fprintf(f, "L'asr, c'est bien!!\n"); + fclose(f); +} + + +void read_using_syscalls() +{ + write_asr_c_pas_bien(); + + // open file descriptor + int fd = open(TESTFILE, O_RDONLY); + + // read first 12 bytes + char buf[BUFSIZ]; + ssize_t nr1 = read(fd, buf, 12); + assert(nr1 >= 0); + + write_asr_c_bien(); + + // read rest of file, print to stdout + ssize_t nr2 = read(fd, buf + nr1, BUFSIZ - nr1); + close(fd); + fwrite(buf, 1, nr1 + nr2, stdout); +} + + +void read_using_stdio() +{ + write_asr_c_pas_bien(); + + // open stdio file + FILE* f = fopen(TESTFILE, "r"); + + // read first 12 bytes + char buf[BUFSIZ]; + size_t nr1 = fread(buf, 1, 12, f); + + write_asr_c_bien(); + + // read rest of file, print to stdout + size_t nr2 = fread(buf + nr1, 1, BUFSIZ - nr1, f); + fclose(f); + fwrite(buf, 1, nr1 + nr2, stdout); +} + + +static void usage() +{ + fprintf(stderr, "Usage: ./coherence -l (linux syscalls) or ./coherence -s (stdio)\n"); + exit(1); +} + +int main(int argc, char** argv) +{ + int which = 0; + + int opt; + while ((opt = getopt(argc, argv, "ls")) != -1) { + switch (opt) { + case 's': + which = 's'; + break; + case 'l': + which = 'l'; + break; + default: + usage(); + } + } + + if (which == 's') { + read_using_stdio(); + } else if (which == 'l') { + read_using_syscalls(); + } else { + usage(); + } +} diff --git a/TP2/copy b/TP2/copy new file mode 100755 index 0000000..943e124 Binary files /dev/null and b/TP2/copy differ diff --git a/TP2/copy.c b/TP2/copy.c new file mode 100644 index 0000000..b45f423 --- /dev/null +++ b/TP2/copy.c @@ -0,0 +1,35 @@ +#include +#include +#include + +#define BLOCK_SIZE 1 + + +int main(int argc, char *argv[]) +{ + int fin, + fout; + char buf[BLOCK_SIZE]; + + assert( argc == 3 ); + + fin = open(argv[1],O_RDONLY); + assert( fin >= 0 ); + + fout = open(argv[2],O_CREAT|O_WRONLY|O_TRUNC,0600); + assert( fout >= 0 ); + + while(1){ + ssize_t nb_read; + nb_read = read(fin,buf,BLOCK_SIZE); + if (nb_read <= 0) + break; + write(fout,buf,nb_read); + } + + close(fin); + close(fout); + + return 0; +} + diff --git a/TP2/copyv1 b/TP2/copyv1 new file mode 100755 index 0000000..3c26a93 Binary files /dev/null and b/TP2/copyv1 differ diff --git a/TP2/copyv2 b/TP2/copyv2 new file mode 100755 index 0000000..50523d2 Binary files /dev/null and b/TP2/copyv2 differ diff --git a/TP2/copyv2.c b/TP2/copyv2.c new file mode 100644 index 0000000..3a181c2 --- /dev/null +++ b/TP2/copyv2.c @@ -0,0 +1,38 @@ +#include +#include + +int copier_fichier(const char *source, const char *dest){ + int octet; + FILE *fichier_source, *fichier_dest; + fichier_source = fopen(source, "rb"); + if (fichier_source == NULL) { + perror("Erreur lors de l'ouverture du fichier source"); + exit(EXIT_FAILURE); + } + fichier_dest = fopen(dest, "wb"); + if (fichier_dest == NULL) { + perror("Erreur lors de l'ouverture du fichier source"); + exit(EXIT_FAILURE); + } + while((octet = fgetc(fichier_source)) != EOF){ + fputc(octet, fichier_dest); + } + fclose(fichier_source); + fclose(fichier_dest); + printf("Copie terminé avec grand succès!\n"); + return EXIT_SUCCESS; +} + +int main(int argc, char *argv[]){ + if (argc != 3) { + printf("Usage : %s [-a] \n", argv[0]); + return EXIT_FAILURE; + } + if (copier_fichier(argv[1], argv[2]) == 0){ + printf("Fichier copié avec succès de %s vers %s\n", argv[1], argv[2]); + return EXIT_SUCCESS; + } else{ + printf("Echec de la copie"); + return EXIT_FAILURE; + } +} \ No newline at end of file diff --git a/TP2/dess.data b/TP2/dess.data new file mode 100644 index 0000000..d7a71ef Binary files /dev/null and b/TP2/dess.data differ diff --git a/TP2/dest.data b/TP2/dest.data new file mode 100644 index 0000000..d7a71ef Binary files /dev/null and b/TP2/dest.data differ diff --git a/TP2/ecruos.txt b/TP2/ecruos.txt new file mode 100644 index 0000000..23bea44 --- /dev/null +++ b/TP2/ecruos.txt @@ -0,0 +1 @@ +GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGIIIIIIIIIIIIIIIOOOOOOOOOOOOYYYYYYYYYYYEEEEEEEEEEEEEEZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ diff --git a/TP2/empty.txt b/TP2/empty.txt new file mode 100644 index 0000000..23bea44 --- /dev/null +++ b/TP2/empty.txt @@ -0,0 +1 @@ +GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGIIIIIIIIIIIIIIIOOOOOOOOOOOOYYYYYYYYYYYEEEEEEEEEEEEEEZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ diff --git a/TP2/my_cp_map.c b/TP2/my_cp_map.c new file mode 100644 index 0000000..c425b68 --- /dev/null +++ b/TP2/my_cp_map.c @@ -0,0 +1,70 @@ +#include +#include +#include +#include +#include +#include +#include +#include + + +#define BUFSIZE 1024 + + +static inline double tstamp(void) +{ + struct timespec tv; + clock_gettime(CLOCK_REALTIME, &tv); + return tv.tv_sec + tv.tv_nsec * 1.0e-9; +} + +int main(int argc, char *argv[]) +{ + //char buf[BUFSIZE]; + char * bufin = NULL, + * bufout = NULL; + + + int fin, + fout; + + double start, + end; + + size_t filesize = 0; + + assert(argc == 3); + + start = tstamp(); + + fin = open(argv[1],O_RDONLY); + assert(fin >=0); + + fout = open(argv[2],O_RDWR|O_CREAT|O_TRUNC,0600); + assert(fout >=0); + + filesize = lseek(fin,0,SEEK_END); + ftruncate (fout,filesize); + + bufin = mmap(NULL,filesize,PROT_READ,MAP_PRIVATE,fin,0); + assert(bufin != (void*)-1); + + bufout = mmap(NULL,filesize,PROT_WRITE,MAP_SHARED,fout,0); + + assert(bufout != (void*)-1); + + + memcpy(bufout,bufin,filesize); + + //munmap(bufin,filesize); + //munmap(bufout,filesize); + + //ssize_t nb_read = read(fin,buf,sizeof(buf)); + close(fin); + close(fout); + + end = tstamp(); + + printf("time = %.3lf\n",end - start); + return 0; +} diff --git a/TP2/source.txt b/TP2/source.txt new file mode 100644 index 0000000..23bea44 --- /dev/null +++ b/TP2/source.txt @@ -0,0 +1 @@ +GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGIIIIIIIIIIIIIIIOOOOOOOOOOOOYYYYYYYYYYYEEEEEEEEEEEEEEZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ diff --git a/TP2/source.txt~ b/TP2/source.txt~ new file mode 100644 index 0000000..e69de29 diff --git a/TP2/test.data b/TP2/test.data new file mode 100644 index 0000000..d7a71ef Binary files /dev/null and b/TP2/test.data differ diff --git a/TP2/tset.data b/TP2/tset.data new file mode 100644 index 0000000..d7a71ef Binary files /dev/null and b/TP2/tset.data differ diff --git a/TP3/exo1/ex1-stdio.c b/TP3/exo1/ex1-stdio.c new file mode 100644 index 0000000..4bc5de2 --- /dev/null +++ b/TP3/exo1/ex1-stdio.c @@ -0,0 +1,12 @@ +#include +#include +#include + + +int main(){ + + printf("NON"); + if (fork()) { + printf("OUI\n"); + } +} diff --git a/TP3/exo1/ex1-syscall.c b/TP3/exo1/ex1-syscall.c new file mode 100644 index 0000000..1b46b6a --- /dev/null +++ b/TP3/exo1/ex1-syscall.c @@ -0,0 +1,14 @@ +#include +#include +#include + + +int main(){ + + //printf("NON"); + write(STDOUT_FILENO,"NON",3); + if (fork()) { + //printf("OUI\n"); + write(STDOUT_FILENO,"OUI\n",4); + } +} diff --git a/TP3/exo1/rep ex1.txt b/TP3/exo1/rep ex1.txt new file mode 100644 index 0000000..a2ddc6e --- /dev/null +++ b/TP3/exo1/rep ex1.txt @@ -0,0 +1,9 @@ +ce que fait grossièrement le fichier ex1-stdio.c est: +-Il affiche d'abord le résultat du printf (NON) -> l'enregistre dans le buffer +-puis il lance une condition du fork (la fonction qui va gérer les processus) +-Donc le processus duplique le père (printf("NON')) et le fils dans le buffer +-Le père écrit NON à la suite du OUI (fils) +Donc le résultat sera: +NONOUI +NON +et les 2 sont vidés à la fin dans le terminal. diff --git a/TP3/exo1/rep ex1.txt~ b/TP3/exo1/rep ex1.txt~ new file mode 100644 index 0000000..e69de29 diff --git a/TP3/exo1/stdio b/TP3/exo1/stdio new file mode 100755 index 0000000..2321e4e Binary files /dev/null and b/TP3/exo1/stdio differ diff --git a/TP3/exo1/syscall b/TP3/exo1/syscall new file mode 100755 index 0000000..69b77f1 Binary files /dev/null and b/TP3/exo1/syscall differ diff --git a/TP3/exo2/#exo2-rep.txt# b/TP3/exo2/#exo2-rep.txt# new file mode 100644 index 0000000..152939d --- /dev/null +++ b/TP3/exo2/#exo2-rep.txt# @@ -0,0 +1 @@ +pour l'exo 2, \ No newline at end of file diff --git a/TP3/exo2/copy1byte.c b/TP3/exo2/copy1byte.c new file mode 100644 index 0000000..02fe937 --- /dev/null +++ b/TP3/exo2/copy1byte.c @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define msg1 "je suis le pere" +#define msg2 "je suis le fils !!!" + +int main(int argc,char * argv[]){ + + int infd,outfd; + ssize_t nbread; + char buf[1]; + pid_t p; + + if (argc != 3){ + + printf("%s infile outfile\n",argv[0]); + exit(1); + } + + infd = open(argv[1],O_RDONLY); + assert(infd >= 0); + outfd = open(argv[2],O_WRONLY|O_CREAT|O_TRUNC,0644); + assert(outfd >= 0); + + //p=fork(); // <- decommentez cette ligne + + while(1){ + + nbread=read(infd,buf,sizeof(buf)); + if (nbread <=0 ) break; + write(outfd,buf,sizeof(buf)); + + } + + + close(infd); + close(outfd); +} diff --git a/TP3/exo2/fork_and_fd1.c b/TP3/exo2/fork_and_fd1.c new file mode 100644 index 0000000..2c529b9 --- /dev/null +++ b/TP3/exo2/fork_and_fd1.c @@ -0,0 +1,42 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define msg1 "je suis le pere" +#define msg2 "je suis le fils !!!" + +int main(int argc,char * argv[]){ + + int outfd; + pid_t p; + + if (argc != 2){ + + printf("%s file\n",argv[0]); + exit(1); + } + + //p=fork(); + outfd = open(argv[1],O_WRONLY|O_CREAT,0644); + assert(outfd >= 0); + p=fork(); + switch(p){ + case (pid_t)-1 : + perror(NULL); + exit(2); + + case (pid_t)0 : + write(outfd,msg2,strlen(msg2)); + break; + default : + write (outfd,msg1,strlen(msg1)); + break; + } + close(outfd); +} diff --git a/TP3/exo2/fork_and_fd2.c b/TP3/exo2/fork_and_fd2.c new file mode 100644 index 0000000..7a63fe5 --- /dev/null +++ b/TP3/exo2/fork_and_fd2.c @@ -0,0 +1,42 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define msg1 "je suis le pere" +#define msg2 "je suis le fils !!!" + +int main(int argc,char * argv[]){ + + int outfd; + pid_t p; + + if (argc != 2){ + + printf("%s file\n",argv[0]); + exit(1); + } + + p=fork(); + outfd = open(argv[1],O_WRONLY|O_CREAT,0644); + assert(outfd >= 0); + //p=fork(); + switch(p){ + case (pid_t)-1 : + perror(NULL); + exit(2); + + case (pid_t)0 : + write(outfd,msg2,strlen(msg2)); + break; + default : + write (outfd,msg1,strlen(msg1)); + break; + } + close(outfd); +} diff --git a/TP4/Ex1:/filexec.c b/TP4/Ex1:/filexec.c new file mode 100644 index 0000000..c87b5ff --- /dev/null +++ b/TP4/Ex1:/filexec.c @@ -0,0 +1,18 @@ +#include +#include +#include +#include +#include +#include + +int main(int argc, char* argv[]){ + for(int i=3;i +#include +#include +#include +#include +#include + +int main(int argc, char* argv[]){ + for(int i=2;i +#include +#include +#include + +int main(int argc, char* argv[]) +{ + assert(argc == 2); + + int value = strtol(argv[1], NULL, 0); + pid_t p = getpid(); + printf("%d: debut\n",p); + for(int i = value; i>0; i--){ + printf("%d: %d\n",p,i); + sleep(1); + } + printf("%d: fin\n",p); + return EXIT_SUCCESS; +} diff --git a/TP4/Ex2/pi b/TP4/Ex2/pi new file mode 100755 index 0000000..e0d15d5 Binary files /dev/null and b/TP4/Ex2/pi differ diff --git a/TP4/Ex2/pi.c b/TP4/Ex2/pi.c new file mode 100644 index 0000000..65c1269 --- /dev/null +++ b/TP4/Ex2/pi.c @@ -0,0 +1,31 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +uint64_t shots_in=0,shots=0; +double x,y; + +void signalrm_handler(int n){ + +} + +int main(int argc, char **argv) +{ + shots_in = 0; + shots = 0; + for (;;){ + x = rand()/(RAND_MAX*1.0); + y = rand()/(RAND_MAX*1.0); + shots ++; + if ((x*x+y*y)<=1){ + shots_in ++; + } + } + /* la probabilité vaut tirsIn/tirs, + Elle converge lentement vers pi/4*/ +} \ No newline at end of file