Commit f11b5e64 authored by KESSLER Erwan's avatar KESSLER Erwan 😲
Browse files

TP1

parent c4aabd0d
cmake_minimum_required(VERSION 3.14)
project(TD1)
set(CMAKE_C_FLAGS "-Wall -Wformat")
set(CMAKE_C_STANDARD 99)
add_executable(TD1 main.c)
add_executable(multimodo multimodo.c)
add_executable(signal signal.c)
\ No newline at end of file
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <wait.h>
#include <stdarg.h>
#include <string.h>
#include <signal.h>
void one() {
for (int i = 0; i < 10; i++) {
if (fork() == 0) {
for (int j = 0; j < 10; j++)printf("%d", i);
exit(0);
}
wait(NULL);
}
}
void helperPidPpid() {
printf("pid: %d ppid: %d\n", getpid(), getppid());
}
void two(int n) {
helperPidPpid();
for (int i = 0; i < n; ++i) {
pid_t pid = fork();
if (pid < 0) {
fprintf(stderr, "Process creation failed\n");
exit(-1);
}
if (pid == 0) {
helperPidPpid();
exit(0);
}
}
}
void twoBis(int n) {
helperPidPpid();
for (int i = 0; i < n; ++i) {
pid_t pid = fork();
if (pid < 0) {
fprintf(stderr, "Process creation failed\n");
exit(-1);
}
if (pid > 0) {
exit(0);
}
helperPidPpid();
}
}
void three(char *name, const char *fmt, ...) {
char **argvs = calloc(1, sizeof(char *));
int i = 0;
va_list args;
va_start(args, fmt);
while (*fmt != '\0') {
if (*fmt == 's') {
char *c = va_arg(args, char*);
argvs[i++] = strdup(c);
argvs = realloc(argvs, sizeof(char *) * (i + 1));
}
++fmt;
}
va_end(args);
argvs[i] = NULL;
if (fork() == 0) {
execvp(name, argvs);
exit(0);
}
wait(NULL);
printf("Done\n");
for (int j = 0; j < i; j++) {
free(argvs[i]);
}
free(argvs);
}
void four(const char *fmt, ...) {
char **argvs = calloc(2, sizeof(char *));
argvs[0] = "multimodo";
printf("Launching : multimodo");
int i = 1;
va_list args;
va_start(args, fmt);
while (*fmt != '\0') {
if (*fmt == 's') {
char *c = va_arg(args, char*);
argvs[i++] = strdup(c);
printf(" %s", c);
argvs = realloc(argvs, sizeof(char *) * (i + 1));
}
++fmt;
}
va_end(args);
argvs[i] = NULL;
printf("\n");
if (fork() == 0) {
execvp("multimodo", argvs);
exit(0);
}
}
void handler(int sig) { /* nouveau gestionnaire */
printf("signal SIGKILL recu !\n");
exit(0);
}
void five() {
if (fork()==0){
struct sigaction nvt, old;
memset(&nvt, 0, sizeof(nvt));
nvt.sa_handler = &handler;
sigaction(SIGINT, &nvt, &old);
pause();
for (int i = 0; i < 4; ++i) {
sigaction(SIGINT, &old, &old);
pause();
}
}
}
int main() {
printf("Question 1\n");
one();
printf("Question 2\n");
two(5);
printf("Question 2 Bis\n");
twoBis(5);
printf("Question 3\n");
three("ls", "sss", "ls", "-lt", "/");
printf("Question 4\n");
four("ss", "mkdir", "toto");
printf("Question 5\n");
five();
return 0;
}
\ No newline at end of file
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <wait.h>
#include <string.h>
int main(int argc, char * argv[]){
if (argc<2){
printf("Error, please give at least an argument\n");
exit(-1);
}
int status;
for (int i=0;i<5;i++){
pid_t pid =fork();
if (pid <0){
fprintf(stderr,"Error while creating the process\n");
exit(-1);
}
if (pid==0){
execvp(argv[1],argv+1);
fprintf(stderr,"The command was not found");
exit( -1);
}
else{
while (waitpid(pid,&status,WCONTINUED)>0){
if (WIFEXITED(status)){
if (WEXITSTATUS(status)!=0){
printf("The command exited with %d\n",status);
goto end;
}
else{
printf("The command was successful\n");
}
}
}
}
}
end:;
}
\ No newline at end of file
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
void handler(int sig) { /* nouveau gestionnaire */
printf("signal SIGINT re¸cu !\n");
}
int main() {
struct sigaction nvt;
memset(&nvt, 0, sizeof(nvt));
nvt.sa_handler = &handler;
for (int i = 0; i < 5; ++i) {
sigaction(SIGINT, &nvt, NULL);
pause();
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment