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
| #include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <sys/socket.h>
#include "common.h"
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#define closesocket(s) close (s)
#define PROCESS 3
#define SLEEP 5
#define PORT 1337
typedef int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;
int process = 0;
int sv[2];
int threadProd(int num){
int period=100; /* en millisecondes */
char type_objet[3];
char* trame = malloc(sizeof(char*));
Objet* info = malloc(sizeof(struct Objet));
char* num2;
sprintf(type_objet,"t%d\0",num);
sprintf(info->identificateur,"%s",type_objet);
sprintf(info->objet_content,"25\0");
sprintf(trame,"%s%s",info->identificateur,info->objet_content);
if(write(sv[1], &trame, 5) <0)
printf("error on writing in socket\n");
printf("child: sent '%s'\n", trame);
free(info);
free(trame);
return 0;
}
int threadMACProd(){
char* buffer[PROCESS];
int r;
for(int i=0;i<PROCESS;i++)
buffer[i]=malloc(sizeof(char*));
for(int i=0;i<PROCESS;i++){
read(sv[0], buffer[i], 5);
printf("parent: read '%c'\n", buffer[i]);
}
return 0;
}
int main()
{
pid_t pid;
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == -1) {
perror("socketpair");
exit(1);
}
while (process < PROCESS) {
pid = fork();
// Erreur à la creation du fils
if (pid < 0) {
perror("fork");
exit(EXIT_FAILURE);
}
// Execution du Producteur
else if (!pid) {
pid = getpid();
threadProd(process);
return 0;
}
++process;
}
for(int i=0;i<PROCESS;i++){
threadMACProd();
}
return EXIT_SUCCESS;
} |
Partager