Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Лаба_2_Док_Файл

.docx
Скачиваний:
0
Добавлен:
14.12.2022
Размер:
40.96 Кб
Скачать

Министерство цифрового развития, связи и массовых коммуникаций Российской Федерации

Ордена Трудового Красного Знамени федеральное государственное бюджетное образовательное учреждение высшего образования

«Московский технический университет связи и информатики»

(МТУСИ)

Кафедра «Математическая кибернетика и информационные технологии»

Лабораторная работа №2

на тему

«Процессы в ОС Linux»

Выполнил:

Студент 1 курса магистратуры

Группы М092201(75)

Францев Артем

Проверил:

Симонов Сергей Евгеньевич

Москва 2022

  1. Написать программу, создающую два дочерних процесса с

использованием двух вызовов fork().

#include <stdio.h>

#include <time.h>

#include <sys/types.h>

#include <unistd.h>

#include <stdlib.h>

 

int main(void)

{

    // Fork the first child process

    pid_t pid1 = fork();

    if (pid1 == 0)

    {

        // This is the first child process

 

        // Print the process ID and parent process ID

        printf("First child process: PID %d, PPID %d\n", getpid(), getppid());

 

        // Print the current time

        struct timespec ts;

        clock_gettime(CLOCK_REALTIME, &ts);

        printf("Current time: %ld:%ld:%ld:%ld\n", ts.tv_sec / 3600, (ts.tv_sec / 60) % 60, ts.tv_sec % 60, ts.tv_nsec / 1000000);

 

        return 0;

    }

    else if (pid1 < 0)

    {

        // Error forking the first child process

        printf("Error forking first child process.\n");

        return 1;

    }

 

    // Fork the second child process

    pid_t pid2 = fork();

    if (pid2 == 0)

    {

        // This is the second child process

 

        // Print the process ID and parent process ID

        printf("Second child process: PID %d, PPID %d\n", getpid(), getppid());

 

        // Print the current time

        struct timespec ts;

        clock_gettime(CLOCK_REALTIME, &ts);

        printf("Current time: %ld:%ld:%ld:%ld\n", ts.tv_sec / 3600, (ts.tv_sec / 60) % 60, ts.tv_sec % 60, ts.tv_nsec / 1000000);

 

        return 0;

    }

    return 0;

}

  1. Индивидуальное задание

#include <stdio.h>

#include <pthread.h>

#include <math.h>

 

// Global variables for storing the input and output

int K, N, n;

double y[1000];

 

// Function for calculating a single term of the Taylor series

void *calc_term(void *arg) {

  int i = (int)arg;

 

  // Calculate the term of the Taylor series

  double term = 1;

  for (int j = 1; j <= n; j++) {

    term = -term * (2*i*M_PI/N) * (2*i*M_PI/N) / (2*j*(2*j+1));

  }

 

  // Update the global y[i] value with the calculated term

  y[i] += term;

 

  // Print the thread's pid and the calculated term

  printf("Thread %d: pid=%d, term=%lf\n", i, getpid(), term);

 

  return NULL;

}

 

int main() {

  // Read the input values from the user

  printf("Enter the value of K: ");

  scanf("%d", &K);

  printf("Enter the value of N: ");

  scanf("%d", &N);

  printf("Enter the number of terms in the Taylor series: ");

  scanf("%d", &n);

 

  // Create an array of threads for calculating each term of the Taylor series

  pthread_t threads[K];

 

  // Launch a separate thread for calculating each term of the Taylor series

  for (int i = 0; i < K; i++) {

    pthread_create(&threads[i], NULL, calc_term, (void*)i);

  }

 

  // Wait for all the threads to finish

  for (int i = 0; i < K; i++) {

    pthread_join(threads[i], NULL);

  }

 

  // Write the resulting values of y[i] to a file

  FILE *f = fopen("output.txt", "w");

  if (f == NULL) {

    perror("Error opening file");

    return 1;

  }

  for (int i = 0; i < K; i++) {

    fprintf(f, "%lf\n", y[i]);

  }

  fclose(f);

 

  return 0;

}