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

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

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

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

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

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

(МТУСИ)

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

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

на тему

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

Выполнил:

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

Группы М092201(75)

Юсифов Э.С.

Проверил:

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

Москва 2022

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

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

  1. #include <stdio.h>

  2. #include <time.h>

  3. #include <sys/types.h>

  4. #include <unistd.h>

  5. #include <stdlib.h>

  6.  

  7. int main(void)

  8. {

  9.     // Fork the first child process

  10. pid_t pid1 = fork();

  11.     ( (pid1 == 0)

  12.     {

  13.         // This is the first child process

  14.  

  15.         // Print the process ID and parent process ID

  16.         ,("First child process: PID %d, PPID %d"(, getpid(), getppid());

  17.  

  18.         // Print the current time

  19.         struct;

  20. clock_gettime(CLOCK_REALTIME, &ts);

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

  22.  

  23.         return 0;

  24.     }

  25.     ( if (pid1 < 0)

  26.     {

  27.         // Error forking the first child process

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

  29.         return 1;

  30.     }

  31.  

  32.     // Fork the second child process

  33. pid_t pid2 = fork();

  34.     ( (pid2 == 0)

  35.     {

  36.         // This is the second child process

  37.  

  38.         // Print the process ID and parent process ID

  39.         ,("Second child process: PID %d, PPID %d"(, getpid(), getppid());

  40.  

  41.         // Print the current time

  42.         struct;

  43. clock_gettime(CLOCK_REALTIME, &ts);

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

  45.  

  46.         return 0;

  47.     }

  48.     return 0;

  49. }

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

  1. #include <stdio.h>

  2. #include <pthread.h>

  3. #include <math.h>

  4.  

  5. // Global variables for storing the input and output

  6. int K, N, n;

  7. double y[1000];

  8.  

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

  10. * *calc_term(void *arg) {

  11.   int i = (int)arg;

  12.  

  13.   // Calculate the term of the Taylor series

  14.   double term = 1;

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

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

  17.   }

  18.  

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

  20. y[i] += term;

  21.  

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

  23.   ,("Thread %d: pid=%d, term=%lf"(, i, getpid(), term);

  24.  

  25.   return NULL;

  26. }

  27.  

  28. int main() {

  29.   // Read the input values from the user

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

  31.   &,"%d" ( &K);

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

  33.   &,"%d"( &N);

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

  35.   &,"%d"( &n);

  36.  

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

  38. pthread_t threads[K];

  39.  

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

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

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

  43.   }

  44.  

  45.   // Wait for all the threads to finish

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

  47.     pthread_join(threads[i], NULL);

  48.   }

  49.  

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

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

  52.   if (f == NULL) {

  53.     perror("Error opening file");

  54.     return 1;

  55.   }

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

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

  58.   }

  59.   fclose(f);

  60.  

  61.   return 0;

  62. }