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
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#define MAXT 16
#ifdef WIN32
#include <windows.h>
#define msleep(ms) Sleep(ms)
#else
/* ajouter l'include qui va bien pour usleep */
#define msleep(ms) usleep((ms) * 1000)
#endif
typedef struct
{
int num;
char categorie[MAXT];
char titre[MAXT];
char description[MAXT];
int duree;
int prix_depart;
int termine;
}
objet;
static void clean (char *s)
{
/* search */
char *p = strchr (s, '\n');
if (p != NULL)
{
/* kill */
*p = 0;
}
else
{
/* purge */
int c;
while ((c = getchar ()) != '\n' && c != EOF)
{
}
}
}
static void prompt (char const *s)
{
printf ("%s > ", s);
fflush (stdout);
}
void *task (void *p_user)
{
if (p_user != NULL)
{
objet *p_data = p_user;
printf("\n");
prompt ("task");
printf ("start %d sec\n", p_data->duree);
prompt ("main");
msleep (p_data->duree * 1000);
p_data->termine = 1;
printf("\n");
prompt ("task");
printf ("end\n");
prompt ("main");
}
return NULL;
}
int main (void)
{
/* mini shell */
int end = 0;
objet data = {0};
pthread_t pt;
while (!end)
{
char command[32];
prompt ("main");
fgets (command, sizeof command, stdin);
clean (command);
if (*command != 0)
{
if (strcmp (command, "help") == 0)
{
puts ("commands : help start status quit");
}
else if (strcmp (command, "start") == 0)
{
data.termine = 0;
data.duree = 10;
pthread_create (&pt, NULL, task, &data);
}
else if (strcmp (command, "status") == 0)
{
printf ("termine = %d\n", data.termine);
}
else if (strcmp (command, "quit") == 0)
{
end = 1;
}
else
{
puts ("unknown command");
}
}
}
pthread_join (pt, NULL);
return 0;
} |
Partager