Skip to content

Commit

Permalink
Merge pull request #3 from dario-santos/feature-multithread
Browse files Browse the repository at this point in the history
feature-multithread -> master
  • Loading branch information
dario-santos authored Jun 12, 2019
2 parents 8a384ed + 0108407 commit 0cbc95c
Show file tree
Hide file tree
Showing 13 changed files with 407 additions and 47 deletions.
Empty file modified Project/Client/client
100644 → 100755
Empty file.
9 changes: 6 additions & 3 deletions Project/Server/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
CC=cc
FLAGS=-c -Wall
LIBS=-lm -lpthread
OBS=source_server.o msg.o fila.o server.o connection.o ftp.o send_struct.o
OBS=source_server.o msg.o fila.o server.o connection.o ftp.o send_struct.o lock.o

all : source_server

Expand All @@ -15,7 +15,10 @@ fila.o : fila.h fila.c
ftp.o : ftp.h fifo.h ftp.c
$(CC) $(FLAGS) ftp.c

send_struct.o : send_struct.h send_struct.c
lock.o : lock.h lock.c
$(CC) $(FLAGS) lock.c

send_struct.o : send_struct.h lock.h send_struct.c
$(CC) $(FLAGS) send_struct.c

connection.o : fifo.h connection.h connection.c
Expand All @@ -24,7 +27,7 @@ connection.o : fifo.h connection.h connection.c
source_server.o : server.h fifo.h source_server.c
$(CC) $(FLAGS) source_server.c

server.o : send_struct.h server.h ftp.h connection.h fila.h fifo.h server.c
server.o : send_struct.h server.h ftp.h connection.h lock.h fila.h fifo.h server.c
$(CC) $(FLAGS) server.c


Expand Down
63 changes: 63 additions & 0 deletions Project/Server/lock.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <semaphore.h>
#include <stdlib.h>
#include "lock.h"

sem_t sem_lock;
sem_t sem_unlock;

void lock_sem_init(void)
{
sem_init(&sem_lock, 0, 1);
sem_init(&sem_unlock, 0, 1);
}

int lock(int *L, int size, int index)
{
if (index <= -1 || index >= size)
return 0;

sem_wait(&sem_lock);
if (L[index] == 0)
L[index] = 1;
else
{
sem_post(&sem_lock);
return 0;
}

sem_post(&sem_lock);
return 1;
}

int unlock(int *L, int size, int index)
{
if (index <= -1 || index >= size)
return 0;

sem_wait(&sem_unlock);

if (L[index] == 1)
L[index] = 0;
else
{
sem_post(&sem_unlock);
return 0;
}

sem_post(&sem_unlock);
return 1;
}

int *locks_insert_node(int *L, int *size)
{
(*size)++;

if (*size == 1)
L = (int *)malloc(sizeof(int));
else
L = (int *)realloc(L, (*size) * sizeof(int));

L[*size - 1] = 0;

return L;
}
12 changes: 12 additions & 0 deletions Project/Server/lock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef LOCK_H
#define LOCK_H

void lock_sem_init(void);

int lock(int *L, int size, int index);

int unlock(int *L, int size, int index);

int* locks_insert_node(int *L, int *size);

#endif // LOCK_H
Loading

0 comments on commit 0cbc95c

Please sign in to comment.