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
+30
View File
@@ -0,0 +1,30 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <wait.h>
// cmd < file1 >> file2
int
main(int argc, char **argv)
{
char *file1 = argv[2];
char *file2 = argv[3];
char *cmd = argv[1];
if (!fork()) {
int fin = open(file1, O_RDONLY);
int fout = open(file2, O_WRONLY | O_APPEND | O_CREAT, 0666);
dup2(fin, 0);
dup2(fout, 1);
execlp(cmd, cmd, NULL);
_exit(1);
} else {
wait(NULL);
}
return 0;
}
+39
View File
@@ -0,0 +1,39 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <wait.h>
// cmd1 | cmd2
int
main(int argc, char **argv)
{
char *cmd1 = argv[1];
char *cmd2 = argv[2];
int fd[2];
pipe(fd);
if (!fork()) {
dup2(fd[1], 1);
close(fd[0]);
execlp(cmd1, cmd1, NULL);
_exit(1);
} else if (!fork()) {
dup2(fd[0], 0);
close(fd[1]);
execlp(cmd2, cmd2, NULL);
_exit(1);
} else {
close(fd[0]);
close(fd[1]);
}
while (wait(NULL) != -1)
;
return 0;
}
+63
View File
@@ -0,0 +1,63 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <wait.h>
int
main(int argc, char **argv)
{
int n = strtoll(argv[1], NULL, 10);
int fd1[2];
int fd2[2];
pipe(fd1);
pipe(fd2);
int tmp = 1;
write(fd2[1], &tmp, sizeof(tmp));
if (!fork()) {
int i = 0;
close(fd1[0]);
close(fd2[1]);
while (i < n) {
read(fd2[0], &i, sizeof(i));
if (i > n) {
break;
}
printf("Channel %d: %d\n", 1, i);
i++;
write(fd1[1], &i, sizeof(i));
}
return 0;
} else if (!fork()) {
int i = 0;
close(fd2[0]);
close(fd1[1]);
while (i < n) {
read(fd1[0], &i, sizeof(i));
if (i > n) {
break;
}
printf("Channel %d: %d\n", 2, i);
i++;
write(fd2[1], &i, sizeof(i));
}
return 0;
} else {
close(fd1[0]);
close(fd1[1]);
close(fd2[0]);
close(fd2[1]);
}
while (wait(NULL) != -1)
;
return 0;
}
+56
View File
@@ -0,0 +1,56 @@
#include <stdio.h>
#include <sys/types.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
int
main(int argc, char **argv)
{
key_t key = ftok("/dev/null", 0);
int semid = semget(key, 2, 0666 | IPC_CREAT);
int shmid = shmget(key, sizeof(int), 0666 | IPC_CREAT);
int *shm = shmat(shmid, NULL, 0);
pid_t pid = fork();
if (!pid) {
while (scanf("%d", shm) != -1) {
semop(semid, &(struct sembuf){.sem_num = 0, .sem_op = 1}, 1);
semop(semid, &(struct sembuf){.sem_num = 1, .sem_op = -1}, 1);
}
semctl(semid, 2, IPC_RMID, NULL);
exit(0);
} else if (pid == -1) {
exit(1);
}
pid = fork();
if (!pid) {
for (;;) {
if (semop(semid, &(struct sembuf){.sem_num = 0, .sem_op = -1}, 1) == -1) {
break;
}
printf("$ %d\n", *shm);
if (semop(semid, &(struct sembuf){.sem_num = 1, .sem_op = 1}, 1) == -1) {
break;
}
}
exit(0);
} else if (pid == -1) {
exit(1);
}
while (wait(NULL) != -1)
;
return 0;
}
+64
View File
@@ -0,0 +1,64 @@
#include <stdio.h>
#include <sys/types.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
int
main(int argc, char **argv)
{
key_t key = ftok("/dev/null", 0);
int semid = semget(key, 2, 0666 | IPC_CREAT);
int shmid = shmget(key, sizeof(int), 0666 | IPC_CREAT);
int *shm = shmat(shmid, NULL, 0);
semctl(semid, 0, SETVAL, 1);
semctl(semid, 1, SETVAL, 1);
pid_t pid = fork();
if (!pid) {
for (;;) {
semop(semid, &(struct sembuf){.sem_num = 0, .sem_op = -1}, 1);
semop(semid, &(struct sembuf){.sem_num = 1, .sem_op = -1}, 1);
int r = scanf("%d", shm);
if (r == -1) {
break;
}
semop(semid, &(struct sembuf){.sem_num = 0, .sem_op = 1}, 1);
}
semctl(semid, 2, IPC_RMID, NULL);
exit(0);
} else if (pid == -1) {
exit(1);
}
pid = fork();
if (!pid) {
for (;;) {
if (semop(semid, &(struct sembuf){.sem_num = 0, .sem_op = -1}, 1) == -1) {
break;
}
printf("$ %d\n", *shm);
if (semop(semid, &(struct sembuf){.sem_num = 0, .sem_op = 1}, 1) == -1) {
break;
}
if (semop(semid, &(struct sembuf){.sem_num = 1, .sem_op = 1}, 1) == -1) {
break;
}
}
exit(0);
} else if (pid == -1) {
exit(1);
}
while (wait(NULL) != -1)
;
return 0;
}
+25
View File
@@ -0,0 +1,25 @@
#include <stdio.h>
#include <sys/types.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
// пинг понг для n процессов
int
main(int argc, char **argv)
{
int n = (int) strtoll(argv[1], NULL, 10);
int max_n = (int) strtoll(argv[2], NULL, 10);
for (int i = 0; i < n; ++i) {
}
while (wait(NULL) != -1)
;
return 0;
}
+40
View File
@@ -0,0 +1,40 @@
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
int
main(int argc, char *argv[])
{
char *fin = argv[1];
char *fout = argv[2];
// printf("fdin: %d\n", fin);
int fdin = open(fin, O_RDONLY);
if (fdin == -1) {
return 1;
}
int fdout = open(fout, O_WRONLY | O_CREAT | O_TRUNC, 0777);
for (;;) {
char c[1024];
int r = read(fdin, c, 1024);
if (r == -1) {
return 1;
}
if (r == 0) {
break;
}
write(fdout, c, r);
}
close(fdin);
close(fdout);
return 0;
}
+52
View File
@@ -0,0 +1,52 @@
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
int
main(int argc, char *argv[])
{
int fdin = open("in", O_RDWR);
if (fdin == -1) {
return 1;
}
double num1, num2;
// Read the first double
if (read(fdin, &num1, sizeof(double)) != sizeof(double)) {
close(fdin);
return 1;
}
// Read the second double
if (read(fdin, &num2, sizeof(double)) != sizeof(double)) {
close(fdin);
return 1;
}
// Move the file pointer back to the beginning
if (lseek(fdin, 0, SEEK_SET) == -1) {
close(fdin);
return 1;
}
// Write the second double first
if (write(fdin, &num2, sizeof(double)) != sizeof(double)) {
close(fdin);
return 1;
}
// Write the first double second
if (write(fdin, &num1, sizeof(double)) != sizeof(double)) {
close(fdin);
return 1;
}
close(fdin);
return 0;
}
+27
View File
@@ -0,0 +1,27 @@
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
int main(int argc, char* argv[]) {
int fdout = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0600);
if (fdout == -1) {
return 1;
}
unsigned short x;
while (scanf("%hu", &x) == 1) {
unsigned char buf[2];
buf[0] = x >> 8;
buf[1] = x & 0xff;
write(fdout, &x, 2);
}
close(fdout);
return 0;
}
+35
View File
@@ -0,0 +1,35 @@
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
int
main(int argc, char *argv[])
{
int fdin = open(argv[1], O_RDONLY);
if (fdin == -1) {
return 1;
}
unsigned int res = 0, tmp;
unsigned char num[4]; // не пишем в int, т.к. неизвестно, какой порядок байтов
while (read(fdin, num, 4) == 4) {
// unsigned int tmp = (unsigned int) num << 24 | (unsigned int) num <<
// 16 |
// (unsigned int) num << 8 | (unsigned int) num;
for (int i = 0; i < 4; i++) {
tmp = tmp << 8 | num[i];
}
res += tmp;
}
printf("%u\n", res);
close(fdin);
return 0;
}
BIN
View File
Binary file not shown.
+1
View File
@@ -0,0 +1 @@
123 234
+51
View File
@@ -0,0 +1,51 @@
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/file.h>
#include <sys/types.h>
void
premission_to_char(char *m, int n, mode_t mode)
{
for (int i = 0; i < n; ++i) {
if (!(mode & 1)) {
m[n - i - 1] = '-';
}
mode >>= 1;
}
}
int
main(int argc, char *argv[])
{
struct stat st;
for (int i = 1; i < argc; i++) {
if (lstat(argv[i], &st) < 0) {
continue;
}
char type;
if (S_ISREG(st.st_mode)) {
type = '-';
} else if (S_ISDIR(st.st_mode)) {
type = 'd';
} else if (S_ISLNK(st.st_mode)) {
type = 'l';
} else {
type = '?';
}
char m[] = "rwxrwxrwx";
for (int j = 0; j < 9; j++) {
if (!((st.st_mode >> j) & 1)) {
m[8 - j] = '-';
}
}
printf("%c%s %lu %hu %lu %ld %s\n", type, m, st.st_nlink, st.st_uid, st.st_size, st.st_ctime, argv[i]);
}
return 0;
}
+76
View File
@@ -0,0 +1,76 @@
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/file.h>
#include <sys/types.h>
int
acs(const char *name, int mode)
{
struct stat st;
if (lstat(name, &st) < 0) {
return -1;
}
uid_t uid = getuid();
if (uid == 0) {
return 0;
}
uid_t st_mode = st.st_mode;
if (st.st_mode == uid) {
st_mode >>= 6;
} else if (st.st_gid == getgid()) {
st_mode >>= 3;
}
st_mode &= 7;
// for (int i = 0; i < 3; ++i) {
// if (st_mode ^ mode & 1) {
// return -1;
// }
// st_mode >>= 1;
// mode >>= 1;
// }
if (st_mode & mode == mode) {
return 0;
}
return -1;
}
int
main(int argc, char *argv[])
{
struct stat st;
for (int i = 1; i < argc; i++) {
if (lstat(argv[i], &st) < 0) {
continue;
}
char type;
if (S_ISREG(st.st_mode)) {
type = '-';
} else if (S_ISDIR(st.st_mode)) {
type = 'd';
} else if (S_ISLNK(st.st_mode)) {
type = 'l';
} else {
type = '?';
}
char m[] = "rwxrwxrwx";
for (int j = 0; j < 9; j++) {
if (!((st.st_mode >> j) & 1)) {
m[8 - j] = '-';
}
}
printf("%c%s %lu %hu %lu %ld %s\n", type, m, st.st_nlink, st.st_uid, st.st_size, st.st_ctime, argv[i]);
}
return 0;
}
+39
View File
@@ -0,0 +1,39 @@
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/file.h>
#include <sys/types.h>
#include <string.h>
int
main(int argc, char *argv[])
{
struct stat st;
if (lstat(argv[1], &st) < 0) {
exit(1);
}
if (S_ISDIR(st.st_mode)) {
char *file = calloc(strlen(argv[1]) + 30, sizeof(char));
snprintf(file, strlen(argv[1]) + 30, "%s/%s.uid", argv[1], argv[0]);
sprintf(file, "%s/%s.uid", argv[1], argv[0]);
int fd = open(file, O_WRONLY | O_CREAT | O_TRUNC);
uid_t uid = getuid();
write(fd, &uid, sizeof(uid_t));
close(fd);
} else if (S_ISREG(st.st_mode)) {
int fd = open(argv[1], O_WRONLY | O_TRUNC);
write(fd, st.st_uid, sizeof(uid_t));
close(fd);
} else {
exit(1);
}
}
+37
View File
@@ -0,0 +1,37 @@
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/file.h>
#include <sys/types.h>
#include <string.h>
#include <linux/limits.h>
int
main(int argc, char *argv[])
{
DIR *dir = opendir(argv[1]);
unsigned long long sum = 0;
struct dirent *de;
while (de = readdir(dir)) {
char *name = calloc(PATH_MAX, sizeof(char));
snprintf(name, PATH_MAX, "%s/%s", argv[1], de->d_name);
struct stat st;
if (lstat(name, &st) < 0) {
continue;
}
if (S_ISREG(st.st_mode)) {
sum += st.st_size;
}
}
closedir(dir);
printf("%d\n", sum);
return 0;
}
+42
View File
@@ -0,0 +1,42 @@
#include <stdio.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/file.h>
#include <sys/types.h>
#include <string.h>
#include <linux/limits.h>
#include <sys/stat.h>
int
main(int argc, char *argv[])
{
DIR *dir = opendir(argv[1]);
struct dirent *de;
while (de = readdir(dir)) {
char *name = calloc(PATH_MAX, sizeof(char));
snprintf(name, PATH_MAX, "%s/%s", argv[1], de->d_name);
struct stat st;
if (lstat(name, &st) < 0) {
continue;
}
if (!(S_ISREG(st.st_mode))) {
continue;
}
if (access(name, 3)) {
continue;
}
int len = strlen(name);
if (name[len - 1] == '~' || (len >= 4 && strcmp(name + strlen(name) - 4, ".bak") == 0)) {
unlink(name);
}
}
closedir(dir);
return 0;
}
+66
View File
@@ -0,0 +1,66 @@
// подкаталог пустой
// совпадают айдишники
// есть права у остальных на запись
// rmdir
#include <stdio.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/file.h>
#include <sys/types.h>
#include <string.h>
#include <linux/limits.h>
#include <sys/stat.h>
int
main(int argc, char *argv[])
{
DIR *dir = opendir(argv[1]);
struct dirent *de;
while (de = readdir(dir)) {
if ((de->d_name[0] == '.' && strlen(de->d_name) == 1) ||
(strlen(de->d_name) == 2) && !strcmp(de->d_name, "..")) {
continue;
}
char *name = calloc(PATH_MAX, sizeof(char));
snprintf(name, PATH_MAX, "%s/%s", argv[1], de->d_name);
struct stat st;
if (lstat(name, &st) < 0) {
continue;
}
if (!(S_ISDIR(st.st_mode))) {
continue;
}
if (st.st_uid != getuid()) {
continue;
}
if (!(st.st_mode & 2)) {
continue;
}
int num_files = 0;
DIR *dirt = opendir(name);
while (readdir(dirt)) {
num_files++;
if (num_files > 2) {
break;
}
}
if (num_files > 2) {
continue;
}
closedir(dirt);
rmdir(name);
}
closedir(dir);
return 0;
}
+50
View File
@@ -0,0 +1,50 @@
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/file.h>
#include <sys/types.h>
#include <string.h>
#include <dirent.h>
#include <linux/limits.h>
struct Files
{
char *pathname;
__ino_t st_ino;
__dev_t st_dev;
};
int
main(int argc, char **argv)
{
struct stat st;
if (lstat(".", &st) == -1) {
printf("stat error\n");
return 1;
}
DIR *dir = opendir("..");
struct dirent *de;
while ((de = readdir(dir))) {
if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
continue;
}
struct stat sttmp;
char buf[PATH_MAX];
snprintf(buf, PATH_MAX, "%s/%s", "..", de->d_name);
if (lstat(buf, &sttmp) == -1) {
printf("lstat error\n");
return 1;
}
printf("%s\n", de->d_name);
if (sttmp.st_dev == st.st_dev && sttmp.st_ino == st.st_ino) {
printf("%s\n", de->d_name);
}
}
return 0;
}
+33
View File
@@ -0,0 +1,33 @@
#include <time.h>
#include <stdio.h>
void
f(int *y, int *m, int *d, int offset)
{ // offset in seconds
struct tm t = {0};
// struct tm* tptr = calloc(1, sizeof(struct tm));
t.tm_isdst = -1;
t.tm_year = *y - 1900;
t.tm_mon = *m - 1;
t.tm_mday = *d;
time_t time = mktime(&t);
time += offset;
localtime_r(&time, &t);
*y = t.tm_year + 1900;
*m = t.tm_mon + 1;
*d = t.tm_mday;
return;
}
int
main(void)
{
int y = 2021;
int m = 1;
int d = 1;
f(&y, &m, &d, 65 * 60 * 60 * 24);
printf("%d-%d-%d\n", y, m, d);
return 0;
}
+45
View File
@@ -0,0 +1,45 @@
#include <time.h>
#include <stdio.h>
#include <stdlib.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;
}
int
main(int argc, char **argv)
{
long long year, day, dw, res = 0;
str_to_ll(argv[1], &year);
str_to_ll(argv[2], &day);
str_to_ll(argv[3], &dw);
struct tm t = {0};
t.tm_isdst = -1;
t.tm_mday = 1;
t.tm_year = year - 1900;
while (t.tm_year == year - 1900) {
mktime(&t);
if (t.tm_mday == day && dw == t.tm_wday) {
res++;
}
t.tm_mday++;
}
printf("%d\n", (int) res);
return 0;
}
+47
View File
@@ -0,0 +1,47 @@
#include <time.h>
#include <stdio.h>
#include <stdlib.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;
}
int
main(int argc, char **argv)
{
long long year, mth, res = 0;
str_to_ll(argv[1], &year);
str_to_ll(argv[2], &mth);
struct tm t = {0};
t.tm_isdst = -1;
t.tm_mday = 1;
t.tm_mon = mth - 1;
t.tm_year = year - 1900;
while (t.tm_mon == mth - 1) {
if (t.tm_wday > 0 && t.tm_wday < 5) {
res += 8;
} else if (t.tm_wday == 5) {
res += 6;
}
t.tm_mday++;
mktime(&t);
}
printf("%d\n", (int) res);
return 0;
}
+27
View File
@@ -0,0 +1,27 @@
#include <stdio.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
int
main(int argc, char *argv[])
{
int fd = open(argv[1], O_RDWR | O_CREAT, 0666);
struct stat st;
lseek(fd, 0, SEEK_END);
stat(argv[1], &st);
char *addr = mmap(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
for (int i = 0; i < st.st_size / 2; i++) {
char tmp = addr[i];
addr[i] = addr[st.st_size - i - 1];
addr[st.st_size - i - 1] = tmp;
}
close(fd);
munmap(addr, st.st_size);
return 0;
}
+29
View File
@@ -0,0 +1,29 @@
#include <stdio.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int
main(int argc, char *argv[])
{
int fdin = open(argv[1], O_RDONLY);
int fdout = open(argv[2], O_RDWR | O_CREAT, 0666);
off_t len = lseek(fdin, 0, SEEK_END);
char *fin = mmap(NULL, len, PROT_READ, MAP_SHARED, fdin, 0);
ftruncate(fdout, len);
char *fout = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fdout, 0);
memcpy(fout, fin, len);
close(fdin);
close(fdout);
munmap(fin, len);
munmap(fout, len);
return 0;
}
+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;
}
+52
View File
@@ -0,0 +1,52 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <wait.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;
}
int
main(int argc, char *argv[])
{
for (int i = 0; i < 3; ++i) {
pid_t pid = fork();
if (pid == -1) {
return 1;
}
if (pid == 0) {
char buf[8] = {0};
read(0, buf, 7);
// long long t;
// str_to_ll(buf, &t);
int t = (int) strtol(buf, NULL, 10);
printf("%d\n", t * t);
return 0;
} else {
continue;
}
}
while (wait(NULL) != -1)
;
return 0;
}
+41
View File
@@ -0,0 +1,41 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <wait.h>
#include <errno.h>
// 8-ми сегментная таблица сегментов
// 32-х разрядная система
typedef struct
{
int size;
int base;
} segment;
unsigned int
f(segment *segtable, unsigned int VAdr)
{
int bit = 3;
unsigned int segnum = VAdr >> (32 - bit);
unsigned int segoff = VAdr & ((1 << (32 - bit)) - 1);
int addr = segtable[segnum].base + segoff;
int size = segtable[segnum].size;
if (segoff >= size) {
return 25;
// exit(1);
}
return addr;
}
int
main(int argc, char *argv[])
{
segment segtable[8] = {};
return 0;
}
+37
View File
@@ -0,0 +1,37 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <wait.h>
#include <errno.h>
// инвертированная сегментная таблица сегментов
// 32-х разрядная система
typedef struct
{
pid_t pid;
unsigned int VirtNum;
} page_record;
unsigned int
f(page_record *pagetable, unsigned int VAdr, int size_segtable)
{
pid_t pid = getpid();
unsigned int pagenum = VAdr >> (12); // 12 - размер страницы
for (int i = 0; i < size_segtable; i++) {
if (pagetable[i].VirtNum == pagenum && pagetable[i].pid == pid) {
return i << (32 - 12) + (VAdr << 20 >> 20);
}
}
return -1;
}
int
main(int argc, char *argv[])
{
return 0;
}
+16
View File
@@ -0,0 +1,16 @@
// по номеру начального размер блока и указатель на таблицу файлов
// вывести размер файла
unsigned int
f(unsigned int n, unsigned int *table)
{
unsigned int size = 0;
int first_block = table[n];
while (first_block) {
size++; // размер блока
first_block = table[first_block];
}
return size;
}
+3
View File
@@ -0,0 +1,3 @@
000007
000010
000009