Last active
October 2, 2018 09:48
-
-
Save Dysta/f642d476b6622eb895faf8abf65368d9 to your computer and use it in GitHub Desktop.
TD3 prog_sys
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
#include <sys/types.h> | |
#include <sys/wait.h> | |
int main(int argc, char* argv[]) { | |
int fd; | |
pid_t pid; | |
pid = fork(); | |
if (pid == 0 ) { | |
fd = open("tmp_file", O_CREAT | O_TRUNC | O_WRONLY, 0644); | |
dup2(fd, STDOUT_FILENO); | |
execlp(argv[1], argv[1], NULL); | |
} | |
wait(NULL); | |
pid = fork(); | |
if ( pid == 0) { | |
fd = open("tmp_file", O_RDONLY); | |
dup2(fd, STDIN_FILENO); | |
execvp(argv[2], argv + 2); | |
} | |
wait(NULL); | |
return EXIT_SUCCESS; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <sys/types.h> | |
#include <sys/wait.h> | |
int main(int argc, char* argv[]) { | |
pid_t pid1, pid2; | |
int status1, status2; | |
int tube[2]; // tube[0] lecture, tube[1] ecriture | |
pipe(tube); | |
pid1 = fork(); | |
if ( pid1 == 0 ) { // cmd1 va seulement écrire | |
close(tube[0]); // donc on ferme la lecture dans le fils | |
dup2(tube[1], STDOUT_FILENO); | |
execvp(argv[2], argv + 2); | |
perror("cmd"); | |
} | |
pid2 = fork(); | |
if ( pid2 == 0 ) { // cmd2 va seulement lire | |
close(tube[1]); // donc on ferme l'écriture | |
dup2(tube[0], STDIN_FILENO); | |
execlp("tee", "tee", argv[1], NULL); | |
perror("cmd2"); | |
} | |
// faire les closes avant les wait sinon on va attendre dans le vide | |
close(tube[0]); | |
close(tube[1]); | |
waitpid(pid1, &status1, WCONTINUED); | |
waitpid(pid2, &status2, WCONTINUED); | |
return ( WEXITSTATUS(status1) != 0 ) ? | |
WEXITSTATUS(status1) : | |
WEXITSTATUS(status2) ; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <unistd.h> | |
int main(int argc, char* argv[]) { | |
int tube[2]; // tube[0] lecture, tube[1] écriture | |
unsigned long int compteur = 0; | |
pipe(tube); | |
while( write(tube[1], "a", 1) != 0){ | |
printf("%lu\n", compteur); | |
compteur++; | |
} | |
close(tube[0]); | |
close(tube[1]); | |
return EXIT_SUCCESS; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <sys/types.h> | |
#include <sys/wait.h> | |
int main(int argc, char* argv[]) { | |
pid_t pid; | |
int tube[2]; // tube[0] lecture, tube[1] ecriture | |
pipe(tube); | |
pid = fork(); | |
if ( pid == 0 ) { // cmd1 va seulement écrire | |
close(tube[0]); // donc on ferme la lecture dans le fils | |
dup2(tube[1], STDOUT_FILENO); | |
execlp(argv[1], argv[1], NULL); | |
} | |
pid = fork(); | |
if ( pid == 0 ) { // cmd2 va seulement lire | |
close(tube[1]); // donc on ferme l'écriture | |
dup2(tube[0], STDIN_FILENO); | |
execvp(argv[2], argv + 2); | |
} | |
// faire les closes avant sinon on va attendre dans le vide | |
close(tube[0]); | |
close(tube[1]); | |
wait(NULL); | |
wait(NULL); | |
return EXIT_SUCCESS; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <sys/types.h> | |
#include <sys/wait.h> | |
int main(int argc, char* argv[]) { | |
pid_t pid1, pid2; | |
int status1, status2; | |
int tube[2]; // tube[0] lecture, tube[1] ecriture | |
pipe(tube); | |
pid1 = fork(); | |
if ( pid1 == 0 ) { // cmd1 va seulement écrire | |
close(tube[0]); // donc on ferme la lecture dans le fils | |
dup2(tube[1], STDOUT_FILENO); | |
execlp(argv[1], argv[1], NULL); | |
perror("cmd1"); | |
} | |
pid2 = fork(); | |
if ( pid2 == 0 ) { // cmd2 va seulement lire | |
close(tube[1]); // donc on ferme l'écriture | |
dup2(tube[0], STDIN_FILENO); | |
execvp(argv[2], argv + 2); | |
perror("cmd2"); | |
} | |
// faire les closes avant les wait sinon on va attendre dans le vide | |
close(tube[0]); | |
close(tube[1]); | |
waitpid(pid1, &status1, WCONTINUED); | |
waitpid(pid2, &status2, WCONTINUED); | |
return ( WEXITSTATUS(status1) != 0 ) ? | |
WEXITSTATUS(status1) : | |
WEXITSTATUS(status2) ; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment