Est-il possible d'utiliser des pthread au lieu d'un fork, pour le code suivant ?
J'ai essayé, en mettant les flux en argument. Mais lorsque je ferme l'écriture ou la lecture d'un flux dans un thread, cela le ferme aussi pour l'autre thread...
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 #include <stdio.h> #include <memory.h> #include <unistd.h> int main( int argc, char ** argv ) { /* create the pipe */ int pfd[2]; if (pipe(pfd) == -1) { printf("pipe failed\n"); return 1; } /* create the child */ int pid; if ((pid = fork()) < 0) { printf("fork failed\n"); return 2; } if (pid == 0) { /* child */ close(pfd[1]); /* close the unused write side */ dup2(pfd[0], 0); /* connect the read side with stdin */ close(pfd[0]); /* close the read side */ /* execute the process (wc command) */ execlp("wc", "wc", (char *) 0); printf("wc failed"); /* if execlp returns, it's an error */ return 3; } else { /* parent */ close(pfd[0]); /* close the unused read side */ dup2(pfd[1], 1); /* connect the write side with stdout */ close(pfd[1]); /* close the write side */ /* execute the process (ls command) */ execlp("ls", "ls", (char *)0); printf("ls failed"); /* if execlp returns, it's an error */ return 4; } return 0; }
Donc cela ne fonctionne pas.
Comment peut-on faire?
Partager