Jump to content

williampmb

Members
  • Content count

    1
  • Joined

  • Last visited

Community Reputation

0 Comum

About williampmb

  1. williampmb

    Comunicacao entre Processos

    Ola, Preciso fazer um programa que consiga fazer comunicacao entre dois processos utilizando pipes com chamadas de sistemas. Estou usando semaforo para tal. Tudo ia muito bem ate que nao consigo entender porque o que escrevo no pipe pela segunda vez eh perdido no loop while. Pf, me deem uma luz. #include <stdio.h> /* printf() */ #include <stdlib.h> /* exit(), malloc(), free() */ #include <sys/types.h> /* key_t, sem_t, pid_t */ #include <sys/shm.h> /* shmat(), IPC_RMID */ #include <errno.h> /* errno, ECHILD */ #include <semaphore.h> /* sem_open(), sem_destroy(), sem_wait().. */ #include <fcntl.h> /* O_CREAT, O_EXEC */ int main (int argc, char **argv){ int i; /* loop variables */ key_t shmkey; /* shared memory key */ int shmid; /* shared memory id */ sem_t *sem, *semM,*semJ; /* synch semaphore *//*shared */ pid_t pid; /* fork pid */ pid_t joao; /* fork pid */ pid_t maria; /* fork pid */ int *p; /* shared variable *//*shared */ unsigned int n; /* fork count */ unsigned int value; /* semaphore value */ char buffer[255]; FILE *file1; FILE *file2; /* initialize a shared variable in shared memory */ shmkey = ftok ("/dev/null", 5); /* valid directory name and a number */ printf ("shmkey for p = %d\n", shmkey); shmid = shmget (shmkey, sizeof (int), 0644 | IPC_CREAT); if (shmid < 0){ /* shared memory error check */ perror ("shmget\n"); exit (1); } p = (int *) shmat(shmid, NULL, 0); /* attach p to shared memory */ *p = 0; printf("p=%d is allocated in shared memory.\n\n", *p); /********************************************************/ printf("What do you want the semaphore value to be?\n"); printf("Semaphore value: "); scanf("%u", &value); /* initialize semaphores for shared processes */ sem = sem_open("pSem", O_CREAT | O_EXCL, 0644, value); semJ = sem_open("semJoao", O_CREAT | O_EXCL, 0644,0); semM = sem_open("semMaria", O_CREAT | O_EXCL, 0644,0); /* name of semaphore is "pSem", semaphore is reached using this name */ sem_unlink("pSem"); sem_unlink("semJoao"); sem_unlink("semMaria"); /* unlink prevents the semaphore existing forever */ /* if a crash occurs during the execution */ printf ("semaphores initialized.\n\n"); int pipeJoao[2]; //pipe do filho 1 int pipeMaria[2]; //pipe do filho 2 if(pipe(pipeJoao) < 0){ perror("pipe1"); return -1; } if(pipe(pipeMaria) < 0){ perror("pipe1"); return -1; } int parent = 1; pid = fork(); if(pid == 0 && parent == 1){ parent = 0; file1 = fopen("a.txt","a+"); if(!file1){ printf("Error! opening programaTxt1 file"); exit(1); } while (1){ sem_wait(semJ); sem_wait(sem); printf("Joao lendo PID = %d\n", getpid()); //close(pipeJoao[1]); read(pipeJoao[0],buffer,sizeof(buffer)); printf("Joao leu do pipe: %s\n", buffer); file1 = fopen("a.txt","w"); if(!file1){ printf("Error! opening programaTxt1 file"); exit(1); } //fseek(file1,0,SEEK_END); fputs(buffer, file1); fclose(file1); //__fpurge(buffer); sem_post(sem); sleep(5); sem_wait(sem); printf("Joao escrevendo PID = %d\n", getpid()); file1 = fopen("a.txt","r"); if(!file1){ printf("Error! opening programaTxt1 file"); exit(1); } //fseek(file1,0,SEEK_SET); //fgets(buffer, 255, file1); fscanf(file1,"%[^\n]",buffer); fclose(file1); //close(pipeMaria[0]); write(pipeMaria[1],buffer,sizeof(buffer)+1); sem_post(sem); sem_post(semM); sleep(10); } }else{ pid = fork(); } if(pid == 0 && parent == 1){ file2 = fopen("b.txt","a+"); if(!file2){ printf("Error! opening programaTxt1 file"); exit(1); } while (1){ sem_wait(sem); printf("Maria escrevendo PID = %d\n", getpid()); file2 = fopen("b.txt","r"); if(!file2){ printf("Error! opening programaTxt1 file"); exit(1); } //fseek(file2,0,SEEK_SET); //fgets(buffer, 255, file2); fscanf(file2,"%[^\n]",buffer); //fscanf(file2, "%s", buffer); printf("Lido do arquivo da Maria: %s\n",buffer); //close(pipeJoao[0]); write(pipeJoao[1], buffer,sizeof(buffer)+1); printf("Escrito no pipe do Joao: %s\n", buffer); fclose(file2); sem_post(sem); sem_post(semJ); sleep(5); sem_wait(semM); sem_wait(sem); printf("Maria lendo PID = %d\n", getpid()); //close(pipeMaria[1]); read(pipeMaria[0], buffer,sizeof(buffer)); printf("Lido do pipe Maria: %s\n",buffer); file2 = fopen("b.txt","w"); if(!file2){ printf("Error! opening programaTxt1 file"); exit(1); } //fseek(file2,0,SEEK_END); fputs(buffer, file2); printf("Maria escreveu no arquivo: %s\n", buffer); //__fpurge(buffer); fclose(file2); sem_post(sem); sleep(10); } } if(pid != 0){ while (pid = waitpid (-1, NULL, 0)){ if (errno == ECHILD) break; } printf ("\nParent: All children have exited.\n"); /* shared memory detach */ shmdt (p); shmctl (shmid, IPC_RMID, 0); /* cleanup semaphores */ sem_destroy (sem); exit (0); } }
×

Important Information

Ao usar o fórum, você concorda com nossos Terms of Use.