lab2
diff --git a/Child.cpp b/Child.cpp
deleted file mode 100644
index 89d914a..0000000
--- a/Child.cpp
+++ /dev/null
@@ -1,180 +0,0 @@
-#include "Child.h"
-
-
-void setLevel(int log_level)
-{
-
-	time_t seconds = time(NULL);
-	tm* timeinfo = localtime(&seconds);
-	fstream file(FILE_NAME, ios::app);
-	switch (log_level)
-	{
-	case LOG_DEBUG_LEVEL:
-	{
-		file << asctime(timeinfo) << "some_debug_info" << endl;
-		file << asctime(timeinfo) << "Error: index out of range." << endl;
-		file << asctime(timeinfo) << "Error: could not find file." << endl;
-		file << asctime(timeinfo) << "Fatal_error: processes must be closed." << endl;
-		file.close();
-		cout << "some_debug_info" << endl;
-		cout << "Error: index out of range." << endl;
-		cout << "Error: could not find file." << endl;
-		cout << "Fatal_error: processes must be closed." << endl;
-		break;
-	}
-	case LOG_ERROR_LEVEL:
-	{
-		file << asctime(timeinfo) << "Error: could not find file." << endl;
-		file << asctime(timeinfo) << "Fatal_error: processes must be closed." << endl;
-		file.close();
-		cout << "Error: could not find file." << endl;
-		cout << "Fatal_error: processes must be closed." << endl;
-		break;
-	}
-	case LOG_FATAL_LEVEL:
-	{
-		file << asctime(timeinfo) << "Fatal_error: processes must be closed." << endl;
-		file.close();
-		cout << "Fatal_error: processes must be closed." << endl;
-		break;
-	}
-	}
-}
-
-#ifdef _MSC_VER
-
-#define UP_EVENT 'U'
-#define DOWN_EVENT 'D'
-#define EXIT_EVENT 'E'
-#define ACCESS_EVENT "A"
-
-#else
-
-void handler(int p)
-{
-	switch(p) 
-	{
-	case SIGINT:
-	{
-		exit(0);
-	}
-	case SIGHUP:
-	{
-		setLevel();
-		cout << "getppid() = " << getppid() << endl;
-		kill(getppid(), SIGHUP);
-		return;
-	}
-	case SIGUSR1:
-	{
-		if (log_level < 2)
-		{
-			log_level++;
-		}
-		return;
-	}	
-
-	case SIGUSR2:
-	{
-		cout << "Log level down" << endl;
-		if (log_level > 0)
-		{
-			log_level--;
-		}
-		return;
-	}
-	}
-}	
-#endif
-
-int main(int argc, char * argv[])
-{
-
-#ifdef _MSC_VER
-
-	HANDLE up_event;
-	HANDLE down_event;
-	HANDLE exit_event;
-	HANDLE access_event;
-	DWORD check;
-	char buff[3];
-	buff[0] = argv[0][0];
-	buff[2] = '\0';
-	int log_level = 2;
-
-	buff[1] = UP_EVENT;
-	if (!(up_event = OpenEventA(EVENT_ALL_ACCESS | EVENT_MODIFY_STATE, TRUE, buff)))
-	{
-		return 0;
-	}
-
-	buff[1] = DOWN_EVENT;
-	if (!(down_event = OpenEventA(EVENT_ALL_ACCESS | EVENT_MODIFY_STATE, TRUE, buff)))
-	{
-		return 0;
-	}
-
-	buff[1] = EXIT_EVENT;
-	if (!(exit_event = OpenEventA(EVENT_ALL_ACCESS | EVENT_MODIFY_STATE, TRUE, buff)))
-	{
-		return 0;
-	}
-
-	if (!(access_event = OpenEventA(EVENT_ALL_ACCESS | EVENT_MODIFY_STATE, TRUE, ACCESS_EVENT)))
-	{
-		return 0;
-	}
-
-	while (true)
-	{
-		check = WaitForSingleObject(up_event, NULL);
-		if (check == WAIT_OBJECT_0)
-		{
-			if (log_level < 2){
-				log_level++;
-			}
-		}
-
-		check = WaitForSingleObject(down_event, NULL);
-		if (check == WAIT_OBJECT_0)
-		{
-			if (log_level > 0){
-				log_level--;
-			}
-		}
-
-		check = WaitForSingleObject(exit_event, NULL);
-		if (check == WAIT_OBJECT_0)
-		{
-			return 0;
-		}
-
-		check = WaitForSingleObject(access_event, INFINITY);
-		if (check == WAIT_OBJECT_0) 
-		{
-			setLevel(log_level);
-			SetEvent(access_event);
-		}
-		Sleep(4000);
-	}
-
-#else
-
-	struct sigaction act;
-	memset(&act, 0, sizeof(act));
-	act.sa_handler = handler;
-	sigset_t set;
-	sigemptyset(&set);
-	sigaddset(&set, SIGUSR1);
-	sigaddset(&set, SIGUSR2);
-	sigaddset(&set, SIGINT);
-	sigaddset(&set, SIGHUP);
-	act.sa_mask = set;
-	sigaction(SIGUSR1, &act, NULL);
-	sigaction(SIGUSR2, &act, NULL);
-	sigaction(SIGINT, &act, NULL);
-	sigaction(SIGHUP, &act, NULL);
-
-#endif	
-
-}
\ No newline at end of file
diff --git a/Child.h b/Child.h
deleted file mode 100644
index 17e9f3a..0000000
--- a/Child.h
+++ /dev/null
@@ -1,27 +0,0 @@
-#ifdef _MSC_VER
-
-#define _CRT_SECURE_NO_WARNINGS
-#include <cstdlib>
-#include <windows.h>
-
-#else
-
-#include <unistd.h>
-#include <stdio.h>
-#include <cstring>
-#include <sys/types.h>
-#include <signal.h>
-
-#endif
-
-#include <string>
-#include <fstream>
-#include <ctime>
-#include <cstdlib>
-#include <iostream>
-using namespace std;
-
-#define FILE_NAME "log.log"
-#define LOG_DEBUG_LEVEL 2
-#define LOG_ERROR_LEVEL 1
-#define LOG_FATAL_LEVEL 0
\ No newline at end of file
diff --git a/Client.cpp b/Client.cpp
new file mode 100644
index 0000000..d85efb2
--- /dev/null
+++ b/Client.cpp
@@ -0,0 +1,116 @@
+#include "Client.h"
+
+#ifndef _MSC_VER
+void handler(int snum) {
+  if (snum == SIGUSR1) {
+    Send_message();
+    kill(pnum, SIGUSR1);
+  }
+}
+#endif
+
+int main(int argc, char *argv[]) {
+
+#ifdef _MSC_VER
+
+  if (!(hEvent = OpenEvent(EVENT_ALL_ACCESS | EVENT_MODIFY_STATE, TRUE, SYNC_EVENT))) {
+    cout << "Run server program before." << endl;
+    system("pause");
+    return 0;
+  }
+
+  hNamedPipe = CreateFileA(
+      szPipeName,
+      GENERIC_READ | GENERIC_WRITE,
+      0,
+      NULL,
+      OPEN_EXISTING,
+      0,
+      NULL);
+
+  if (hNamedPipe == INVALID_HANDLE_VALUE) {
+    cout << "Run server program before." << endl;
+    system("pause");
+    return 0;
+  }
+
+#else
+
+  struct sigaction act;
+  memset(&act, 0, sizeof(act));
+  act.sa_handler = handler;
+  sigset_t set;
+  sigemptyset(&set);
+  sigaddset(&set, SIGUSR1);
+  act.sa_mask = set;
+  sigaction(SIGUSR1, &act, NULL);
+
+  cout << "Client pid is " << getpid() << endl;
+  cout << " Server pid: ";
+  cin >> pnum;
+
+  if (mkfifo(NAMEDPIPE_NAME, 0777)) {
+    perror("mkfifo");
+  }
+
+  if ((fd = open(NAMEDPIPE_NAME, O_RDWR)) <= 0) {
+    perror("Openfile");
+    return 1;
+  }
+
+#endif
+
+  cout << "Enter your messages here:" << endl;
+
+#ifdef _MSC_VER
+
+  while (true) {
+    Send_message();
+    SetEvent(hEvent);
+  }
+
+  CloseHandle(hNamedPipe);
+
+#else
+
+  while (true) {
+    sleep(1);
+  }
+
+#endif
+
+  return 0;
+}
+
+#ifdef _MSC_VER
+
+void Send_message() {
+  getline(cin, buf);
+  if (buf == "exit") {
+    SetEvent(hEvent);
+    exit(0);
+  }
+  strcpy_s(buffer, buf.c_str());
+  if (!WriteFile(hNamedPipe, buffer, strlen(buffer) + 1, &cbWritten, NULL)) {
+    perror("WriteFile");
+  }
+}
+
+#else
+
+void Send_message() {
+  memset(readbuffer, '\0', BUF_SIZE);
+  memset(buffer, '\0', BUF_SIZE);
+  cin.clear();
+  while (cin.get() != '\n')
+    ;
+  getline(cin, buf);
+  if (buf == "exit") {
+    kill(pnum, SIGUSR2);
+    exit(0);
+  }
+  strcpy(buffer, buf.c_str());
+  write(fd, buffer, strlen(buffer));
+}
+
+#endif
diff --git a/Client.h b/Client.h
new file mode 100644
index 0000000..c5eb903
--- /dev/null
+++ b/Client.h
@@ -0,0 +1,37 @@
+#ifdef _MSC_VER
+#include <windows.h>
+#else
+#include <unistd.h>
+#include <sys/wait.h>
+#include <sys/stat.h>
+#include <stdlib.h>
+#include <signal.h>
+#include <dirent.h>
+#include <cstdio>
+#include <fcntl.h>
+#endif
+#include <cstring>
+#include <string>
+#include <sstream>
+#include <iostream>
+
+using namespace std;
+
+#define BUF_SIZE 512
+
+char buffer[BUF_SIZE];
+char readbuffer[BUF_SIZE];
+string buf;
+void Send_message();
+
+#ifdef _MSC_VER
+#define SYNC_EVENT LPCWSTR("sync")
+HANDLE hEvent;
+HANDLE hNamedPipe;
+LPSTR szPipeName = "\\\\.\\pipe\\$mypipe$";
+DWORD cbWritten;
+DWORD cbRead;
+#else
+#define NAMEDPIPE_NAME "/tmp/serverok"
+int fd, len, pnum;
+#endif
diff --git a/Parent.cpp b/Parent.cpp
deleted file mode 100644
index abde75f..0000000
--- a/Parent.cpp
+++ /dev/null
@@ -1,244 +0,0 @@
-#include "parent.h"
-
-bool CreateProcesses()
-{
-
-#ifdef _MSC_VER
-
-	accessEvent = CreateEventA(NULL, FALSE, TRUE, ACCESS_EVENT);
-	char buff[3];
-	for (int i = 0; i < PROCESSCOUNT; i++)
-	{
-		_itoa(i, buff, 10);
-		buff[2] = '\0';
-		buff[1] = UP_EVENT;
-		upEvent[i] = CreateEventA(NULL, FALSE, FALSE, buff);
-		buff[1] = DOWN_EVENT;
-		downEvent[i] = CreateEventA(NULL, FALSE, FALSE, buff);
-		buff[1] = EXIT_EVENT;
-		exitEvent[i] = CreateEventA(NULL, FALSE, FALSE, buff);
-
-		ZeroMemory(&si, sizeof(si));
-		ZeroMemory(&pi, sizeof(pi));
-
-		if (!CreateProcessA(PATH,
-			_itoa(i, buff, 10),
-			NULL,
-			NULL,
-			FALSE,
-			0,
-			NULL,
-			NULL,
-			&si[i],
-			&pi[i])
-			)
-		{
-			printf("CreateProcess failed (%d).\n", GetLastError());
-			system("pause");
-			return false;
-		}
-	}
-
-#else
-
-	struct sigaction act;
-	memset(&act, 0, sizeof(act));
-	act.sa_handler = synchandler;
-	sigset_t set;
-	sigemptyset(&set);
-	sigaddset(&set, SIGHUP);
-	act.sa_mask = set;
-	sigaction(SIGHUP, &act, NULL);
-	for (int i = 0; i < PROCESSCOUNT; i++)
-	{
-		pid[i] = fork();
-		switch(pid[i]){
-		case 0:
-		{
-			if (!execl(PATH, "", NULL)){
-				printf("execl failed ");
-				return false;
-			}
-			break;
-		}
-		case -1:
-		{
-			return false;
-		}
-		default:
-		{
-			continue;
-		}
-		}
-	}
-
-#endif
-
-	return true;
-}
-
-void PrintMenu(){
-	cout << "1. level up." << endl;
-	cout << "2. level lower." << endl;
-	cout << "3. exit." << endl;
-	cout << "4. skip." << endl;
-}
-
-void ShowMenu()
-{
-
-#ifdef _MSC_VER
-
-	DWORD check = WaitForSingleObject(accessEvent, INFINITY);
-	if (check == WAIT_OBJECT_0)
-	{
-		PrintMenu();
-		SetEvent(accessEvent);
-	}
-
-#else
-
-	PrintMenu();
-
-#endif
-}
-
-int ChooseMenu(int &inputNumber)
-{
-	while (true){
-		if (!(cin >> inputNumber))
-		{
-			cin.clear();
-			cin.sync();
-		}
-		else if (inputNumber < 1 || inputNumber > 4){
-			cin.clear();
-			cin.sync();
-		}
-		else {
-			return inputNumber;
-		}
-	}
-}
-
-void WaitAndClose()
-{
-
-#ifdef _MSC_VER
-
-	for (int i = 0; i < PROCESSCOUNT; i++)
-	{
-		WaitForSingleObject(pi[i].hProcess, INFINITE);
-		CloseHandle(pi[i].hProcess);
-		CloseHandle(upEvent[i]);
-		CloseHandle(downEvent[i]);
-		CloseHandle(exitEvent[i]);
-
-	}
-	CloseHandle(accessEvent);
-
-#else
-
-	for (int i = 0; i < PROCESSCOUNT; i++)
-	{
-		waitpid(pid[i], 0, 0);
-	}
-
-#endif
-}
-
-int main()
-{
-	if (!CreateProcesses())
-	{
-		return 1;
-	}
-
-	int inputNumber;
-
-	do {
-		ShowMenu();
-		ChooseMenu(inputNumber);
-		switch (inputNumber)
-		{
-		case 1: {
-			for (int i = 0; i < PROCESSCOUNT; i++)
-			{
-
-#ifdef _MSC_VER
-
-				SetEvent(upEvent[i]);
-
-#else
-
-				kill(pid[i], SIGUSR1);
-
-#endif
-
-			}
-			break;
-		}
-		case 2:
-		{
-			for (int i = 0; i < PROCESSCOUNT; i++)
-			{
-
-#ifdef _MSC_VER
-
-				SetEvent(downEvent[i]);
-
-#else
-
-				kill(pid[i], SIGUSR2);
-
-#endif
-			}
-			break;
-		}
-		case 3:
-		{
-			for (int i = 0; i < PROCESSCOUNT; i++)
-			{
-
-#ifdef _MSC_VER
-
-				SetEvent(exitEvent[i]);
-
-#else
-
-				kill(pid[i], SIGINT);
-
-#endif
-			}
-			break;
-		}
-		default:
-		{
-			break;
-		}
-		}
-#ifdef _MSC_VER
-
-#else
-
-		if(inputNumber != 3)
-		{
-			for(int i = 0; i < PROCESSCOUNT; i++) 
-			{
-				kill(pid[i], SIGHUP);
-				fl = true;
-				while (fl)
-				{
-					sleep(1);
-				}
-			}
-		}
-
-#endif
-
-	} while (inputNumber != 3);
-
-	WaitAndClose();
-	return 0;
-}
-
diff --git a/Server.cpp b/Server.cpp
new file mode 100644
index 0000000..c218edb
--- /dev/null
+++ b/Server.cpp
@@ -0,0 +1,109 @@
+#include "Server.h"
+
+#ifndef _MSC_VER
+void handler(int snum) {
+
+  if (snum == SIGUSR1) {
+    Recieve_message();
+    kill(pnum, SIGUSR1);
+  } else if (snum == SIGUSR2) {
+    close(fd);
+    remove(NAMEDPIPE_NAME);
+    exit(0);
+  }
+}
+#endif
+
+int main(int argc, char *argv[]) {
+
+  cout << "Now run client program." << endl;
+
+#ifdef _MSC_VER
+
+  HANDLE hEvent;
+  hEvent = CreateEvent(NULL, FALSE, FALSE, SYNC_EVENT);
+
+  hNamedPipe = CreateNamedPipeA(
+      lpszPipeName,
+      PIPE_ACCESS_DUPLEX,
+      PIPE_TYPE_MESSAGE |
+      PIPE_READMODE_MESSAGE |
+      PIPE_WAIT,
+      PIPE_UNLIMITED_INSTANCES,
+      BUF_SIZE,
+      BUF_SIZE,
+      5000,
+      NULL);
+
+  if (hNamedPipe == INVALID_HANDLE_VALUE) {
+    perror("CreateNamedPipe");
+    return 1;
+  }
+
+  if (!ConnectNamedPipe(hNamedPipe, NULL)) {
+    perror("ConnectNamedPipe");
+  }
+
+#else
+
+  struct sigaction act;
+  memset(&act, 0, sizeof(act));
+  act.sa_handler = handler;
+  sigset_t set;
+  sigemptyset(&set);
+  sigaddset(&set, SIGUSR1);
+  sigaddset(&set, SIGUSR2);
+  act.sa_mask = set;
+  sigaction(SIGUSR1, &act, NULL);
+  sigaction(SIGUSR2, &act, NULL);
+  cout << "Server pid is " << getpid() << endl;
+
+  cout << " Client pid: ";
+  cin >> pnum;
+
+  if ((fd = open(NAMEDPIPE_NAME, O_RDWR)) <= 0) {
+    perror("openfile");
+    return 1;
+  }
+
+  kill(pnum, SIGUSR1);
+
+#endif
+  cout << "Recieved messages:" << endl;
+#ifdef _MSC_VER
+  while (true) {
+    WaitForSingleObject(hEvent, INFINITE);
+    Recieve_message();
+  }
+#endif
+
+  while (true) {
+    sleep(1);
+  }
+  return 0;
+}
+
+#ifdef _MSC_VER
+
+void Recieve_message() {
+  if (ReadFile(hNamedPipe, buffer, BUF_SIZE, &cbRead, NULL)) {
+
+    cout << buffer << endl;
+  }
+  else {
+    DisconnectNamedPipe(hNamedPipe);
+    CloseHandle(hNamedPipe);
+    exit(0);
+  }
+}
+#else
+
+void Recieve_message() {
+
+  memset(readbuffer, '\0', BUF_SIZE);
+  memset(buffer, '\0', BUF_SIZE);
+  read(fd, readbuffer, BUF_SIZE - 1);
+  cout << readbuffer << endl;
+}
+
+#endif
diff --git a/Server.h b/Server.h
new file mode 100644
index 0000000..e80d499
--- /dev/null
+++ b/Server.h
@@ -0,0 +1,36 @@
+#ifdef _MSC_VER
+#include <windows.h>
+#else
+#include <unistd.h>
+#include <sys/wait.h>
+#include <sys/stat.h>
+#include <stdlib.h>
+#include <signal.h>
+#include <dirent.h>
+#include <stdio.h>
+#include <fcntl.h>
+#endif
+#include <cstring>
+#include <string>
+#include <sstream>
+#include <iostream>
+
+using namespace std;
+
+#define BUF_SIZE 512
+
+string getCommand(char *, int);
+void Recieve_message();
+char buffer[BUF_SIZE];
+char readbuffer[BUF_SIZE];
+
+#ifdef _MSC_VER
+#define SYNC_EVENT LPCWSTR("sync")
+HANDLE hNamedPipe;
+LPSTR lpszPipeName = "\\\\.\\pipe\\$mypipe$";
+DWORD cbRead;
+DWORD cbWritten;
+#else
+#define NAMEDPIPE_NAME "/tmp/serverok"
+int fd, len, pnum;
+#endif
diff --git a/parent.h b/parent.h
deleted file mode 100644
index 54366b4..0000000
--- a/parent.h
+++ /dev/null
@@ -1,48 +0,0 @@
-#ifdef _MSC_VER
-
-#define _CRT_SECURE_NO_WARNINGS
-#include <windows.h>
-
-#else
-
-#include <unistd.h>
-#include <sys/types.h>
-#include <semaphore.h>
-#include <signal.h>
-#include <cstring>
-#include <sys/wait.h>
-
-#endif
-
-#include <iostream>
-#include <stdio.h>
-#include <string>
-using namespace std;
-
-#ifdef _MSC_VER
-
-#define UP_EVENT 'U'
-#define DOWN_EVENT 'D'
-#define EXIT_EVENT 'E'
-#define ACCESS_EVENT "A"
-#define PATH "..\\Debug\\labka2_spo_child_lin.exe"
-
-#endif
-
-#define PROCESSCOUNT 4
-
-#ifdef _MSC_VER
-STARTUPINFOA si[PROCESSCOUNT];
-PROCESS_INFORMATION pi[PROCESSCOUNT];
-HANDLE upEvent[PROCESSCOUNT], downEvent[PROCESSCOUNT], exitEvent[PROCESSCOUNT], accessEvent;
-
-#else
-
-#define PATH "child"
-int pid[PROCESSCOUNT];
-bool fl;
-void synchandler(int p){
-	fl = false;
-}
-
-#endif
\ No newline at end of file