First commit

This commit is contained in:
Yorick Barbanneau 2018-10-12 23:06:04 +02:00
commit 5c269302ed
59 changed files with 5613 additions and 0 deletions

View file

@ -0,0 +1,9 @@
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char **argv) {
fork();
fork();
fork();
sleep(10);
exit(EXIT_SUCCESS);
}

View file

@ -0,0 +1,33 @@
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/wait.h>
int main (int argc, char **argv) {
pid_t pid;
int status;
pid = fork ();
if ( pid < 0 ){
printf("Error: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
if ( pid == 0){
printf(" Into child, command : %s\n",argv[1]);
// we are in children
int out = execvp(argv[1], &argv[1]);
if ( out == -1 ){
printf("Error : %s\n",strerror(errno));
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
else {
// if child exit code is not 0 then exit father with EXIT_FAILURE
while ((pid = waitpid(pid, &status, 0)) > 0){
(status == 0) ? exit(EXIT_SUCCESS) : exit(EXIT_FAILURE);
};
}
}

View file

@ -0,0 +1,37 @@
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/wait.h>
int main(int argc, char **argv){
if (argc > 2) {
printf("Error : %s must have one argument\n", argv[0]);
exit(EXIT_FAILURE);
}
if (argv[1] == NULL) {
printf("Execute child process\n");
exit(EXIT_SUCCESS);
}
else {
int x = atoi(argv[1]);
printf("Number of children to create : %d\n", x);
for (int i=0; i < x ; i++){
int pid = fork();
if (pid == -1) {
printf("Error : %s", strerror(errno));
exit(EXIT_FAILURE);
}
if (pid == 0) {
// we are in our children
if ((execl(argv[0],"",NULL)) < 0 ) {
printf("Error : %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
}
}
}
exit(EXIT_SUCCESS);
}

View file

@ -0,0 +1,35 @@
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/wait.h>
int main(int argc, char **argv){
if (argv[1] == NULL) {
printf("Error : %s must have one argument\n", argv[0]);
exit(EXIT_FAILURE);
}
printf("Execute programme number %s\n",argv[1]);
int r = atoi(argv[1]) - 1;
if (r > 0){
int pid = fork();
if (pid == -1) {
printf("Error : %s", strerror(errno));
exit(EXIT_FAILURE);
}
if (pid == 0) {
// we are in our children
char args[12] = "";
sprintf(args,"%i", r);
if ((execl(argv[0], argv[0], args, NULL)) < 0 ) {
printf("Error : %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
}
}
else {
printf("This is the end!\n");
}
exit(EXIT_SUCCESS);
}