Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added lab0/hello/empty
Empty file.
6 changes: 6 additions & 0 deletions lab0/hello/newhello.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <stdio.h>

int main()
{
printf("Hello World!\n");
}
7 changes: 7 additions & 0 deletions lab1/proga
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

echo "Текущий путь: $(pwd)"

echo "Текущая дата и время: $(date)"

echo $PATH
14 changes: 13 additions & 1 deletion lab3/src/find_min_max.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <stdio.h>
#include <stdlib.h>
#include "find_min_max.h"

#include <limits.h>
Expand All @@ -7,6 +9,16 @@ struct MinMax GetMinMax(int *array, unsigned int begin, unsigned int end) {
min_max.min = INT_MAX;
min_max.max = INT_MIN;

// your code here
for(int i = begin; i < end; i++){
if (array[i] < min_max.min){
min_max.min = array[i];
}

if (array[i] > min_max.max){
min_max.max = array[i];
}
}

return min_max;
}

Binary file added lab3/src/find_min_max.o
Binary file not shown.
Binary file added lab3/src/image-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lab3/src/image-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lab3/src/image-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lab3/src/image-4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lab3/src/image-5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lab3/src/image-6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lab3/src/image-7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lab3/src/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 8 additions & 1 deletion lab3/src/makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
CC=gcc
CFLAGS=-I.

.PHONY: all clean

all: sequential_min_max parallel_min_max

sequential_min_max : utils.o find_min_max.o utils.h find_min_max.h
$(CC) -o sequential_min_max find_min_max.o utils.o sequential_min_max.c $(CFLAGS)

Expand All @@ -13,5 +17,8 @@ utils.o : utils.h
find_min_max.o : utils.h find_min_max.h
$(CC) -o find_min_max.o -c find_min_max.c $(CFLAGS)

run: run.c
$(CC) -o run run.c $(CFLAGS)

clean :
rm utils.o find_min_max.o sequential_min_max parallel_min_max
rm -f utils.o find_min_max.o sequential_min_max parallel_min_max
Binary file added lab3/src/min_max
Binary file not shown.
Binary file added lab3/src/parallel_min_max
Binary file not shown.
118 changes: 97 additions & 21 deletions lab3/src/parallel_min_max.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include <ctype.h>
#include <limits.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#include <sys/time.h>
Expand All @@ -15,19 +17,30 @@
#include "find_min_max.h"
#include "utils.h"

pid_t* pids;
int pnum = -1;

void KILL_CHILDREN() {
printf("timeout exceeded. Killing children and exiting immediately\n");
for (int i = 0; i < pnum; i++) {
kill(pids[i], 0);
}

exit(0);
}

