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
Executable
+54
View File
@@ -0,0 +1,54 @@
#include <stdio.h>
#include <unistd.h>
#include <wait.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/file.h>
int main(int argc, char** argv){
int id_of_shm = shmget(argv[1], sizeof(double) * 2, IPC_CREAT | 0666);
float* start_of_shm = shmat(id_of_shm, NULL, 0);
int sem_id = semget(IPC_PRIVATE, 1, IPC_CREAT | 0666);
semctl(sem_id, 0, SETVAL, 1);
struct sembuf p = {0, -1, 0}, v = {0,1,0};
for(int i = 2; i < argc; i++){
pid_t pid = fork();
if (pid == 0) {
FILE* in_file = fopen(argv[i], "r");
float sum = 0, n = 0;
float buf;
while(fscanf(in_file, "%f", &buf) != EOF){
sum += buf;
n++;
}
semop(sem_id, &p, 1);
start_of_shm[1] += sum;
start_of_shm[0] += n;
semop(sem_id, &v, 1);
fclose(in_file);
return 0;
}
}
while (wait(NULL) != -1 ){
}
printf("%f\n", start_of_shm[1] / start_of_shm[0]);
semctl(sem_id, 0, IPC_RMID);
shmctl(id_of_shm, IPC_RMID, NULL);
return 0;
}
Executable
+41
View File
@@ -0,0 +1,41 @@
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
volatile int counter = 0;
volatile int signals = 0;
int fd2;
void handler(int sig) {
++signals;
if (signals < 3) {
printf("%d\n", counter / 2);
} else {
lseek(fd2, 0, SEEK_SET);
char buf;
while (read(fd2, &buf, 1) > 0) {
write(1, &buf, 1);
}
exit(0);
}
}
int main(int argc, char** argv) {
signal(SIGINT, handler);
int fd1 = open(argv[1], O_RDONLY, 0666);
fd2 = open(argv[2], O_CREAT | O_WRONLY);
char buf;
while (read(fd1, &buf, 1) > 0) {
if (counter % 2 == 0) {
write(fd2, &buf, 1);
}
++counter;
}
return 0;
}
Executable
+39
View File
@@ -0,0 +1,39 @@
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <string.h>
int
main(int argc, char **argv)
{
int fd[2];
pipe(fd);
if (!fork()) {
close(fd[1]);
dup2(fd[0], 0);
execlp("wc", "wc", NULL);
_exit(1);
}
close(fd[0]);
int file = open(argv[1], O_RDONLY);
char test = argv[2][0];
char buf[8];
while (read(file, buf, 8) == 8) {
if (buf[0] != test) {
continue;
}
printf("%s\n", buf);
write(fd[1], buf, 8);
}
close(fd[1]);
while (wait(NULL) != -1)
;
return 0;
}
Executable
+34
View File
@@ -0,0 +1,34 @@
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <string.h>
int
main(int argc, char **argv)
{
int file = open(argv[1], O_RDONLY);
int file_out = open("tmp", O_WRONLY | O_CREAT | O_TRUNC, 0666);
dup2(file, 0);
dup2(file_out, 1);
char buf[101];
while (fgets(buf, 100, stdin) != NULL) {
for (int i = 0; i < strlen(buf); i++) {
if (!strncmp(buf + i, "begin", 5)) {
printf("{");
i += 4;
} else if (!strncmp(buf + i, "end", 3)) {
printf("}");
i += 2;
} else {
printf("%c", buf[i]);
}
}
}
rename("tmp", argv[1]);
return 0;
}
Executable
+33
View File
@@ -0,0 +1,33 @@
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <string.h>
int
main(int argc, char **argv)
{
int mas[5] = {0, 0, 0, 0, 0};
int num = 5, tmp;
while (scanf("%d", &tmp) == 1) {
for (int i = 0; i < 4; i++) {
mas[i] = mas[i + 1];
}
mas[4] = tmp;
num--;
}
// if (num < 0) {
// num = 0;
// }
num = num < 0 ? 0 : num;
for (int i = num; i < 5; i++) {
printf("%d ", mas[i]);
}
printf("\n");
return 0;
}
+13
View File
@@ -0,0 +1,13 @@
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <string.h>
#include <signal.h>
int
main(int argc, char **argv)
{
return 0;
}