1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define TAILLE_LIGNE 1024
#define NB_LIGNES 60
int cps = 0;
// lit x lignes et les place dans le buffer
int read_x_lines(FILE *file, char **buffer, int x)
{
int nb_lignes_lues = 0;
int i=0;
while((fgets(buffer[i],TAILLE_LIGNE,file))!= NULL && i<x)
{
nb_lignes_lues++;
i++;
}
/*for(i=0;i<x;i++)
printf(" %s \n",buffer[i]);*/
return nb_lignes_lues;
}
void sing(char *text, char *filename)
{
int fd;
if((fd=open(filename, O_WRONLY| O_APPEND)) == -1)
{
perror(" fopen ");
exit(1);
}
if(write(fd,text,strlen(text)) == -1)
{
perror(" write ");
exit(1);
}
close(fd);
}
int check_if_it_is_my_turn()
{
FILE *fp = fopen ("toto.txt", "r");
char spid[4];
int pid_toto;
if (fp == NULL)
{
printf( " Fils %d : le fichier toto.txt n'existe pas. Je me rendors\n",getpid());
return 0;
}
else
{
if((fgets(spid,sizeof(int),fp))!= NULL)
{
perror( "fopen ");
exit(1);
}
pid_toto = atoi(spid);
if(getpid() == pid_toto)
{
fclose(fp);
return 1;
}
else
{
fclose(fp);
return 0;
}
}
}
void fils(char *text)
{
int tour = check_if_it_is_my_turn();
if(tour == 1)
{
printf(" %d : %s",getpid(),text);
sing(text,"Lechanteur.txt");
}
else
{
sleep(1);
}
}
// crée un fils en lui transmettant une chaine de caractère qu'il devra afficher et écrire dans le fichier "filename"
int create_new_child(char *text, char *filename)
{
pid_t fils;
fils = fork();
switch(fils)
{
case -1 :
{
perror(" fork ");
exit(1);
}
case 0 :
{
fils(text);
}
default :
{
int desc;
if(desc = open("toto.txt", O_WRONLY | O_CREAT | O_APPEND) == -1)
{
perror(" open" );
exit(1);
}
/* if(write(desc,&fils,sizeof(int)) == -1)
{
perror(" write ");
exit(1);
}*/
//wait((int*)0);
return fils;
}
}
}
int *read_file_and_create_childs(char *srcname, char *dstname, int x, int *nbprocs)
{
int i = 0;
int nbll;
char **buffer;
buffer = (char **)malloc(NB_LIGNES*sizeof(char));
for (i=0; i<NB_LIGNES; i++)
{
buffer[i] = (char*)malloc( sizeof(char) * TAILLE_LIGNE);
}
FILE *file = fopen(srcname,"r");
//nbll = read_x_lines(file, buffer, x);
if (file == NULL)
{
perror(" Fopen ");
exit(1);
}
nbll = read_x_lines(file, buffer, x);
nbprocs[cps] = create_new_child(buffer[cps], dstname);
cps++;
fclose(file);
return nbprocs;
}
int main(int argc, char **argv) // argv[1] = n, argv[2] = fichier_source, argv[3] = fichier_destination
{
pid_t pid;
int n = atoi(argv[1]);
int *PID;
PID = (int *) malloc(n*sizeof(int *));
int i;
for(i=0;i<n;i++) // temporairement n
PID = read_file_and_create_childs(argv[2], argv[3], n, PID);
for(i=0;i<n;i++)
printf(" pid = %d taille = %d\n",PID[i],strlen(PID));
exit(0);
} |
Partager