septembre + octobre

This commit is contained in:
2023-10-12 16:39:49 +02:00
commit 06bf5f9488
389 changed files with 4233 additions and 0 deletions

45
SCR/TP03/copy1byte.c Normal file
View File

@@ -0,0 +1,45 @@
#include <sys/types.h>
#include <unistd.h>
#include<stdio.h>
#include <stdlib.h>
#include<assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#define msg1 "je suis le pere"
#define msg2 "je suis le fils !!!"
int main(int argc,char * argv[]){
int infd,outfd;
ssize_t nbread;
char buf[1];
pid_t p;
if (argc != 3){
printf("%s infile outfile\n",argv[0]);
exit(1);
}
infd = open(argv[1],O_RDONLY);
assert(infd >= 0);
outfd = open(argv[2],O_WRONLY|O_CREAT|O_TRUNC,0644);
assert(outfd >= 0);
p=fork(); // <- decommentez cette ligne
while(1){
nbread=read(infd,buf,sizeof(buf));
if (nbread <=0 ) break;
write(outfd,buf,sizeof(buf));
}
close(infd);
close(outfd);
}

45
SCR/TP03/copy1byte.c~ Normal file
View File

@@ -0,0 +1,45 @@
#include <sys/types.h>
#include <unistd.h>
#include<stdio.h>
#include <stdlib.h>
#include<assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#define msg1 "je suis le pere"
#define msg2 "je suis le fils !!!"
int main(int argc,char * argv[]){
int infd,outfd;
ssize_t nbread;
char buf[1];
pid_t p;
if (argc != 3){
printf("%s infile outfile\n",argv[0]);
exit(1);
}
infd = open(argv[1],O_RDONLY);
assert(infd >= 0);
outfd = open(argv[2],O_WRONLY|O_CREAT|O_TRUNC,0644);
assert(outfd >= 0);
//p=fork(); // <- decommentez cette ligne
while(1){
nbread=read(infd,buf,sizeof(buf));
if (nbread <=0 ) break;
write(outfd,buf,sizeof(buf));
}
close(infd);
close(outfd);
}

12
SCR/TP03/ex1-stdio.c Normal file
View File

@@ -0,0 +1,12 @@
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main(){
printf("NON");
if (fork()) {
printf("OUI\n");
}
}

14
SCR/TP03/ex1-syscall.c Normal file
View File

@@ -0,0 +1,14 @@
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main(){
//printf("NON");
write(STDOUT_FILENO,"NON",3);
if (fork()) {
//printf("OUI\n");
write(STDOUT_FILENO,"OUI\n",4);
}
}

5
SCR/TP03/ex2.txt Normal file
View File

@@ -0,0 +1,5 @@
je suis alexis et je suis vraiment crazy !!! (:
je suis alexis et je suis vraiment crazy !!! (:
je suis alexis et je suis vraiment crazy !!! (:
je suis alexis et je suis vraiment crazy !!! (:
je suis alexis et je suis vraiment crazy !!! (:

1
SCR/TP03/ex2.txt~ Normal file
View File

@@ -0,0 +1 @@
je suis alexis et je suis vraiment crazy !!! (:

5
SCR/TP03/ex3.txt Normal file
View File

@@ -0,0 +1,5 @@
j esuis alexis et je suis vraiment crazy !!! (:
je suis alexis t jee suis vraiment crazy !!! (:
je uiss alexis et je suis vraiment crazy !!! (:
je suis alexis et je suis vraiment crazy !!! (:
je suis alexis et je suis vraiment crazy !!! (:

22
SCR/TP03/ex4.c Normal file
View File

@@ -0,0 +1,22 @@
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(){
int pid = fork();
if (pid == -1) {
exit(0);
}
if (pid == 0) {
printf("fork = %d, pid = %d, ppid = %d\n",pid,getpid(),getppid());
sleep(4);
exit(2);
}
if (pid > 0) {
printf("fork = %d, pid = %d, ppid = %d\n",pid,getpid(),getppid());
printf("%d\n",wait());
exit(2);
}
}

12
SCR/TP03/ex4.c~ Normal file
View File

@@ -0,0 +1,12 @@
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main(){
printf("NON");
if (fork()) {
printf("OUI\n");
}
}

124
SCR/TP03/ex5.c Normal file
View File

@@ -0,0 +1,124 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#define SIZE 1000
int search0(const unsigned char * t,int start,int end)
{
int i;
for (i=start; i<end; i++){
if (t[i]==0){
return 1;
}
}
return 0;
}
int search1(const unsigned char * t,int start,int end)
{
int milieu = (end-start)/2;
int pid = fork();
int i;
int status;
if (pid < 0){
return 0;
}
if (pid == 0){
exit(search0(t,start,milieu+1));
}
if (pid > 0){
if (search0(t,milieu+1,end)){
return 1;
}
wait(&status);
return WEXITSTATUS(status);
}
return 0;
}
int search2(const unsigned char * t,int start,int end)
{
int i;
int nbFils;
int taille = (end-start);
int pid;
int status;
printf("Enter a number between 0 and 100: ", SIZE);
scanf(" %d", &nbFils);
for (int i=0; i<nbFils; i++){
pid = fork();
if (pid < 0){
exit(0);
}
if (pid == 0){
exit(search0(t, i*taille/nbFils, (i+1)*taille/nbFils));
}
}
for (int i=0; i<nbFils; i++){
wait(&status);
if (WEXITSTATUS(status) == 1){
return 1;
}
}
return 0;
}
int search3(const unsigned char * t,int start,int end)
{
int i;
int nbFils;
int taille = (end-start);
int pid;
int status;
printf("Enter a number between 0 and 100: ", SIZE);
scanf(" %d", &nbFils);
int* child_pids = (int*) malloc(sizeof(int) * nbFils);
for (int i=0; i<nbFils; i++){
pid = fork();
if (pid < 0){
exit(0);
}
if (pid == 0){
exit(search0(t, i*taille/nbFils, (i+1)*taille/nbFils));
}
child_pids[i] = pid;
}
for (int i=0; i<nbFils; i++){
wait(&status);
if (WEXITSTATUS(status) == 1){
for (int j = 0; j < nbFils; j++) {
kill(child_pids[j], SIGTERM);
}
return 1;
}
}
return 0;
}
int main(int argc , char * argv[])
{
unsigned char arr[SIZE];
int i;
srandom(time(NULL));
for (i = 0; i < SIZE; i++)
arr[i] = (unsigned char) (random() % 255) + 1;
printf("Enter a number between 0 and %d: ", SIZE);
scanf(" %d", &i);
if (i >= 0 && i < SIZE) arr[i] = 0;
if (search3(arr,0,SIZE-1))
printf("Found !\n");
else
printf("Not found !\n");
return EXIT_SUCCESS;
}

36
SCR/TP03/ex5.c~ Normal file
View File

@@ -0,0 +1,36 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define SIZE 1000
int search0(const unsigned char * t,int start,int end)
{
int i;
for (i=start; i<end; i++){
if (t[i]=="0"){
return 1;
}
}
return 0;
}
int main(int argc , char * argv[])
{
unsigned char arr[SIZE];
int i;
srandom(time(NULL));
for (i = 0; i < SIZE; i++)
arr[i] = (unsigned char) (random() % 255) + 1;
printf("Enter a number between 0 and %d: ", SIZE);
scanf(" %d", &i);
if (i >= 0 && i < SIZE) arr[i] = 0;
if (search0(arr,0,SIZE-1))
printf("Found !\n");
else
printf("Not found !\n");
return EXIT_SUCCESS;
}

BIN
SCR/TP03/exe Executable file

Binary file not shown.

42
SCR/TP03/fork_and_fd1.c Normal file
View File

@@ -0,0 +1,42 @@
#include <sys/types.h>
#include <unistd.h>
#include<stdio.h>
#include <stdlib.h>
#include<assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#define msg1 "je suis le pere"
#define msg2 "je suis le fils !!!"
int main(int argc,char * argv[]){
int outfd;
pid_t p;
if (argc != 2){
printf("%s file\n",argv[0]);
exit(1);
}
//p=fork();
outfd = open(argv[1],O_WRONLY|O_CREAT,0644);
assert(outfd >= 0);
p=fork();
switch(p){
case (pid_t)-1 :
perror(NULL);
exit(2);
case (pid_t)0 :
write(outfd,msg2,strlen(msg2));
break;
default :
write (outfd,msg1,strlen(msg1));
break;
}
close(outfd);
}

42
SCR/TP03/fork_and_fd2.c Normal file
View File

@@ -0,0 +1,42 @@
#include <sys/types.h>
#include <unistd.h>
#include<stdio.h>
#include <stdlib.h>
#include<assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#define msg1 "je suis le pere"
#define msg2 "je suis le fils !!!"
int main(int argc,char * argv[]){
int outfd;
pid_t p;
if (argc != 2){
printf("%s file\n",argv[0]);
exit(1);
}
p=fork();
outfd = open(argv[1],O_WRONLY|O_CREAT,0644);
assert(outfd >= 0);
//p=fork();
switch(p){
case (pid_t)-1 :
perror(NULL);
exit(2);
case (pid_t)0 :
write(outfd,msg2,strlen(msg2));
break;
default :
write (outfd,msg1,strlen(msg1));
break;
}
close(outfd);
}

10
SCR/TP03/reponse.txt Normal file
View File

@@ -0,0 +1,10 @@
1)
le 1er non est écrit dans un cache. ensuite le programme se divise, et le non apparait donc 2 fois
2)
Dans le premier cas, un seul processus ouvre le fichier.
je suis le pere !!!je suis le fils !!!
Dans le deuxième cas le fichier est ouvert et modifier 2 fois simultanément.
je suis le pere !!!uis le fils !!!
3)

1
SCR/TP03/reponse.txt~ Normal file
View File

@@ -0,0 +1 @@
1)

18
SCR/TP03/session.c Normal file
View File

@@ -0,0 +1,18 @@
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
if (fork()){
printf("session = %d\n",getsid(getpid()));
while(1);
} else {
setsid();
printf("session = %d\n",getsid(getpid()));
while(1);
}
return 0;
}