Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2LATE e CLOSD #40

Merged
merged 5 commits into from
May 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added mp2.pdf
Binary file not shown.
25 changes: 11 additions & 14 deletions src2/Q2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
16 changes: 12 additions & 4 deletions src2/U2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down