-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
122 lines (73 loc) · 2.05 KB
/
client.c
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
#include "File.h"
int main (int argc, char *argv[])
{
fifo *fifo_p = NULL;
requete_t req;
struct sigaction action;
pid_t pid = getpid ();
char x[10];
char n[10] = NOM_TUBE;
char tube[10];
char c[128];
action.sa_flags = SA_SIGINFO;
action.sa_sigaction = &sighand;
//Si le server envoi un signal .
if (sigaction (SIGUSR1, &action, NULL) == -1)
{
perror ("sigusr: sigaction");
return 0;
}
clientPrompt ();
/////////////////////// SHM////////////////////////
//ouvrir la mémoire partagée par l'appel système shm_open
int shm_fd = shm_open (NOM_SHM, O_RDWR, S_IRUSR | S_IWUSR);
if (shm_fd == -1)
{
perror ("shm_open");
exit (EXIT_FAILURE);
}
//on projete l'objet de mémoire partagée dans l'espace d'adressage virtuel du processus
char *file = mmap (NULL, TAILLE_SHM, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
if (file == MAP_FAILED)
{
perror ("mmap");
exit (EXIT_FAILURE);
}
fifo_p = (fifo *) file;
if (close (shm_fd) == -1)
{
perror ("close");
exit (EXIT_FAILURE);
}
///////////////////////Remplissage de la requete ////////////////////////
if (argc < 1)
{
printf ("Erreur \n");
exit (EXIT_FAILURE);
}
sprintf (x, "%d", pid);
strcpy (tube, strcat (n, x));
create_tube_client (tube);
while (fgets (c, 128, stdin) != NULL)
{
c[strlen (c) - 1] = '\0';
//si le client veut quitter il peut ecrire quit et le processus vas envoyer un signal SIGTERM C à lui meme
if (strncmp (c, "quit", strlen ("quit")) == 0)
{
//si le client tape la commande quit le processus envoi un signal SIGTERM Ã lui meme
if (kill (pid, SIGTERM) == -1)
{
perror ("Erreur kill");
exit (EXIT_FAILURE);
}
}
strcpy (req.cmd, c);
strcpy (req.nom, tube);
req.pid = pid;
//on ajoute la requete à la file
enfiler (fifo_p, req);
read_write (tube);
clientPrompt ();
}
exit (EXIT_SUCCESS);
}