-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
118 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,87 @@ | ||
#include <string> | ||
#include <iostream> | ||
#include <sys/socket.h> | ||
#include <unistd.h> | ||
#include <arpa/inet.h> | ||
#include <fstream> | ||
#include <netinet/in.h> | ||
#include <ctime> | ||
#include "../lib/MyLib.h" | ||
#include <stdlib.h> | ||
|
||
using namespace std; | ||
|
||
class remoteMutex | ||
{ | ||
private: | ||
int sock; | ||
/* data */ | ||
public: | ||
remoteMutex(string coord_ip); | ||
int acquire(); | ||
int release(); | ||
~remoteMutex(); | ||
remoteMutex(char *coord_ip) | ||
{ | ||
int valread; | ||
struct sockaddr_in serv_addr; | ||
|
||
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) | ||
{ | ||
cout << "Erro criando o socket." << endl; | ||
return; | ||
} | ||
|
||
serv_addr.sin_family = AF_INET; | ||
serv_addr.sin_port = htons(PORT); | ||
|
||
if (inet_pton(AF_INET, coord_ip, &serv_addr.sin_addr) <= 0) | ||
{ | ||
cout << "Erro no endereço IPv4." << endl; | ||
return; | ||
} | ||
|
||
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) | ||
{ | ||
printf("Erro ao conectar no socket do servidor."); | ||
return; | ||
} | ||
} | ||
int acquire() | ||
{ | ||
char *buffer = encode(REQUEST, getpid()); | ||
send(sock, buffer, sizeof(char) * 30, 0); | ||
int valread = read(sock, buffer, sizeof(char) * 30); | ||
delete buffer; | ||
if (valread != 0) | ||
{ | ||
return 1; | ||
} | ||
else | ||
{ | ||
return 0; | ||
} | ||
} | ||
int release() | ||
{ | ||
char *buffer = encode(RELEASE, getpid()); | ||
send(sock, buffer, sizeof(char) * 30, 0); | ||
delete buffer; | ||
return 1; | ||
} | ||
~remoteMutex(){}; | ||
}; | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
int k = atoi(argv[1]); | ||
int r = atoi(argv[2]); | ||
remoteMutex rm(argv[3]); | ||
ofstream myfile; | ||
for (int i = 0; i < r; i++) | ||
{ | ||
if (!rm.acquire()) | ||
return -1; | ||
myfile.open("resultado.txt", ios::app); | ||
myfile << time(0) << " " << getpid() << "\n"; | ||
myfile.close(); | ||
rm.release(); | ||
sleep(k); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,15 @@ | ||
#pragma once | ||
|
||
#define PORT 8080 | ||
#define F 30 | ||
|
||
enum | ||
{ | ||
REQUEST = 1, | ||
GRANT, | ||
RELEASE | ||
}; | ||
|
||
bool isPrime(int number); | ||
char *encode(int, int); | ||
void decode(char *, int *, int *); |