Лаба_2_Док_Файл-1
.docxМинистерство цифрового развития, связи и массовых коммуникаций Российской Федерации
Ордена Трудового Красного Знамени федеральное государственное бюджетное образовательное учреждение высшего образования
«Московский технический университет связи и информатики»
(МТУСИ)
Кафедра «Математическая кибернетика и информационные технологии»
Лабораторная работа №2
на тему
«Процессы в ОС Linux»
Выполнил:
Студент 1 курса магистратуры
Группы М092201(75)
Юсифов Э.С.
Проверил:
Симонов Сергей Евгеньевич
Москва 2022
Написать программу, создающую два дочерних процесса с
использованием двух вызовов 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();
( (pid1 == 0)
{
// This is the first child process
// Print the process ID and parent process ID
,("First child process: PID %d, PPID %d"(, getpid(), getppid());
// Print the current time
struct;
clock_gettime(CLOCK_REALTIME, &ts);
,("Current time: %ld:%ld:%ld:%ld" (, ts.tv_sec / 3600, (ts.tv_sec / 60) % 60, ts.tv_sec % 60, ts.tv_nsec / 1000000);
return 0;
}
( 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();
( (pid2 == 0)
{
// This is the second child process
// Print the process ID and parent process ID
,("Second child process: PID %d, PPID %d"(, getpid(), getppid());
// Print the current time
struct;
clock_gettime(CLOCK_REALTIME, &ts);
,("Current time: %ld:%ld:%ld:%ld" (, ts.tv_sec / 3600, (ts.tv_sec / 60) % 60, ts.tv_sec % 60, ts.tv_nsec / 1000000);
return 0;
}
return 0;
}
Индивидуальное задание
#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
* *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
,("Thread %d: pid=%d, term=%lf"(, i, getpid(), term);
return NULL;
}
int main() {
// Read the input values from the user
printf("Enter the value of K: ");
&,"%d" ( &K);
printf("Enter the value of N: ");
&,"%d"( &N);
printf("Enter the number of terms in the Taylor series: ");
&,"%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;
}