Bonsoir,

Pouvez vous m'aider à traduire ce code en utilisant les fonctions POSIX (sem_open, sem_unlink, sem_post, sem_wait...) car là je ne comprend pas trop les key, etc..

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
#include <stdio.h>
#include <stdlib.h>
#include "semlib.h"
#include "semaphore.h"
#include "info_proc_user.h"
 
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/wait.h>
#include <signal.h>
 
int msgid_q;
int errno;
int id_shm_question;
int id_shm_reponse_client;
 
void handler(int numero) {
	switch(numero) {
		case SIGINT:
			if (shmctl(id_shm_question, 0, IPC_RMID) == -1) {
				perror("shmctl");
			}
			if (shmctl(id_shm_reponse_client, 0, IPC_RMID) == -1) {
				perror("shmctl");
			}
			if ((semctl(msgid_q, 0, IPC_RMID) == -1) && errno != EIDRM) {
				perror("semc");
			}
			exit(0);
			break;
		default :	
				;
	}			
}
 
int main() {
 
    //pb pour compiler avec sigaction et std=c99
    struct sigaction action_SIGINT,old_action_SIGINT;
    action_SIGINT.sa_handler = handler;
	action_SIGINT.sa_flags = 0;
    struct sigaction action_SIGCHLD,old_action_SIGCHLD;
    action_SIGCHLD.sa_handler = SIG_DFL;
	action_SIGCHLD.sa_flags = SA_NOCLDWAIT; //j'indique que les fils ne doit pas se transformer en zombie
 
	//mise au vide de mask 
	if (sigemptyset(&action_SIGCHLD.sa_mask) == -1) {
		perror("sigempty");
		exit(1);
	}
 
	//nouveaux comportement du processus face à SIGCHLD et SIGINT
	if (sigaction (SIGCHLD, &action_SIGCHLD, &old_action_SIGCHLD) == -1) {
		perror("SIGCHLD");
		exit(2);
	}
	if (sigaction(SIGINT, &action_SIGINT,&old_action_SIGINT) == -1) {
		perror("SIGINT");
		exit(2);
	}
 
	/** creation ensemble semaphore question **/ 
	key_t cle_Semaphore = ftok("/etc", 'a');
 
	int id_semaphore = semget(cle_Semaphore, 3, IPC_CREAT | 0666);
	if (id_semaphore == -1) {
		perror("semget creation question serveur");
		exit(EXIT_FAILURE);
	}
 
	/* initialisation du semaphore QUESTION */ 
	if (semctl(id_semaphore, QUESTION, SETVAL, 0) == -1) {
		perror("smctl sem question");
		exit(EXIT_FAILURE);
	}
 
	/* initialisation du semaphore REPONSE */ 
	if (semctl(id_semaphore, REPONSE, SETVAL, 0) == -1) {
		perror("smctl sem reponse");
		exit(EXIT_FAILURE);
	}
 
	/* initialisation du semaphore TICKET */ 
	if (semctl(id_semaphore, TICKET, SETVAL, 1) == -1) {
		perror("smctl sem ticket");
		exit(EXIT_FAILURE);
	}
 
	//création clé pour le shm des questions
	key_t cle_shm_question;
	cle_shm_question = ftok("/etc",'a'); 
	if (cle_shm_question == -1) {
		perror("erreur création cle_shm_question");
		exit(1);
	}
 
	//création de shm de question
	id_shm_question = shmget(cle_shm_question, sizeof(struct question), IPC_CREAT|0666);
	if (id_shm_question == -1) {
		perror("problème création shm question");
		exit(1);
	}	
 
	//adressage de shm question
	struct question* shm_quest = (struct question*) shmat(id_shm_question,NULL,0);
	if (shm_quest  == (void *) -1) {
		perror("adressage de shm question");
		exit(1);
	}
 
	//traitement des requêtes
    char reponse[TAILLE_MESSAGE];
    char* t;
    struct question quest;
    uid_t user_id;
    pid_t pid_id;
	while(1) {
		P(id_semaphore, QUESTION);
			quest = *shm_quest;
 
		switch (fork()) {
			case -1:
				perror("fork");
				exit(1);
			case 0:
				switch(quest.typequestion) {
					case 1:
						user_id = quest.uid;
						t = info_user(user_id);
						break;
					case 2:
						pid_id = quest.pid;
						t = info_proc(pid_id);
						break;
					default:
						perror("erreur type");
						exit(1);
				}
				strcpy(reponse, t);
 
				//ouverture de shm réponse client
				key_t cle_shm_reponse_client;
				cle_shm_reponse_client = ftok("/etc", quest.mypid);
				id_shm_reponse_client = shmget(cle_shm_reponse_client, sizeof(char) * 1024, 0666);
				if (id_shm_reponse_client == -1) {	
					perror("shm reponse client");
					exit(1);
				}
 
				//adressage de shm reponse client
				char *shm_reponse_client = (char *) shmat(id_shm_reponse_client,NULL,0);
				if (shm_reponse_client  == (void *) -1) {
					perror("adressage de shm reponse serveur client");
					exit(1);
				}
 
				//A SYNCHRONISER
				strcpy(shm_reponse_client,reponse);
				V(id_semaphore, REPONSE);
				exit(0);
			default :
				continue;			
		}				
	}		
}