int main(int argc, char **argv) {
int seed = -1;
int array_size = -1;
int pnum = -1;
bool with_files = false;
int timeout = -1;

while (true) {
int current_optind = optind ? optind : 1;

static struct option options[] = {{"seed", required_argument, 0, 0},
{"array_size", required_argument, 0, 0},
{"pnum", required_argument, 0, 0},
{"by_files", no_argument, 0, 'f'},
{"timeout", required_argument, 0, 0},
{0, 0, 0, 0}};

int option_index = 0;
Expand All @@ -40,22 +53,35 @@ int main(int argc, char **argv) {
switch (option_index) {
case 0:
seed = atoi(optarg);
// your code here
// error handling
if (seed <= 0) {
printf("seed must be a positive number\n");
return 1;
}
break;
case 1:
array_size = atoi(optarg);
// your code here
// error handling
if (array_size <= 0) {
printf("array size must be a positive number\n");
return 1;
}
break;
case 2:
pnum = atoi(optarg);
// your code here
// error handling
if (pnum <= 0) {
printf("pnum must be a positive number\n");
return 1;
}
break;
case 3:
with_files = true;
break;
case 4:
timeout = atoi(optarg);
if (timeout <= 0) {
printf("timeout must be a positive number\n");
return 1;
}
break;

defalut:
printf("Index %d is out of options\n", option_index);
Expand Down Expand Up @@ -91,20 +117,53 @@ int main(int argc, char **argv) {
struct timeval start_time;
gettimeofday(&start_time, NULL);

pids = malloc(pnum * sizeof(pid_t));
int* pipes = malloc(pnum * 2 * sizeof(int));

int baseChunkSize = array_size / pnum;
int extendedCount = array_size % pnum;
int j = 0;

if (timeout > 0) {
signal(SIGALRM, KILL_CHILDREN);
alarm(timeout);
printf("Starting calculation with timeout %ds\n", timeout);
}

for (int i = 0; i < pnum; i++) {
int* readDescriptor = pipes + i * 2;
pipe(readDescriptor);

int startIndex = j;
int endIndex = startIndex + baseChunkSize;
if (i < extendedCount) endIndex++;
j = endIndex;

pid_t child_pid = fork();

if (child_pid >= 0) {
// successful fork
active_child_processes += 1;
pids[i] = child_pid;
active_child_processes++;
if (child_pid == 0) {
// child process

// parallel somehow
struct MinMax chunkResult = GetMinMax(array, startIndex, endIndex);

if (with_files) {
// use files here
char fileName[40];
sprintf(fileName, "./%d.txt", getpid());
FILE* temporaryResultsFile = fopen(fileName, "w");
if (!temporaryResultsFile) {
printf("Cannot open file %s\n", fileName);
return 1;
}

fprintf(temporaryResultsFile, "%d %d", chunkResult.min, chunkResult.max);
fclose(temporaryResultsFile);
} else {
// use pipe here
int* writeDescriptor = readDescriptor + 1;
write(*writeDescriptor, &chunkResult, sizeof(struct MinMax));

close(*readDescriptor);
close(*writeDescriptor);
}
return 0;
}
Expand All @@ -116,9 +175,14 @@ int main(int argc, char **argv) {
}

while (active_child_processes > 0) {
// your code here
int pid = pids[active_child_processes - 1];
int code = waitpid(pid, NULL, 0);
if (!code) {
printf("Failed to wait for process with id: %d", pid);
return 1;
}

active_child_processes -= 1;
active_child_processes--;
}

struct MinMax min_max;
Expand All @@ -130,9 +194,19 @@ int main(int argc, char **argv) {
int max = INT_MIN;

if (with_files) {
// read from files
char fileName[40];
sprintf(fileName, "./%d.txt", pids[i]);
FILE* temporaryResultsFile = fopen(fileName, "r");

fscanf(temporaryResultsFile, "%d %d", &min, &max);
fclose(temporaryResultsFile);
remove(fileName);
} else {
// read from pipes
int readDescriptor = pipes[i * 2];
struct MinMax buff;
read(readDescriptor, &buff, sizeof(struct MinMax));
min = buff.min;
max = buff.max;
}

if (min < min_max.min) min_max.min = min;
Expand All @@ -146,10 +220,12 @@ int main(int argc, char **argv) {
elapsed_time += (finish_time.tv_usec - start_time.tv_usec) / 1000.0;

free(array);
free(pids);
free(pipes);

printf("Min: %d\n", min_max.min);
printf("Max: %d\n", min_max.max);
printf("Elapsed time: %fms\n", elapsed_time);
fflush(NULL);
return 0;
}
}
46 changes: 46 additions & 0 deletions lab3/src/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Задание 1

---
![alt text](image.png)
---

```bash
@StarAres1 ➜ /workspaces/os_lab_2019 (master) $ gcc --version

@StarAres1 ➜ /workspaces/os_lab_2019 (master) $ cd lab3
@StarAres1 ➜ /workspaces/os_lab_2019/lab3 (master) $ cd src

@StarAres1 ➜ /workspaces/os_lab_2019/lab3/src (master) $ gcc -c find_min_max.c
@StarAres1 ➜ /workspaces/os_lab_2019/lab3/src (master) $ gcc -c utils.c
@StarAres1 ➜ /workspaces/os_lab_2019/lab3/src (master) $ gcc -c sequantial_min_max.c

@StarAres1 ➜ /workspaces/os_lab_2019/lab3/src (master) $ gcc find_min_max.o utils.c sequential_min_max.o -o min_max
@StarAres1 ➜ /workspaces/os_lab_2019/lab3/src (master) $ ./min_max 1 3
```
![alt text](image-1.png)
# Задание 2-3

---
![alt text](image-2.png)
---

```bash
@StarAres1 ➜ /workspaces/os_lab_2019/lab3/src (master) $ gcc -o parallel_min_max parallel_min_max.c find_min_max.c utils.c -lm
```
![alt text](image-3.png)

# Задание 4

---
![alt text](image-5.png)
---

![alt text](image-4.png)

# Задание 5

---
![alt text](image-6.png)
---

![alt text](image-7.png)
Binary file added lab3/src/run
Binary file not shown.
34 changes: 34 additions & 0 deletions lab3/src/run.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h> // Добавлено для waitpid

int main(int argc, char *argv[]) {
// Проверка на наличие достаточного количества аргументов
if (argc != 3) {
fprintf(stderr, "Usage: %s <seed> <arraysize>\n", argv[0]);
return 1;
}

// Запуск приложения sequential_min_max
pid_t pid = fork();
if (pid < 0) {
perror("Fork failed");
return 1; // Ошибка при создании процесса
} else if (pid == 0) {
// Дочерний процесс
execl("./sequential_min_max", "sequential_min_max", argv[1], argv[2], (char *)NULL);
perror("execl failed"); // Если execl вернется, произошла ошибка
return 1; // Ошибка при выполнении execl
} else {
// Родительский процесс
int status;
waitpid(pid, &status, 0); // Ожидание завершения дочернего процесса
if (WIFEXITED(status)) {
printf("sequential_min_max exited with status %d\n", WEXITSTATUS(status));
} else {
printf("sequential_min_max did not terminate normally\n");
}
}
return 0; // Успешное завершение программы
}
Binary file added lab3/src/sequential_min_max
Binary file not shown.
5 changes: 5 additions & 0 deletions lab3/src/sequential_min_max.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ int main(int argc, char **argv) {
int *array = malloc(array_size * sizeof(int));
GenerateArray(array, array_size, seed);
struct MinMax min_max = GetMinMax(array, 0, array_size);

for (int i = 0; i < array_size; i++){
printf("%d ", array[i]);
}

free(array);

printf("min: %d\n", min_max.min);
Expand Down
Binary file added lab3/src/sequential_min_max.o
Binary file not shown.
Binary file added lab3/src/utils.o
Binary file not shown.
24 changes: 24 additions & 0 deletions lab4/src/find_min_max.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "find_min_max.h"

#include <limits.h>

struct MinMax GetMinMax(int *array, unsigned int begin, unsigned int end) {
struct MinMax min_max;
min_max.min = INT_MAX;
min_max.max = INT_MIN;

for (int i = begin; i<end; i++){
if (array[i]>min_max.max){
min_max.max = array[i];
}
if (array[i]<min_max.min){
min_max.min = array[i];
}
}

// your code here
return min_max;
}



8 changes: 8 additions & 0 deletions lab4/src/find_min_max.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef FIND_MIN_MAX_H
#define FIND_MIN_MAX_H

#include "utils.h"

struct MinMax GetMinMax(int *array, unsigned int begin, unsigned int end);

#endif
Binary file added lab4/src/find_min_max.o
Binary file not shown.
Binary file added lab4/src/libsum.a
Binary file not shown.
Loading