Compare commits
1 Commits
os/lab7
...
os/lab-lib
| Author | SHA1 | Date | |
|---|---|---|---|
| 0aed24179b |
22
OSs/lab-libs/Makefile
Normal file
22
OSs/lab-libs/Makefile
Normal file
@ -0,0 +1,22 @@
|
||||
all: dynamic static
|
||||
|
||||
dynamic: main.o lib.so
|
||||
gcc $^ -o $@ $(CFLAGS) -lm
|
||||
|
||||
static: main.o lib.a
|
||||
gcc $^ -o $@ $(CFLAGS) -lm
|
||||
|
||||
lib.so: lib.o
|
||||
gcc -shared $^ -o $@ $(CFLAGS)
|
||||
|
||||
lib.a: lib.o
|
||||
ar r $@ $^
|
||||
|
||||
main.o: main.c
|
||||
gcc -c $^ -o $@ $(CFLAGS)
|
||||
|
||||
lib.o: lib.c
|
||||
gcc -c -o $@ -fPIC $^ $(CFLAGS)
|
||||
|
||||
clean:
|
||||
rm -f dynamic static *.so *.o
|
||||
45
OSs/lab-libs/lib.c
Normal file
45
OSs/lab-libs/lib.c
Normal file
@ -0,0 +1,45 @@
|
||||
#include "lib.h"
|
||||
|
||||
#include <math.h>
|
||||
|
||||
#define ZEROIN_MAX_ITERATIONS 1000
|
||||
|
||||
size_t fibo(size_t n)
|
||||
{
|
||||
size_t prev = 0, cur = 1, tmp = 0;
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
tmp = cur;
|
||||
cur = cur + prev;
|
||||
prev = tmp;
|
||||
}
|
||||
return cur;
|
||||
}
|
||||
|
||||
double zeroin(double (*f)(double), double err, double a, double b)
|
||||
{
|
||||
double left = f(a);
|
||||
if (fabs(left) < err) return left;
|
||||
double right = f(b);
|
||||
if (fabs(right) < err) return right;
|
||||
if (left * right > 0) {
|
||||
return NAN;
|
||||
}
|
||||
|
||||
double x = NAN;
|
||||
double cur = NAN;
|
||||
size_t n = 0;
|
||||
do {
|
||||
x = (a + b) / 2;
|
||||
cur = f(x);
|
||||
if (cur * left > 0) {
|
||||
left = cur;
|
||||
a = x;
|
||||
}
|
||||
else {
|
||||
right = cur;
|
||||
b = x;
|
||||
}
|
||||
n++;
|
||||
} while (fabs(cur) > err || n < ZEROIN_MAX_ITERATIONS);
|
||||
return x;
|
||||
}
|
||||
21
OSs/lab-libs/lib.h
Normal file
21
OSs/lab-libs/lib.h
Normal file
@ -0,0 +1,21 @@
|
||||
#ifndef LIB_H_
|
||||
#define LIB_H_
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
/**
|
||||
* Это будет просто какая-то не очень умная библиотечка с двумя функциями,
|
||||
* просто чтобы их типа собрать
|
||||
*/
|
||||
|
||||
/**
|
||||
* Считает n-ное число фибоначи
|
||||
*/
|
||||
size_t fibo(size_t n);
|
||||
|
||||
/**
|
||||
* Считает ноль функции на интервале с указанной погрешностью
|
||||
*/
|
||||
double zeroin(double (*f)(double), double err, double a, double b);
|
||||
|
||||
#endif
|
||||
17
OSs/lab-libs/main.c
Normal file
17
OSs/lab-libs/main.c
Normal file
@ -0,0 +1,17 @@
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include "lib.h"
|
||||
|
||||
#define FIBO_NUM 15
|
||||
|
||||
#define SIN_A 1.6
|
||||
#define SIN_B 3.7
|
||||
#define ZEROIN_ERR 0.001
|
||||
|
||||
int main()
|
||||
{
|
||||
printf("fibonachi number under index %u: %zu\n", FIBO_NUM, fibo(FIBO_NUM));
|
||||
printf("zero of sin(x) in interval (%lf %lf) is %lf\n",
|
||||
SIN_A, SIN_B, zeroin(sin, ZEROIN_ERR, SIN_A, SIN_B));
|
||||
return 0;
|
||||
}
|
||||
@ -6,12 +6,19 @@
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/msg.h>
|
||||
#define MQ_KEY1 1234L
|
||||
#define MQ_KEY2 2345L
|
||||
#define MAXMESGDATA 4096
|
||||
|
||||
#include "common.h"
|
||||
struct mymesg {
|
||||
long mesg_len;
|
||||
long mesg_type;
|
||||
char mesg_data[MAXMESGDATA];
|
||||
};
|
||||
|
||||
void client(int readid, int writeid);
|
||||
ssize_t mesg_send(int id, struct msg *mptr);
|
||||
ssize_t mesg_recv(int id, struct msg *mptr);
|
||||
ssize_t mesg_send(int id, struct mymesg *mptr);
|
||||
ssize_t mesg_recv(int id, struct mymesg *mptr);
|
||||
|
||||
int main(int argc,char **argv)
|
||||
{
|
||||
@ -50,7 +57,7 @@ void client(int readid, int writeid)
|
||||
{
|
||||
size_t len;
|
||||
ssize_t n;
|
||||
struct msg ourmesg;
|
||||
struct mymesg ourmesg;
|
||||
|
||||
printf("Client:readid=%d writeid=%d\n",readid,writeid);
|
||||
printf("Input file name, please\n");
|
||||
@ -74,18 +81,18 @@ void client(int readid, int writeid)
|
||||
write(1,ourmesg.mesg_data, n);
|
||||
}
|
||||
|
||||
ssize_t mesg_send(int id, struct msg *mptr)
|
||||
ssize_t mesg_send(int id, struct mymesg *mptr)
|
||||
{
|
||||
return(msgsnd(id, &(mptr->mesg_type),mptr->mesg_len, 0));
|
||||
}
|
||||
|
||||
ssize_t mesg_recv(int id, struct msg *mptr)
|
||||
ssize_t mesg_recv(int id, struct mymesg *mptr)
|
||||
{
|
||||
ssize_t n;
|
||||
|
||||
n=msgrcv(id, &(mptr->mesg_type),MAXMESGDATA,mptr->mesg_type, 0);
|
||||
mptr->mesg_len=n;
|
||||
printf("Client: n=%ld\n",n);
|
||||
printf("Client: n=%d\n",n);
|
||||
|
||||
return(n);
|
||||
}
|
||||
|
||||
@ -1,14 +0,0 @@
|
||||
#ifndef COMMON_H_
|
||||
#define COMMON_H_
|
||||
|
||||
#define MQ_KEY1 1234L
|
||||
#define MQ_KEY2 2345L
|
||||
#define MAXMESGDATA 4096
|
||||
|
||||
struct msg {
|
||||
long mesg_len;
|
||||
long mesg_type;
|
||||
char mesg_data[MAXMESGDATA];
|
||||
};
|
||||
|
||||
#endif
|
||||
@ -6,12 +6,19 @@
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/msg.h>
|
||||
#define MQ_KEY1 1234L
|
||||
#define MQ_KEY2 2345L
|
||||
#define MAXMESGDATA 4096
|
||||
|
||||
#include "common.h"
|
||||
struct mymesg {
|
||||
long mesg_len;
|
||||
long mesg_type;
|
||||
char mesg_data[MAXMESGDATA];
|
||||
};
|
||||
|
||||
void server(int readid, int writeid);
|
||||
ssize_t mesg_send(int id, struct msg *mptr);
|
||||
ssize_t mesg_recv(int id, struct msg *mptr);
|
||||
ssize_t mesg_send(int id, struct mymesg *mptr);
|
||||
ssize_t mesg_recv(int id, struct mymesg *mptr);
|
||||
|
||||
int main(int argc,char **argv)
|
||||
{
|
||||
@ -20,11 +27,25 @@ int main(int argc,char **argv)
|
||||
|
||||
printf("Server: Hello tvk!\n");
|
||||
|
||||
/*
|
||||
if((key1=ftok("/home/tvk/IPC/input.txt",'A'))<0)
|
||||
{
|
||||
printf("Server: can not get key!\n"); exit(1);
|
||||
}
|
||||
printf("key1=%x\n",key1);
|
||||
*/
|
||||
if((readid=msgget(MQ_KEY1, 0666|IPC_CREAT)) < 0)
|
||||
{
|
||||
printf("Server: can not get readid!\n"); exit(1);
|
||||
}
|
||||
printf("Server: readid=%d\n",readid);
|
||||
/*
|
||||
if((key2=ftok("/home/tvk/IPC/server_msg.c",'B'))<0)
|
||||
{
|
||||
printf("Server: can not get key!\n"); exit(1);
|
||||
}
|
||||
printf("key2=%x\n",key2);
|
||||
*/
|
||||
if((writeid=msgget(MQ_KEY2, 0666|IPC_CREAT)) < 0)
|
||||
{
|
||||
printf("Server: can not get readid!\n"); exit(1);
|
||||
@ -40,7 +61,7 @@ void server(int readid, int writeid)
|
||||
{
|
||||
FILE *fp;
|
||||
ssize_t n;
|
||||
struct msg ourmesg;
|
||||
struct mymesg ourmesg;
|
||||
|
||||
printf("Server:readid=%d writeid=%d\n",readid,writeid);
|
||||
|
||||
@ -76,12 +97,12 @@ void server(int readid, int writeid)
|
||||
mesg_send(writeid,&ourmesg);
|
||||
}
|
||||
|
||||
ssize_t mesg_send(int id, struct msg *mptr)
|
||||
ssize_t mesg_send(int id, struct mymesg *mptr)
|
||||
{
|
||||
return(msgsnd(id, &(mptr->mesg_type),mptr->mesg_len, 0));
|
||||
}
|
||||
|
||||
ssize_t mesg_recv(int id, struct msg *mptr)
|
||||
ssize_t mesg_recv(int id, struct mymesg *mptr)
|
||||
{
|
||||
ssize_t n;
|
||||
n=msgrcv(id, &(mptr->mesg_type),MAXMESGDATA,mptr->mesg_type, 0);
|
||||
|
||||
@ -1,15 +0,0 @@
|
||||
targets = server client
|
||||
all: $(targets)
|
||||
|
||||
server: server.o
|
||||
$(CC) -o $@ $^ $(CFLAGS)
|
||||
|
||||
client: client.o
|
||||
$(CC) -o $@ $^ $(CFLAGS)
|
||||
|
||||
%.o: %.c
|
||||
$(CC) -c $^ -o $@ $(CFLAGS)
|
||||
|
||||
clean:
|
||||
rm -f $(targets)
|
||||
rm -f *.o
|
||||
@ -1,65 +0,0 @@
|
||||
#include "common.h"
|
||||
#include <fcntl.h>
|
||||
#include <semaphore.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define FAIL_TO_CLEANUP(cond, jmp_to) \
|
||||
if (cond) { \
|
||||
perror(#cond); \
|
||||
ret = EXIT_FAILURE; \
|
||||
goto jmp_to; \
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
int ret = 0;
|
||||
int shm_fd = -1;
|
||||
void *shm_ptr = MAP_FAILED;
|
||||
sem_t *sem_c2s = SEM_FAILED;
|
||||
sem_t *sem_s2c = SEM_FAILED;
|
||||
|
||||
FAIL_TO_CLEANUP((shm_fd = shm_open(SHM_NAME, O_RDWR, 0)) == -1, exit);
|
||||
FAIL_TO_CLEANUP(
|
||||
(shm_ptr = mmap(NULL, SHM_SIZE,
|
||||
PROT_READ | PROT_WRITE,
|
||||
MAP_SHARED, shm_fd, 0)) == MAP_FAILED,
|
||||
unmap_shared_mem);
|
||||
|
||||
FAIL_TO_CLEANUP((sem_c2s = sem_open(SEM_C2S_NAME, 0)) == SEM_FAILED,
|
||||
close_sem_c2s);
|
||||
FAIL_TO_CLEANUP((sem_s2c = sem_open(SEM_S2C_NAME, 0)) == SEM_FAILED,
|
||||
close_sem_s2c);
|
||||
|
||||
char filename[SHM_SIZE];
|
||||
FAIL_TO_CLEANUP(fgets(filename, sizeof(filename), stdin) == NULL, close_sem_s2c);
|
||||
|
||||
size_t ln = strlen(filename);
|
||||
if (ln > 0 && filename[ln - 1] == '\n')
|
||||
filename[ln - 1] = '\0';
|
||||
|
||||
memset(shm_ptr, 0, SHM_SIZE);
|
||||
strncpy((char *)shm_ptr, filename, SHM_SIZE - 1);
|
||||
|
||||
FAIL_TO_CLEANUP(sem_post(sem_c2s) == -1, close_sem_s2c);
|
||||
|
||||
FAIL_TO_CLEANUP(sem_wait(sem_s2c) == -1, close_sem_s2c);
|
||||
|
||||
size_t len = strnlen((char *)shm_ptr, SHM_SIZE);
|
||||
FAIL_TO_CLEANUP(fwrite(shm_ptr, sizeof(char), len, stdout) != len, close_sem_s2c);
|
||||
fflush(stdout); // don't really care if fails
|
||||
|
||||
close_sem_s2c:
|
||||
sem_close(sem_s2c);
|
||||
close_sem_c2s:
|
||||
sem_close(sem_c2s);
|
||||
unmap_shared_mem:
|
||||
munmap(shm_ptr, SHM_SIZE);
|
||||
close_shared_fd:
|
||||
close(shm_fd);
|
||||
exit:
|
||||
return ret;
|
||||
}
|
||||
@ -1,12 +0,0 @@
|
||||
#ifndef COMMON_H
|
||||
#define COMMON_H
|
||||
|
||||
#define SHM_NAME "/lab7_shm"
|
||||
#define SEM_C2S_NAME "/sem_c2s_lab7"
|
||||
#define SEM_S2C_NAME "/sem_s2c_lab7"
|
||||
#define SHM_SIZE (64 * 1024)
|
||||
|
||||
#define FAIL_ON(cond) if (cond) return EXIT_FAILURE
|
||||
|
||||
#endif /* COMMON_H */
|
||||
|
||||
@ -1,136 +0,0 @@
|
||||
#include <fcntl.h>
|
||||
#include <semaphore.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#define FAIL_TO_CLEANUP(cond, jmp_to) \
|
||||
if (cond) { \
|
||||
perror(#cond); \
|
||||
ret = EXIT_FAILURE; \
|
||||
goto jmp_to; \
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
int ret;
|
||||
int shm_fd = -1;
|
||||
void *shm_ptr = MAP_FAILED;
|
||||
sem_t *sem_c2s = SEM_FAILED;
|
||||
sem_t *sem_s2c = SEM_FAILED;
|
||||
|
||||
/* Create or open shared memory */
|
||||
FAIL_TO_CLEANUP((shm_fd = shm_open(SHM_NAME, O_CREAT | O_RDWR, 0600)), cleanup);
|
||||
if (shm_fd == -1) {
|
||||
perror("shm_open");
|
||||
goto cleanup;
|
||||
}
|
||||
if (ftruncate(shm_fd, SHM_SIZE) == -1) {
|
||||
perror("ftruncate");
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
shm_ptr = mmap(NULL, SHM_SIZE, PROT_READ | PROT_WRITE,
|
||||
MAP_SHARED, shm_fd, 0);
|
||||
if (shm_ptr == MAP_FAILED) {
|
||||
perror("mmap");
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
/* Create semaphores (initialize to 0) */
|
||||
sem_c2s = sem_open(SEM_C2S_NAME, O_CREAT, 0600, 0);
|
||||
if (sem_c2s == SEM_FAILED) {
|
||||
perror("sem_open c2s");
|
||||
goto cleanup;
|
||||
}
|
||||
sem_s2c = sem_open(SEM_S2C_NAME, O_CREAT, 0600, 0);
|
||||
if (sem_s2c == SEM_FAILED) {
|
||||
perror("sem_open s2c");
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
printf("Server: waiting for client...\n");
|
||||
|
||||
while (1) {
|
||||
/* Wait for client to write filename */
|
||||
if (sem_wait(sem_c2s) == -1) {
|
||||
perror("sem_wait c2s");
|
||||
break;
|
||||
}
|
||||
|
||||
/* Read filename from shared memory (ensure NUL) */
|
||||
char filename[SHM_SIZE];
|
||||
memset(filename, 0, sizeof(filename));
|
||||
memcpy(filename, shm_ptr, sizeof(filename) - 1);
|
||||
|
||||
/* If client sent special "EXIT" filename, break loop and cleanup */
|
||||
if (strcmp(filename, "EXIT") == 0) {
|
||||
/* Notify client and exit */
|
||||
const char *msg = "Server shutting down.";
|
||||
size_t len = strlen(msg) + 1;
|
||||
if (len > SHM_SIZE)
|
||||
len = SHM_SIZE;
|
||||
memcpy(shm_ptr, msg, len);
|
||||
sem_post(sem_s2c);
|
||||
break;
|
||||
}
|
||||
|
||||
/* Try open file */
|
||||
FILE *f = fopen(filename, "rb");
|
||||
if (!f) {
|
||||
char errbuf[SHM_SIZE];
|
||||
snprintf(errbuf, sizeof(errbuf), "Error opening file '%s'\n", filename);
|
||||
size_t len = strlen(errbuf) + 1;
|
||||
if (len > SHM_SIZE)
|
||||
len = SHM_SIZE;
|
||||
memcpy(shm_ptr, errbuf, len);
|
||||
sem_post(sem_s2c);
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Read file content and write to shared memory (truncate if too large) */
|
||||
size_t total = 0;
|
||||
while (!feof(f) && total < SHM_SIZE - 1) {
|
||||
size_t toread = SHM_SIZE - 1 - total;
|
||||
size_t r = fread((char *)shm_ptr + total, 1, toread, f);
|
||||
total += r;
|
||||
if (r == 0)
|
||||
break;
|
||||
}
|
||||
((char *)shm_ptr)[total] = '\0'; /* ensure NUL */
|
||||
|
||||
if (!feof(f) && total == SHM_SIZE - 1) {
|
||||
/* truncated */
|
||||
const char *note =
|
||||
"\n[Output truncated: file larger than shared memory]\n";
|
||||
size_t note_len = strlen(note);
|
||||
if (note_len + 1 < SHM_SIZE) {
|
||||
size_t copy_pos = SHM_SIZE - 1 - note_len;
|
||||
memcpy((char *)shm_ptr + copy_pos, note, note_len);
|
||||
((char *)shm_ptr)[SHM_SIZE - 1] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
|
||||
/* Signal client that data is ready */
|
||||
if (sem_post(sem_s2c) == -1) {
|
||||
perror("sem_post s2c");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
cleanup:
|
||||
if (sem_c2s != SEM_FAILED)
|
||||
sem_close(sem_c2s);
|
||||
if (sem_s2c != SEM_FAILED)
|
||||
sem_close(sem_s2c);
|
||||
if (shm_ptr != MAP_FAILED) munmap(shm_ptr, SHM_SIZE); if (shm_fd != -1)
|
||||
close(shm_fd);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user