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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
| #include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/wait.h>
#include "ccrt.h"
int main (){
key_t cle;
pid_t f;
int num_f, nb_f, i, status;
int id_sem;
struct sembuf op_v= {0, +1, SEM_UNDO};
struct sembuf op_p= {0, -1, SEM_UNDO};
ushort init_sem[] = {1};
//génération de la clé...pour le semaphore
if ( (cle = ftok (".", 'a')) == -1){
perror ("ftok");
return -1;
}
//création d'un sémaphores...
if ( (id_sem = semget (cle, 1 ,IPC_CREAT | IPC_EXCL | 0666)) == -1 ){
per??? ????????????????????? ???????????????????????????? ?? ???????????? ????? ??????? ???????? ?? ??????? ????????????????????????? ????????????????????? ??????????? ?????????????????????????????????????????????????????????????????????????????????????? ?? ?? ?????????? ?? ??????? ????????????? ? ????? ? ???? ????? ????? ?? ?????????????? ??????????????????? ???????????????????????? ?? ??????? ?? ?? ??????? ???????????????????? ? ?????? ????? ??????? ?????????????????????????????? ??????? ?? ?????????????? ????????????????????? ????????????????? ?????????????? ? ??????????? ?????????? ????????????? ????????? ??????????????????? ????????????????????????????????????????????????????? ??????????????????????????????????? ?????????????????????????????????????? ?? ??????? ?? ???? ??? ???? ??? ???????????????????????????????????? ???????? ?????????? ???? ?? ?????? ???? ?????????????????????????????????????????????????????????????????????????????????? ???????? ?? ?????? ?? ??????? ????????????? ?????? ???????? getchar(); // pour éviter le problème d'entréé...
if (nb_f<2){
printf ("Vous devez donner un nombre superieur à 1...\n\n");
goto a;
}
int poche[100];
for(i=1;i<=nb_f;i++)
{
printf("Introduire le solde du client (%d) \t\tDA\b\b\b\b\b\b\b\b\b\b",i);
scanf("%d",&poche[i]);
}
int tmax;
printf("Introduire le temp max :");
scanf("%d",&tmax);
time(&t0);
ClrScr();
num_f = 1; // le numéro du premier client...
for (i=0; i<nb_f; i++){
srand (time(NULL));//initialisation de la génération des nombres aléatoires
f = fork();// créer un client..
switch (f){
case -1:{
perror ("fork()");
return -1;
}
case 0:{ // les client...
while (1){
sleep ((rand()%4)); // temps aléatoire entre deux prposietions
for(i=1;i<=nb_f;i++){
GotoXY (3,2*i);
TextColor(BLACK);
printf("le client(%d)",i);
}
time(&t1);
if(num_f!=val[1] && t1-t0<=tmax && poche[num_f]-val[0]>0)
? ??????????????????? ?????????????? ?????? ?? ?? ?? ????????????????? ??????? ? ??????????????????? ???????????????????????????????????????????????????????????????????????????????????????????? ????????????? ???????????? ?????????? ??????????? ?????? ?????????? ?????????? ????? ??????? ????? ????????????????? ???????? ????? ???? ???????? ??? ?????? ?????????? ?????????????????? ?????????? ???????? ????? ? GotoXY (38,12);
TextColor(RED);
printf(" le temps est terminé !!!");
}
for(i=1;i<=nb_f;i++){
if(i!=num_f){
GotoXY (3,2*i);
TextColor(BLACK);
printf("Le client(%d)",i);
}
if(i==num_f){
r=(rand()%4);
TextColor(BLUE);
GotoXY (3,2*i);
printf("---------->");
? ????????????????? ?????? ????????????? ?? ???? ?? ?????????? ??????????? ???????? ?? ??????????? ?? ???? ?? ?????????? ??????????? ?? ??????? ?? ?? ?? ???? ????????? ?? ???? ?? ?????????? ??????????? ?? ????? ?? ?????????????????? ?? ???? ?? ?????????? ??????????? ?? ?? ???????????????? ? ?? ????? ?????? ?????????? ?????????????????? ?????? ?????????????????? ?????? ????????? ????????????????? ?????? ???? ????????????????? ????????????????;
GotoXY (63,6);
printf ("///////////");
val[1]=num_f;
val[0]=val[0]+m;}
printf("\n\n");
if (semop (id_sem, &op_v, 1) == -1 ){
perror ("semop - v");
return -1;
}
}
// pour casser la boucle while(1)
time(&t1);
if(t1-t0>tmax)
break;
}
return 0;
}// fin zone des clients...
}
num_f++; // la prochain numéro de client..
sleep (1); // attendre une seconde avant la prochaine génération des clients...
}
// le père...
for (i=0; i<nb_f; i++)// le point de rdv avec le pere il faut execute nb_f fois pour rendre la main au père
wait (&status);
if (semctl (id_sem, 0, IPC_RMID, 0) == -1){ // la libération du sémaphore...
perror ("semctl");
return -1;
}
if(val[1]>0)
{
ClrScr();
**********************************************************
et le fichier .h le voila :
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
typedef enum
{
BLACK,
RED,
GREEN,
YELLOW,
BLUE,
MAGENTA,
CYAN,
WHITE
} COLORS;
void mode_raw(int activer) {
static struct termios cooked;
static int raw_actif = 0;
if (raw_actif == activer) {return;}
if (activer) {
struct termios raw;
tcgetattr(STDIN_FILENO, &cooked);
raw = cooked;
cfmakeraw(&raw);
tcsetattr(STDIN_FILENO, TCSANOW, &raw);
}
else {tcsetattr(STDIN_FILENO, TCSANOW, &cooked);}
raw_actif = activer;
}
void ClrScr(void) {printf("\e[2J");}
void GotoXY(int y, int x) {printf("\e[%d;%dH", x, y);}
void Delay(int pause) {usleep(pause*1000);}
void Blink(int activer) {(activer)? printf("\e[5m") : printf("\e[0m");}
void Blod(int activer) {(activer)? printf("\e[1m") : printf("\e[0m");}
void Underline(int activer) {(activer)? printf("\e[4m") : printf("\e[0m");}
void TextColor(int fg)
{
int x=fg;
if((fg < 0) || (fg > 7)) x = 0;
printf("\e[3%dm", x);
}
void TextBackground(int bg)
{
int x=bg;
if((bg < 0) || (bg > 7)) x = 0;
printf("\e[4%dm", x);
}
char ReadKey(void)
{
char c;
mode_raw(1);
c = getchar();
mode_raw(0);
return c;
}
int KeyPressed(void)
{
struct timeval tv = { 0, 0 };
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(STDIN_FILENO, &readfds);
return select(STDIN_FILENO + 1, &readfds, NULL, NULL, &tv) == 1;
}
void GetPass(char * prompt, char * pass)
{
int cpt=0;
printf("%s", prompt);
while((pass[cpt]=ReadKey())!=13 && cpt!=100)
{
putchar('*');
cpt++;
}
printf("\n");
pass[cpt]=0;
}
void line(int x, char c1, char c2)
{
int cpt;
putchar(c1);
for(cpt=1; cpt<=(x-2); cpt++) putchar(c2);
putchar(c1);
putchar('\n');
}
void box(int x, int y, char c1, char c2, char c3, char c4)
{
int cpt;
line(x, c1, c2);
for(cpt=1; cpt<=(y-2); cpt++) line(x, c3, c4);
line(x, c1, c2);
} |
Partager