diff --git a/mp2.pdf b/mp2.pdf new file mode 100644 index 0000000..b2d223d Binary files /dev/null and b/mp2.pdf differ diff --git a/src2/Q2.c b/src2/Q2.c index bf871ec..566fefa 100644 --- a/src2/Q2.c +++ b/src2/Q2.c @@ -47,7 +47,7 @@ void * thread_func(void *arg){ strcat(privateFifo,"."); if(sprintf(temp,"%ld",tid)<0){perror("Server-sprintf");} strcat(privateFifo,temp); - + // open private fifo int fd_priv; /**< private fifo file descriptor */ float startt = elapsedTime(); @@ -147,26 +147,23 @@ int main(int argc, char* argv[]) { if(pthread_create(&tid, NULL, thread_func, &clientRequest)!=0){perror("Server-pthread_Create");} if(pthread_detach(tid)!=0){perror("Server-pthread_detach");} } - // closing sequence + if(unlink(fifopath)==-1){perror("Error destroying public fifo:");} if(pthread_mutex_lock(&mut2)!=0){perror("Server-MutexLock");} closed.x = 1; if(pthread_mutex_unlock(&mut2)!=0){perror("Server-MutexUnLock");} - float starttime; - int readreturn; - - // notifies client threads that server is closed - starttime = elapsedTime(); - do{ - readreturn = read(fd_pub, &clientRequest, BUFSIZE); - } while (readreturn == 0 && elapsedTime() - starttime < MSATTEMPT); - if (readreturn > 0){ - if(pthread_create(&tid, NULL, thread_func, &clientRequest)!=0){perror("Server-pthread_Create");} - if(pthread_detach(tid)!=0){perror("Server-pthread_detach");} + //float starttime; + int readreturn = read(fd_pub, &clientRequest, BUFSIZE); + while (readreturn != 0) + { + printf("Dentro de while\n"); + if(readreturn <0 ){perror("Server-read error");} + if(pthread_create(&tid, NULL, thread_func, &clientRequest)!=0){perror("Server-pthread_Create");} + if(pthread_detach(tid)!=0){perror("Server-pthread_detach");} + readreturn = read(fd_pub, &clientRequest, BUFSIZE); } // cleanup if(close(fd_pub)==-1){perror("Server-closePublicFifo");} - if(unlink(fifopath)==-1){perror("Error destroying public fifo:");} pthread_exit((void*)0); } diff --git a/src2/U2.c b/src2/U2.c index 5c562dd..856c303 100644 --- a/src2/U2.c +++ b/src2/U2.c @@ -44,11 +44,19 @@ void * thread_func(void *arg){ // opening Public fifo to be able to write float startt = elapsedTime(); + int attempt = 0; do{ - fd_pub = open(fifopath,O_WRONLY); - } while (fd_pub==-1 && elapsedTime() - startt < MSATTEMPT); - if (fd_pub < 0) { - fprintf(stderr, "%d-%s\n", i, "Client - Error Opening Public Fifo"); + fd_pub = open(fifopath,O_WRONLY, O_NONBLOCK); + attempt++; + } while (fd_pub==-1 && attempt < 5); + if (fd_pub == -1) { + //fprintf(stderr, "%d-%s\n", i, "Client - Error Opening Public Fifo"); + if(pthread_mutex_lock(&mut2)!=0){perror("Client-MutexLock");} + printRegister(time(NULL), i, getpid(), pthread_self(), useTime, -1, CLOSD); + if(pthread_mutex_unlock(&mut2)!=0){perror("Client-MutexUnLock");} + if(pthread_mutex_lock(&mut)!=0){perror("Client-MutexLock");} + serverOpen.x = 0; + if(pthread_mutex_unlock(&mut)!=0){perror("Client-MutexUnLock");} pthread_exit(NULL); }