consolidate all repos to one for archive
This commit is contained in:
180
semester_2/uvod_v_operacijske_sisteme/dodatna/streznik.cpp
Normal file
180
semester_2/uvod_v_operacijske_sisteme/dodatna/streznik.cpp
Normal file
@@ -0,0 +1,180 @@
|
||||
#include <pthread.h>
|
||||
#include <sys/sysinfo.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <cstring>
|
||||
|
||||
|
||||
struct SystemInfo{
|
||||
unsigned long memTotal;
|
||||
unsigned long memFree;
|
||||
long upTime;
|
||||
unsigned long swapTotal;
|
||||
unsigned long swapFree;
|
||||
};
|
||||
|
||||
struct MesData{
|
||||
unsigned int size, delay;
|
||||
};
|
||||
|
||||
struct MesData mezureData;
|
||||
std::list<SystemInfo> listSI;
|
||||
int running = 1;
|
||||
int icev1, icev2, icev3;
|
||||
pthread_t tid[4];
|
||||
|
||||
void* glavna(void* arg){
|
||||
int i, l;
|
||||
while (running == 1){
|
||||
icev3 = open("izklop",O_RDONLY);
|
||||
if(icev3 < 0) std::cout << "tezava pri odpiranju cevi 2\n";
|
||||
|
||||
l = read(icev3, &i, sizeof(i));
|
||||
if(l<0) std::cout << "tezava pri branju cevi 2\n";
|
||||
|
||||
if(i > 2){
|
||||
pthread_cancel(tid[2]);
|
||||
pthread_cancel(tid[3]);
|
||||
running = 0;
|
||||
}
|
||||
|
||||
if(close(icev3) != 0) std::cout << "tezava pri zapiranju cevi 0\n";
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void* mezuringNode(void* arg){
|
||||
struct sysinfo si;
|
||||
struct SystemInfo info;
|
||||
unsigned int microsecond = 1000000;
|
||||
while (running == 1)
|
||||
{
|
||||
sysinfo (&si);
|
||||
info.memTotal = si.totalram;
|
||||
info.memFree = si.freeram;
|
||||
info.upTime = si.uptime;
|
||||
info.swapFree = si.freeswap;
|
||||
info.swapTotal = si.totalswap;
|
||||
listSI.push_front(info);
|
||||
while(listSI.size() > mezureData.size){
|
||||
listSI.pop_back();
|
||||
}
|
||||
usleep(mezureData.delay * microsecond);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void* input(void* arg){
|
||||
int tmp[2];
|
||||
int l;
|
||||
while(running == 1){
|
||||
|
||||
icev1 = open("vhod",O_RDONLY);
|
||||
if(icev1 < 0) std::cout << "tezava pri odpiranju cevi 0\n";
|
||||
|
||||
l = read(icev1, &tmp, sizeof(tmp));
|
||||
if(l<0) std::cout << "tezava pri branju cevi 0\n";
|
||||
|
||||
std::cout << tmp[0] << " " << tmp[1] << "\n";
|
||||
mezureData.delay = tmp[0];
|
||||
mezureData.size = tmp[1];
|
||||
|
||||
if(close(icev1) != 0) std::cout << "tezava pri zapiranju cevi 0\n";
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void* output(void* arg){
|
||||
unsigned long array[100];
|
||||
int l, i = 0;
|
||||
|
||||
while(running == 1){
|
||||
icev2 = open("izhod",O_WRONLY);
|
||||
if(icev2 < 0)
|
||||
std::cout << "Napaka pri odprtju cevi 1\n";
|
||||
else
|
||||
std::cout << "Odprta cevi 1\n";
|
||||
|
||||
i = 0;
|
||||
for (auto& elm : listSI){
|
||||
array[i] = elm.memTotal;
|
||||
i++;
|
||||
array[i] = elm.memFree;
|
||||
i++;
|
||||
array[i] = elm.upTime;
|
||||
i++;
|
||||
array[i] = elm.swapFree;
|
||||
i++;
|
||||
array[i] = elm.swapTotal;
|
||||
i++;
|
||||
}
|
||||
|
||||
|
||||
l = write(icev2, &array, sizeof(array));
|
||||
if(l < 0)
|
||||
std::cout << "tezave pri pisanju v cev\n";
|
||||
else
|
||||
std::cout << "Vpisano v cevi 1\n";
|
||||
|
||||
if(close(icev2) != 0)
|
||||
std::cout << "tezava pri zapiranju cevi 1\n";
|
||||
else
|
||||
std::cout << "Zaprta cevi 1\n";
|
||||
|
||||
usleep(10000);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
std::cout << "Hello World\n";
|
||||
|
||||
mezureData.delay = 1;
|
||||
mezureData.size = 4;
|
||||
|
||||
mkfifo("vhod", S_IRUSR|S_IWUSR);
|
||||
mkfifo("izhod", S_IRUSR|S_IWUSR);
|
||||
mkfifo("izklop", S_IRUSR|S_IWUSR);
|
||||
|
||||
pthread_create(&tid[0], NULL, glavna, NULL);
|
||||
pthread_create(&tid[1], NULL, mezuringNode, NULL);
|
||||
pthread_create(&tid[2], NULL, input, NULL);
|
||||
pthread_create(&tid[3], NULL, output, NULL);
|
||||
|
||||
pthread_join(tid[0], NULL);
|
||||
pthread_join(tid[1], NULL);
|
||||
pthread_join(tid[2], NULL);
|
||||
pthread_join(tid[3], NULL);
|
||||
|
||||
std::string str;
|
||||
char* strc;
|
||||
for (auto& elm : listSI){
|
||||
str += "TotalMemory: " + std::to_string(elm.memTotal) + " ";
|
||||
str += "FreeMemory: " + std::to_string(elm.memFree) + " ";
|
||||
str += "UpTime: " + std::to_string(elm.upTime) + " ";
|
||||
str += "FreeSwap: " + std::to_string(elm.swapFree) + " ";
|
||||
str += "TotalSwap: " + std::to_string(elm.swapTotal) + "\n";
|
||||
}
|
||||
strc = &str[0];
|
||||
|
||||
printf("%s",strc);
|
||||
|
||||
close(icev1);
|
||||
close(icev2);
|
||||
close(icev3);
|
||||
|
||||
unlink("vhod");
|
||||
unlink("izhod");
|
||||
unlink("izklop");
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user