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
|
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
/* http://delahaye.emmanuel.free.fr/clib/ */
#include "psleep/inc/psleep.h"
#define F "donnees.txt"
static void *task_a (void *user)
{
int count = 0;
while (1)
{
FILE *fp = fopen (F, "w");
if (fp != NULL)
{
printf ("W: count = %d\n", count);
fprintf (fp, "%d\n", count);
fclose (fp);
count++;
}
else
{
printf ("W: '%s' inaccessible\n", F);
}
msleep (1000);
}
return NULL;
}
static void *task_b (void *user)
{
while (1)
{
FILE *fp = fopen (F, "r");
if (fp != NULL)
{
int count;
int n = fscanf (fp, "%d", &count);
fclose (fp);
if (n == 1)
{
printf ("R: count = %d\n", count);
}
}
else
{
printf ("R: '%s' inconnu ou inaccessible\n", F);
}
msleep (1100);
}
return NULL;
}
int main (void)
{
pthread_t ta;
pthread_t tb;
pthread_create (&ta, NULL, task_a, NULL);
pthread_create (&tb, NULL, task_b, NULL);
pthread_join (ta, NULL);
pthread_join (tb, NULL);
return 0;
} |
Partager