Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/wait.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/sem.h>
- #include <sys/stat.h>
- #define SEM_ACCESS 0
- #define SEM_EMPTY 1
- #define SEM_FILLED 2
- #define SIZE 3
- #define OP_DOWN -1
- #define OP_UP 1
- #define nb_prod 4
- #define nb_cons 4
- void die(const char *s)
- {
- perror(s);
- exit(EXIT_FAILURE);
- }
- void produce(int semid)
- {
- struct sembuf operations[1];
- operations[0].sem_flg=0;
- operations[0].sem_num=SEM_EMPTY;
- operations[0].sem_op=OP_DOWN;
- if (semop(semid,operations,1) == -1)
- {
- perror("MAIN.c op_up a foiré dans sem_access");
- return EXIT_FAILURE;
- }
- operations[0].sem_num=SEM_ACCESS;
- operations[0].sem_op=OP_DOWN;
- if (semop(semid,operations,1) == -1)
- {
- perror("MAIN.c op_up a foiré dans sem_access");
- return EXIT_FAILURE;
- }
- struct storage {
- int pos_prod, pos_cons;
- int store[SIZE];
- };
- struct storage storage;
- FILE* stream=fopen("entrepot.txt","r");
- fscanf(stream, "%d %d", &storage.pos_prod, &storage.pos_cons);
- int loop_i;
- for(loop_i = 0; loop_i < SIZE; loop_i++)
- {
- fscanf(stream, "%d", &storage.store[loop_i]);
- }
- fclose(stream);
- storage.store[storage.pos_prod] = getpid();
- storage.pos_prod++;
- stream=fopen("entrepot.txt","w");
- if(stream == NULL)
- {
- die("produce:fopen");
- }
- fprintf(stream, "%d %d\n", storage.pos_prod, storage.pos_cons);
- for(loop_i=0;loop_i<SIZE;loop_i++)
- {
- fprintf(stream, "%d ",storage.store[loop_i]);
- }
- operations[0].sem_num=SEM_ACCESS;
- operations[0].sem_op=OP_UP;
- if (semop(semid,operations,1) == -1)
- {
- perror("MAIN.c op_up a foiré dans sem_access");
- return EXIT_FAILURE;
- }
- operations[0].sem_num=SEM_FILLED;
- operations[0].sem_op=OP_UP;
- if (semop(semid,operations,1) == -1)
- {
- perror("MAIN.c op_up a foiré dans sem_filled");
- return EXIT_FAILURE;
- }
- }
- void consume(int semid)
- {
- }
- int main() {
- // synchronisation
- int semid = 0;
- if((semid=semget(IPC_PRIVATE,3 /*nb de sempahore*/ ,IPC_CREAT
- |S_IRUSR/*user read*/
- |S_IWUSR/*user write*/))==-1)
- {
- die("ERROR creation semaphore");
- }
- if(semctl(semid,SEM_ACCESS,SETVAL,1) == -1)
- {
- die("ERROR control semaphore access");
- } //binaire ou mutux ---> un seul processus
- if(semctl(semid,SEM_EMPTY,SETVAL,SIZE) == -1)
- {
- die("ERROR control semaphore empty");
- }
- if(semctl(semid,SEM_FILLED,SETVAL,0) == -1)
- {
- die("ERROR control semaphore filled");
- }
- FILE* stream=fopen("entrepot.txt","w+");
- fprintf(stream, "0 0\n");
- int loop_i = 0;
- for(loop_i = 0; loop_i < SIZE; loop_i++)
- {
- fprintf(stream, "0 ");
- }
- // Processus management
- char father = 1;
- pid_t PIDs[nb_cons + nb_prod];
- for(loop_i = 0; loop_i < nb_cons; loop_i++)
- {
- PIDs[loop_i] = fork();
- if(PIDs[loop_i] == -1 )
- {
- die("ERROR Creation");
- }
- if(PIDs[loop_i] == 0)
- {
- father = 0;
- consume(semid);
- break;
- }
- }
- if(father)
- {
- for(loop_i = 0; loop_i < nb_cons; loop_i++)
- {
- PIDs[loop_i] = fork();
- if(PIDs[loop_i] == -1 )
- {
- die("ERROR Creation processus");
- }
- if(PIDs[loop_i] == 0)
- {
- father = 0;
- produce(semid);
- break;
- }
- }
- if(father)
- {
- for(loop_i = 0; loop_i < nb_cons; loop_i++)
- {
- if(waitpid(PIDs[loop_i], NULL, 0) == -1)
- {
- die("ERROR attente processus");
- }
- }
- if (semctl(semid, SEM_ACCESS, IPC_RMID) == -1)
- {
- die("ERROR semaphore détruite");
- }
- }
- }
- return EXIT_SUCCESS;
- }
Add Comment
Please, Sign In to add comment