Код программы:
server.cpp
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include
#include
#include
#define FIFO_NAME "fifo_file"
#define MAX_CLIENTS 10
using namespace std;
struct employee
{
int num;
char name[10];
double hours;
};
void print_employee(employee emp)
{
cout << "Number: " << emp.num << ", Name: " << emp.name << ", Hours: " << emp.hours << endl;
}
int main()
{
string file_name;
cout << "Enter file name: ";
cin >> file_name;
ofstream file(file_name, ios::binary);
int num_clients;
cout << "Enter number of clients: ";
cin >> num_clients;
employee emp = {0, "", 0.0};
for (int i = 0; i < 10; i++) {
file.write((char*)&emp, sizeof(emp));
}
file.close();
cout << "File content: " << endl;
ifstream infile(file_name, ios::binary);
while(infile.read((char*)&emp, sizeof(emp))) {
print_employee(emp);
}
infile.close();
int fd, fd1;
char readbuf[80], writebuf[80];
int to_end, read_bytes;
mkfifo(FIFO_NAME, S_IFIFO | 0666);
cout << "Waiting for clients..." << endl;
fd = open(FIFO_NAME, O_RDWR);
for (int i = 0; i < num_clients; i++) {
int pid = fork();
if (pid == 0) {
execlp("./client", "client", NULL);
}
}
int client_fds[num_clients];
for (int i = 0; i < num_clients; i++) {
int fd1 = open(FIFO_NAME, O_WRONLY);
client_fds[i] = fd1;
}
int key;
while (1) {
read_bytes = read(fd, readbuf, sizeof(readbuf));
readbuf[read_bytes] = '\0';
key = atoi(readbuf);
employee emp = {0, "", 0.0};
lseek(fd, key * sizeof(emp), SEEK_SET);
read(fd, &emp, sizeof(emp));
cout << "Server got request for key: " << key << endl;
print_employee(emp);
while (1) {
read_bytes = read(fd, readbuf, sizeof(readbuf));
readbuf[read_bytes] = '\0';
if (strcmp(readbuf, "quit") == 0) {
break;
}
else if (strcmp(readbuf, "modify") == 0) {
for (int i = 0; i < num_clients; i++) {
if (client_fds[i] != -1) {
write(client_fds[i], "lock", 5);
cout << "Locking access to key: " << key
<< " for client " << (i+1) << endl;
}
}
employee emp = {0, "", 0.0};
lseek(fd, key * sizeof(emp), SEEK_SET);
read(fd, &emp, sizeof(emp));
print_employee(emp);
write(client_fds[key % num_clients], "data", 5);
write(client_fds[key % num_clients], (char*)&emp, sizeof(emp));
read(client_fds[key % num_clients], readbuf, sizeof(readbuf));
readbuf[strlen(readbuf)] = '\0';
employee new_emp;
memcpy(&new_emp, readbuf, sizeof(new_emp));
cout << "Modified employee record: ";
print_employee(new_emp);
lseek(fd, key * sizeof(new_emp), SEEK_SET);
write(fd, &new_emp, sizeof(new_emp));
for (int i = 0; i < num_clients; i++) {
if (client_fds[i] != -1) {
write(client_fds[i], "unlock", 7);
cout << "Unlocking access to key: " << key
<< " for client " << (i+1) << endl;
}
}
}
else if (strcmp(readbuf, "read") == 0) {
for (int i = 0; i < num_clients; i++) {
if (client_fds[i] != -1) {
write(client_fds[i], "lock", 5);
cout << "Locking access to key: " << key
<< " for client " << (i+1) << endl;
}
}
employee emp = {0, "", 0.0};
lseek(fd, key * sizeof(emp), SEEK_SET);
read(fd, &emp, sizeof(emp));
print_employee(emp);
for (int i = 0; i < num_clients; i++) {
if (client_fds[i] != -1) {
write(client_fds[i], "unlock", 7);
cout << "Unlocking access to key: " << key
<< " for client " << (i+1) << endl;
}
}
}
}
}
for (int i = 0; i < num_clients; i++) {
close(client_fds[i]);
}
close(fd);
unlink(FIFO_NAME);
return 0;
}
client.cpp
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include
#include
#define FIFO_NAME "fifo_file"
using namespace std;
struct employee
{
int num;
char name[10];
double hours;
};
void print_employee(employee emp)
{
cout << "Number: " << emp.num << ", Name: " << emp.name << ", Hours: " << emp.hours << endl;
}
void modify_employee(int fd, int key)
{
employee emp;
read(fd, &emp, sizeof(emp));
print_employee(emp);
employee new_emp;
cout << "Enter new employee details (num, name, hours): ";
cin >> new_emp.num >> new_emp.name >> new_emp.hours;
write(fd, (char*)&new_emp, sizeof(new_emp));
}
void read_employee(int fd, int key)
{
employee emp;
read(fd, &emp, sizeof(emp));
print_employee(emp);
}
void lock(int fd)
{
write(fd, "lock", 5);
char readbuf[80];
read(fd, readbuf, sizeof(readbuf));
readbuf[strlen(readbuf)] = '\0';
if (strcmp(readbuf, "locked") == 0) {
cout << "Acquired lock" << endl;
}
}
void unlock(int fd)
{
write(fd, "unlock", 7);
char readbuf[80];
read(fd, readbuf, sizeof(readbuf));
readbuf[strlen(readbuf)] = '\0';
if (strcmp(readbuf, "unlocked") == 0) {
cout << "Released lock" << endl;
}
}
int main()
{
int fd;
char readbuf[80], writebuf[80];
int to_end, read_bytes;
cout << "Enter key: ";
cin >> writebuf;
mkfifo(FIFO_NAME, S_IFIFO | 0666);
fd = open(FIFO_NAME, O_WRONLY);
write(fd, writebuf, strlen(writebuf)+1);
while (1) {
cout << "Enter command (read, modify, quit): ";
cin >> writebuf;
write(fd, writebuf, strlen(writebuf)+1);
if (strcmp(writebuf, "quit") == 0) {
break;
}
else if (strcmp(writebuf, "modify") == 0) {
char readbuf[80];
read(fd, readbuf, sizeof(readbuf));
readbuf[strlen(readbuf)] = '\0';
if (strcmp(readbuf, "lock") == 0) {
cout << "Waiting for lock..." << endl;
while (strcmp(readbuf, "locked") != 0) {
read(fd, readbuf, sizeof(readbuf));
readbuf[strlen(readbuf)] = '\0';
}
}
modify_employee(fd, atoi(writebuf));
unlock(fd);
}
else if (strcmp(writebuf, "read") == 0) {
char readbuf[80];
read(fd, readbuf, sizeof(readbuf));
readbuf[strlen(readbuf)] = '\0';
if (strcmp(readbuf, "lock") == 0) {
cout << "Waiting for lock..." << endl;
while (strcmp(readbuf, "locked") != 0) {
read(fd, readbuf, sizeof(readbuf));
readbuf[strlen(readbuf)] = '\0';
}
}
read_employee(fd, atoi(writebuf));
unlock(fd);
}
}
close(fd);
unlink(FIFO_NAME);
return 0;
}