| | 1 | = Programmation des tubes Linux = |
| | 2 | |
| | 3 | == Travail demandé == |
| | 4 | |
| | 5 | Le but de ce TME est de vous faire programmer un squelette de programme qui sera repris lors du projet. |
| | 6 | Le comportement de cette application est simple, parce que son objectif est de vous faire manipuler les fifos Unix entre deux processus programmés en C et en python. |
| | 7 | |
| | 8 | [[Image(htdocs:png/fake2server.png,nolink,400px)]] |
| | 9 | |
| | 10 | Vous avez une certaine liberté sur le comportement pourvu que le programme utilise select et utilise un "serveur" en python. Par exemple : |
| | 11 | - fake lit une valeur sur stdin, |
| | 12 | - fake ajoute un numéro id et écrit le message sur f2s, |
| | 13 | - le server lit le message, calcule la parité et renvoie l'id et la parité sur s2f, |
| | 14 | - fake lit le message et affiche l'id, la valeur et la parité. |
| | 15 | |
| | 16 | Vous devez proposer un Makefile qui effectue la compilation et des benchs d'autotest. |
| | 17 | |
| | 18 | Vous rédigerez un rapport expliquant le comportement effectif, le code, le Makefile et les tests. |
| | 19 | |
| | 20 | == Annexes == |
| | 21 | |
| | 22 | Pour vous aider, nous vous proposons des bribes de code utilisant les tubes |
| | 23 | |
| | 24 | Lecteur C |
| | 25 | {{{ |
| | 26 | //http://stackoverflow.com/questions/2784500/how-to-send-a-simple-string-between-two-programs-using-pipes |
| | 27 | #include <fcntl.h> |
| | 28 | #include <stdio.h> |
| | 29 | #include <sys/stat.h> |
| | 30 | #include <unistd.h> |
| | 31 | |
| | 32 | #define MAX_BUF 1024 |
| | 33 | |
| | 34 | int main() |
| | 35 | { |
| | 36 | int err; |
| | 37 | int fd; |
| | 38 | char * myfifo = "/tmp/myfifo"; |
| | 39 | char buf[MAX_BUF]; |
| | 40 | |
| | 41 | /* open, read, and display the message from the FIFO */ |
| | 42 | fd = open(myfifo, O_RDONLY); |
| | 43 | while(1) { |
| | 44 | if ((err =read(fd, buf, MAX_BUF)) == 0) break; |
| | 45 | printf("Received (%d): %s\n", err,buf); |
| | 46 | } |
| | 47 | close(fd); |
| | 48 | |
| | 49 | return 0; |
| | 50 | } |
| | 51 | }}} |
| | 52 | |
| | 53 | Ecrivain C |
| | 54 | {{{ |
| | 55 | // http://stackoverflow.com/questions/2784500/how-to-send-a-simple-string-between-two-programs-using-pipes |
| | 56 | #include <fcntl.h> |
| | 57 | #include <sys/stat.h> |
| | 58 | #include <sys/types.h> |
| | 59 | #include <unistd.h> |
| | 60 | |
| | 61 | int main() |
| | 62 | { |
| | 63 | int fd; |
| | 64 | char * myfifo = "/tmp/myfifo"; |
| | 65 | |
| | 66 | /* create the FIFO (named pipe) */ |
| | 67 | mkfifo(myfifo, 0666); |
| | 68 | |
| | 69 | /* write "Hi" to the FIFO */ |
| | 70 | fd = open(myfifo, O_WRONLY); |
| | 71 | while (1) { |
| | 72 | write(fd, "hello\n", sizeof("hello")); |
| | 73 | sleep(3); |
| | 74 | } |
| | 75 | close(fd); |
| | 76 | |
| | 77 | /* remove the FIFO */ |
| | 78 | unlink(myfifo); |
| | 79 | |
| | 80 | return 0; |
| | 81 | } |
| | 82 | }}} |
| | 83 | |
| | 84 | Lecteur Python |
| | 85 | {{{ |
| | 86 | import os, time |
| | 87 | |
| | 88 | pipe_name = '/tmp/myfifo' |
| | 89 | |
| | 90 | if not os.path.exists(pipe_name): |
| | 91 | os.mkfifo(pipe_name) |
| | 92 | |
| | 93 | pipe_in = open(pipe_name,'r',0) |
| | 94 | while 1: |
| | 95 | str = pipe_in.readline() |
| | 96 | print '%s' % str, |
| | 97 | }}} |
| | 98 | |
| | 99 | Ecrivain Python |
| | 100 | {{{ |
| | 101 | import os, time |
| | 102 | |
| | 103 | pipe_name = '/tmp/myfifo' |
| | 104 | |
| | 105 | if not os.path.exists(pipe_name): |
| | 106 | os.mkfifo(pipe_name) |
| | 107 | |
| | 108 | pipe_out = open(pipe_name,'w') |
| | 109 | while 1: |
| | 110 | pipe_out.write("hello") |
| | 111 | pipe_out.flush() |
| | 112 | time.sleep(1) |
| | 113 | }}} |