init commit

This commit is contained in:
2025-04-13 21:48:15 +03:00
commit 23255e9121
192 changed files with 12200 additions and 0 deletions
+62
View File
@@ -0,0 +1,62 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/wait.h>
void
str_to_ll(char *str, long long *num)
{
errno = 0;
char *endptr;
*num = strtoll(str, &endptr, 10);
if (*endptr || errno || endptr == str) {
fprintf(stderr, "Invalid number: %s\n", str);
exit(1); // TODO: по-хорошему заменить на внешний обработчик ошибок
}
return;
}
int
main(int argc, char *argv[])
{
if (argc != 2) {
fprintf(stderr, "Usage: %s <deepth>\n", argv[0]);
return 1;
}
long long deepth;
str_to_ll(argv[1], &deepth);
// long long i = 1;
// while (deepth > 0) {
// printf("Deepth: %lld\n", i++);
// if (deepth == 1) {
// break;
// }
// if (!fork()) {
// deepth--;
// } else {
// wait(NULL);
// break;
// }
// }
for (int i = 1; i <= deepth; i++) {
if (i == deepth || fork() > 0) {
wait(NULL);
printf("Deepth: %d\n", i);
break;
}
}
return 0;
}
+58
View File
@@ -0,0 +1,58 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
void
str_to_ll(char *str, long long *num)
{
errno = 0;
char *endptr;
*num = strtoll(str, &endptr, 10);
if (*endptr || errno || endptr == str) {
fprintf(stderr, "Invalid number: %s\n", str);
exit(1); // TODO: по-хорошему заменить на внешний обработчик ошибок
}
return;
}
void
make_proc(int deepth, int max_deepth)
{
if (deepth == max_deepth) {
return;
}
printf("Deepth: %d\n", deepth);
pid_t pid = fork();
if (pid < 0) {
fprintf(stderr, "Fork failed\n");
exit(1);
} else if (pid == 0) {
make_proc(deepth + 1, max_deepth);
exit(0);
} else {
wait(NULL);
}
return;
}
int
main(int argc, char *argv[])
{
if (argc != 2) {
fprintf(stderr, "Usage: %s <deepth>\n", argv[0]);
return 1;
}
long long deepth;
str_to_ll(argv[1], &deepth);
make_proc(0, deepth);
return 0;
}
+52
View File
@@ -0,0 +1,52 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <string.h>
#include <linux/limits.h>
#include <sys/mman.h>
void
str_to_ll(char *str, long long *num)
{
errno = 0;
char *endptr;
*num = strtoll(str, &endptr, 10);
if (*endptr || errno || endptr == str) {
fprintf(stderr, "Invalid number: %s\n", str);
exit(1); // TODO: по-хорошему заменить на внешний обработчик ошибок
}
return;
}
int
main(int argc, char *argv[])
{
if (argc != 3) {
fprintf(stderr, "Usage: %s <deepth> <file>\n", argv[0]);
return 1;
}
long long deepth;
str_to_ll(argv[1], &deepth);
for (int i = 1; i <= deepth; i++) {
if (fork() == 0) {
int fd = open(argv[2], O_CREAT | O_WRONLY, 0666);
lseek(fd, (deepth - i) * sizeof(int), SEEK_SET);
write(fd, &i, sizeof(int));
close(fd);
return 0;
}
}
return 0;
}
+61
View File
@@ -0,0 +1,61 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <string.h>
#include <linux/limits.h>
#include <sys/mman.h>
#include <stdint.h>
void
str_to_ll(char *str, long long *num)
{
errno = 0;
char *endptr;
*num = strtoll(str, &endptr, 10);
if (*endptr || errno || endptr == str) {
fprintf(stderr, "Invalid number: %s\n", str);
exit(1); // TODO: по-хорошему заменить на внешний обработчик ошибок
}
return;
}
int
main(int argc, char *argv[])
{
if (argc != 3) {
fprintf(stderr, "Usage: %s <deepth> <file>\n", argv[0]);
return 1;
}
long long deepth;
str_to_ll(argv[1], &deepth);
truncate(argv[2], sizeof(int) * deepth);
int fd = open(argv[2], O_CREAT | O_RDWR, 0666);
int *addr = mmap(NULL, sizeof(int) * deepth, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
for (int i = 1; i <= deepth; i++) {
if (fork() == 0) {
addr[deepth - i] = i;
return 0;
}
}
// for (int i = 1; i <= deepth; i++) {
// wait(NULL);
// }
while (wait(NULL) != -1)
;
munmap(addr, sizeof(int) * argc);
return 0;
}