progsys:index
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
progsys:index [2017/10/19 15:36] – [Une chaîne de commandes] orel | progsys:index [2024/03/18 15:06] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 790: | Line 790: | ||
int k = atoi(argv[1]); | int k = atoi(argv[1]); | ||
- | for(int s = 1 ; s < argc ; s++) | + | for(int s = 2 ; s < argc ; s++) |
for(int i = 0 ; i < k ; i++) { | for(int i = 0 ; i < k ; i++) { | ||
kill(ppid, atoi(argv[s])); | kill(ppid, atoi(argv[s])); | ||
Line 809: | Line 809: | ||
sigemptyset(& | sigemptyset(& | ||
| | ||
- | for(int sig = 2 ; sig < NSIGNORT ; sig++) { | + | for(int sig = 1 ; sig < NSIGNORT ; sig++) { |
sigaction(sig, | sigaction(sig, | ||
tab[sig] = 0; | tab[sig] = 0; | ||
Line 917: | Line 917: | ||
| | ||
return 0; | return 0; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== Open Pipe Command (exo 3, DS 2017-2018) ==== | ||
+ | |||
+ | On souhaite disposer d’une fonction // | ||
+ | |||
+ | int open_pipe_command (int *fd, char *cmd, char **argv) | ||
+ | |||
+ | Cette fonction crée un processus chargé d’exécuter la commande //cmd// avec les arguments //argv//, et renvoie deux descripteurs (dans le tableau fd) permettant respectivement d’écrire vers l’entrée standard de la commande (descripteur fd[1]) et de lire depuis la sortie standard de la commande (descripteur fd[0]). On utilisera pour cela deux tubes permettant d’établir une connexion bidirectionnelle avec le processus exécutant la commande. La fonction retournera le //pid// du processus fils exécutant la commande. | ||
+ | |||
+ | <code C open_pipe_command.c> | ||
+ | /* gcc -Wall -std=c99 open_pipe_command.c && ./a.out */ | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | #define HELLO "hello world!" | ||
+ | |||
+ | int open_pipe_command(int * fd, char * cmd, char **argv) | ||
+ | { | ||
+ | int pr[2]; pipe(pr); | ||
+ | int pw[2]; pipe(pw); | ||
+ | /* user reads in fd[0]=pr[0] & writes in fd[1]=pw[1] */ | ||
+ | fd[0] = pr[0]; fd[1] = pw[1]; | ||
+ | int pid = fork(); | ||
+ | if(pid == 0) { /* child */ | ||
+ | close(pw[1]); | ||
+ | /* cmd writes in pr[1]=1 & reads in pw[0]=0 */ | ||
+ | dup2(pw[0], | ||
+ | close(pw[0]); | ||
+ | execvp(cmd, argv); | ||
+ | perror(" | ||
+ | exit(EXIT_FAILURE); | ||
+ | } | ||
+ | close(pw[0]); | ||
+ | return pid; /* success > 0*/ | ||
+ | } | ||
+ | |||
+ | int main(int argc, char * argv[]) | ||
+ | { | ||
+ | int fd[2]; | ||
+ | char* cmd[] = {" | ||
+ | int pid = open_pipe_command(fd, | ||
+ | if(pid <= 0) return EXIT_FAILURE; | ||
+ | write(fd[1], | ||
+ | close(fd[1]); | ||
+ | char msg[128]; | ||
+ | int r = read(fd[0], msg, 128); | ||
+ | printf(" | ||
+ | close(fd[0]); | ||
+ | waitpid(pid, | ||
+ | return EXIT_SUCCESS; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== Ctrl-C ==== | ||
+ | |||
+ | Un processus fils est dans le même PGID (process group ID) que son père par defaut (pgid père = pgid fils = pid père). Un kill sur -pid du père envoie le signal à tous le groupe, donc au fils également... | ||
+ | |||
+ | <code C sigint.c> | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | void handler(int sig) | ||
+ | { | ||
+ | printf(" | ||
+ | } | ||
+ | |||
+ | int main(void) | ||
+ | { | ||
+ | signal(SIGINT, | ||
+ | |||
+ | if (fork() == 0) | ||
+ | { | ||
+ | printf(" | ||
+ | pause(); | ||
+ | printf(" | ||
+ | exit(EXIT_SUCCESS); | ||
+ | } | ||
+ | |||
+ | printf(" | ||
+ | pause(); | ||
+ | printf(" | ||
+ | return EXIT_SUCCESS; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | ==== Timeout ==== | ||
+ | |||
+ | <code C executer-avant-delai.c> | ||
+ | |||
+ | #define _GNU_SOURCE | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | static struct sigaction sa, old; | ||
+ | static sigjmp_buf env; | ||
+ | |||
+ | static void myalarm(int sig) | ||
+ | { | ||
+ | printf(" | ||
+ | siglongjmp(env, | ||
+ | } | ||
+ | |||
+ | int executer_avant_delai( void (*fun)(void *), void *parametre, int delai_en_seconde) | ||
+ | { | ||
+ | int ret = 1; | ||
+ | sa.sa_handler = myalarm; | ||
+ | sa.sa_flags = 0; // SA_RESETHAND; | ||
+ | sigemptyset(& | ||
+ | sigaction(SIGALRM, | ||
+ | |||
+ | alarm(delai_en_seconde); | ||
+ | if(sigsetjmp(env, | ||
+ | fun(parametre); | ||
+ | else | ||
+ | ret = 0; // alarm | ||
+ | alarm(0); | ||
+ | sigaction(SIGALRM, | ||
+ | return ret; | ||
} | } | ||
</ | </ |
progsys/index.1508427363.txt.gz · Last modified: 2024/03/18 15:05 (external edit)