Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<sys/mman.h>
- #include<sys/stat.h>
- #include<fcntl.h>
- #include<unistd.h>
- #include<sys/types.h>
- #include <sys/stat.h>
- #include <sys/wait.h>
- #include <time.h>
- #include <math.h>
- #include <string.h>
- #include <mqueue.h>
- #include <semaphore.h>
- #define SEM "/sem"
- #define SEM2 "/sem2"
- typedef struct {
- int valor;
- char aviso[80];
- } Mensaje;
- int main(int argc, char** argv) {
- struct mq_attr attributes = {
- .mq_flags = 0,
- .mq_maxmsg = 10,
- .mq_curmsgs = 0,
- .mq_msgsize = sizeof(Mensaje)
- };
- pid_t pid = 1;
- int i, id = 5;
- char nombreCola1[80];
- char nombreCola2[80];
- char nombreFich[80];
- sem_t *sem, *sem2;
- int sval;
- strcpy(nombreCola1, argv[1]);
- strcpy(nombreCola2, argv[2]);
- strcpy(nombreFich, argv[3]);
- if ((sem = sem_open(SEM, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, 0)) == SEM_FAILED) {
- perror("sem_open");
- exit(EXIT_FAILURE);
- }
- if ((sem2 = sem_open(SEM2, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, 0)) == SEM_FAILED) {
- perror("sem_open");
- exit(EXIT_FAILURE);
- }
- mqd_t queue = mq_open(nombreCola1,O_CREAT | O_RDONLY,S_IRUSR | S_IWUSR,&attributes);
- if(queue == (mqd_t)-1)
- {
- fprintf (stderr, "Error opening the queue\n");
- return EXIT_FAILURE;
- }
- mqd_t queue2 = mq_open(nombreCola2,O_CREAT | O_RDONLY,S_IRUSR | S_IWUSR,&attributes);
- if(queue == (mqd_t)-1)
- {
- fprintf (stderr, "Error opening the queue\n");
- return EXIT_FAILURE;
- }
- for (i = 0; i < 3; i++) {/*bucle que crea N_READ hijos*/
- if (pid) {
- pid = fork();
- if (pid == 0)
- id = i;
- }
- }
- if(pid == 0 ) {
- if(id == 0){
- execl("./ejA","./ejA",argv[3],argv[1],NULL);
- sem_getvalue(sem, &sval);
- exit(EXIT_SUCCESS);
- }
- if(id == 1){
- sem_wait(sem);
- execl("./ejB","./ejB",argv[1],argv[2],NULL);
- exit(EXIT_SUCCESS);
- }
- if(id == 2){
- sem_wait(sem2);
- execl("./ejC","./ejC",argv[2],NULL);
- exit(EXIT_SUCCESS);
- }
- }
- if(pid != 0 && id == 5) {
- for(i=0;i<3;i++)
- wait(NULL);
- mq_close(queue);
- mq_unlink(argv[1]);
- mq_close(queue2);
- mq_unlink(argv[2]);
- sem_close(sem);
- sem_unlink(SEM);
- sem_close(sem2);
- sem_unlink(SEM2);
- exit(EXIT_SUCCESS);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement