mise à jour

This commit is contained in:
2021-09-29 11:31:40 +02:00
parent a1b9d70868
commit c2b219f7a0
9 changed files with 366 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
char tabRef[1000];
int nbLu;
char c;
int main(void) {
printf("Caractéristiques après recouvrement\n");
printf(" => Identité du processus:%d\n",getpid());
printf(" => Identité du processus père:%d\n",getppid());
printf(" => Propriétaire réel : %d\n", getuid());
printf(" => Propriétaire effectif : %d\n", geteuid());
printf(" => Répertoire de travail:%s\n",getcwd(tabRef,1000));
if ((nbLu = read(STDIN_FILENO,&c,1)) == -1)
perror(" read");
else
printf(" Valeur du read : %d\n", nbLu);
return EXIT_SUCCESS;
}

View File

@@ -0,0 +1,22 @@
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
char tabRef[1000];
main() {
printf("Caractéristiques avant recouvrement\n");
printf(" Identité du processus : %d\n", getpid());
printf(" Identité du processus père:%d\n",getppid());
printf(" Propriétaire réel : %d\n", getuid());
printf(" Propriétaire effectif : %d\n", geteuid());
printf(" Répertoire de travail:%s\n",getcwd(tabRef,1000));
/* demande de fermeture automatique de lentrée standard au recouvrement */
fcntl(STDIN_FILENO, F_SETFD, fcntl(STDIN_FILENO,F_GETFD,0)|FD_CLOEXEC);
execl("apresExec", "apresExec", NULL);
perror("execl");
}

View File

@@ -0,0 +1,18 @@
#include <stdio.h>
#include <stdlib.h>
extern char **environ;
int main(int argc, char *argv[]) {
int ind;
char **ptr;
for (ind = 0; ind < argc; ind++)
printf("%s\n", argv[ind]);
ptr = environ;
while (*ptr != NULL)
printf("%s\n", *ptr++);
return EXIT_SUCCESS;
}

View File

@@ -0,0 +1,16 @@
/* binaire : recouvre1 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
char *envp[3];
int main() {
envp[0] = "X=AAAAA"; envp[1] = "Y=BBB"; envp[2] = NULL;
execl( "recouvrante", "recouvrante", "/tmp/ddd", NULL, envp);
perror("execl");
return EXIT_FAILURE;
}

View File

@@ -0,0 +1,15 @@
/* binaire : recouvre2 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
char *envp[3];
int main() {
envp[0] = "X=AAAAA"; envp[1] = "Y=BBB"; envp[2] = NULL;
execle( "recouvrante", "recouvrante", "/tmp/ddd", NULL, envp);
perror("execle");
return EXIT_FAILURE;
}

View File

@@ -0,0 +1,13 @@
#include <stdio.h>
#include <unistd.h>
#define NMAX 5
main() {
char *argv[NMAX]; /* NMAX > 3 */
argv[0] = "ls"; argv[1] = "-l"; argv[2]="/"; argv[3]=NULL;
execv("/bin/ls", argv);
perror("execv");
}

View File

@@ -0,0 +1,99 @@
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#define MAXARGS 128
#define MAXLINE 80
#define FOREVER while(1)
extern char **environ;
void eval(char *cmdline);
int parseline(char *buf, char **argv);
int builtin_cmd(char **argv);
int main() {
char cmdline[MAXLINE];
FOREVER {
printf("> ");
fgets(cmdline, MAXLINE, stdin);
if (feof(stdin)) exit(0);
eval(cmdline);
}
return EXIT_SUCCESS;
}
void eval(char *cmdline) {
char *argv[MAXARGS];
char buf[MAXLINE];
int bg;
pid_t pid;
strcpy(buf, cmdline);
bg = parseline(buf, argv);
if (argv[0] == NULL) {
return;
}
if (!builtin_cmd(argv)) {
if ((pid = fork()) == 0) {
if (execvp(argv[0], argv) < 0) {
printf("%s: cette commande n'existe pas.\n", argv[0]);
exit(0);
}
}
if (!bg) {
int status;
if (waitpid(pid, &status, 0) < 0) {
perror("waitfg: waitpid error"); exit(0);
}
}
else {
printf("%d %s", pid, cmdline);
}
}
return;
}
int builtin_cmd(char **argv) {
if (!strcmp(argv[0], "quit"))
exit(0);
if (!strcmp(argv[0], "&"))
return 1;
return 0;
}
int parseline(char *buf, char **argv) {
char *delim;
int argc;
int bg;
buf[strlen(buf)-1] = ' ';
while (*buf && (*buf == ' ')) {
buf++;
}
argc = 0;
while ((delim = strchr(buf, ' '))) {
argv[argc++] = buf;
*delim = '\0';
buf = delim+1;
while (*buf && (*buf == ' ')) buf++;
}
argv[argc] = NULL;
if (argc == 0) return 1;
if ((bg = (*argv[argc-1] == '&')) != 0) {
argv[--argc] = NULL;
}
return bg;
}

35
Exemples/concurr.c Normal file
View File

@@ -0,0 +1,35 @@
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
int desc_read, desc_write;
char c;
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Deux noms de fichiers comme arguments\n");
exit(1);
}
if ((desc_read = open(argv[1], O_RDONLY)) == -1) {
perror(argv[1]);
exit(1);
}
if ((desc_write = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1) {
perror(argv[2]);
exit(1);
}
for(;;) {
if (read(desc_read, &c, 1) != 1) exit(0);
write(desc_write, &c, 1);
}
close(desc_read);
close(desc_write);
return EXIT_SUCCESS;
}