Compare commits
2 Commits
os/lab-vir
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 0aed24179b | |||
| 5dbe188b08 |
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;
|
||||
}
|
||||
15
OSs/lab6/Makefile
Normal file
15
OSs/lab6/Makefile
Normal file
@ -0,0 +1,15 @@
|
||||
targets = server_msg client_msg
|
||||
all: $(targets)
|
||||
|
||||
server_msg: server_msg.o
|
||||
$(CC) -o $@ $^ $(CFLAGS)
|
||||
|
||||
client_msg: client_msg.o
|
||||
$(CC) -o $@ $^ $(CFLAGS)
|
||||
|
||||
%.o: %.c
|
||||
$(CC) -c $^ -o $@ $(CFLAGS)
|
||||
|
||||
clean:
|
||||
rm -f $(targets)
|
||||
rm -f *.o
|
||||
99
OSs/lab6/client_msg.c
Normal file
99
OSs/lab6/client_msg.c
Normal file
@ -0,0 +1,99 @@
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/msg.h>
|
||||
#define MQ_KEY1 1234L
|
||||
#define MQ_KEY2 2345L
|
||||
#define MAXMESGDATA 4096
|
||||
|
||||
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 mymesg *mptr);
|
||||
ssize_t mesg_recv(int id, struct mymesg *mptr);
|
||||
|
||||
int main(int argc,char **argv)
|
||||
{
|
||||
int readid, writeid;
|
||||
|
||||
if((writeid=msgget(MQ_KEY1,0666)) < 0)
|
||||
{
|
||||
printf("Client: can not get writeid!\n");
|
||||
exit(1);
|
||||
}
|
||||
printf("Client:writeid=%d\n",writeid);
|
||||
|
||||
if((readid=msgget(MQ_KEY2,0666)) < 0)
|
||||
{
|
||||
printf("Client: can not get readid!\n");
|
||||
exit(1);
|
||||
}
|
||||
printf("Client: readid=%d\n",readid);
|
||||
|
||||
client(readid,writeid);
|
||||
|
||||
if((msgctl(readid,IPC_RMID, NULL)) < 0)
|
||||
{
|
||||
printf("Client: can not delete massage queue2!\n"); exit(1);
|
||||
}
|
||||
|
||||
if((msgctl(writeid,IPC_RMID, NULL)) < 0)
|
||||
{
|
||||
printf("Client: can not delete massage queue1!\n"); exit(1);
|
||||
}
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
void client(int readid, int writeid)
|
||||
{
|
||||
size_t len;
|
||||
ssize_t n;
|
||||
struct mymesg ourmesg;
|
||||
|
||||
printf("Client:readid=%d writeid=%d\n",readid,writeid);
|
||||
printf("Input file name, please\n");
|
||||
|
||||
fgets(ourmesg.mesg_data,MAXMESGDATA, stdin);
|
||||
len=strlen(ourmesg.mesg_data);
|
||||
|
||||
if(ourmesg.mesg_data[len-1]=='\n') len--;
|
||||
ourmesg.mesg_len=len;
|
||||
|
||||
ourmesg.mesg_type=1;
|
||||
|
||||
printf("Client: %s\n",ourmesg.mesg_data);
|
||||
|
||||
mesg_send(writeid,&ourmesg);
|
||||
|
||||
printf("Client: tvk before recv!\n");
|
||||
|
||||
while((n= mesg_recv(readid, &ourmesg))>0)
|
||||
//n= mesg_recv(readid, &ourmesg);
|
||||
write(1,ourmesg.mesg_data, n);
|
||||
}
|
||||
|
||||
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 mymesg *mptr)
|
||||
{
|
||||
ssize_t n;
|
||||
|
||||
n=msgrcv(id, &(mptr->mesg_type),MAXMESGDATA,mptr->mesg_type, 0);
|
||||
mptr->mesg_len=n;
|
||||
printf("Client: n=%d\n",n);
|
||||
|
||||
return(n);
|
||||
}
|
||||
|
||||
116
OSs/lab6/server_msg.c
Normal file
116
OSs/lab6/server_msg.c
Normal file
@ -0,0 +1,116 @@
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/msg.h>
|
||||
#define MQ_KEY1 1234L
|
||||
#define MQ_KEY2 2345L
|
||||
#define MAXMESGDATA 4096
|
||||
|
||||
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 mymesg *mptr);
|
||||
ssize_t mesg_recv(int id, struct mymesg *mptr);
|
||||
|
||||
int main(int argc,char **argv)
|
||||
{
|
||||
int readid, writeid;
|
||||
key_t key1,key2;
|
||||
|
||||
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);
|
||||
}
|
||||
printf("Server: writeid=%d\n",writeid);
|
||||
|
||||
server(readid,writeid);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
void server(int readid, int writeid)
|
||||
{
|
||||
FILE *fp;
|
||||
ssize_t n;
|
||||
struct mymesg ourmesg;
|
||||
|
||||
printf("Server:readid=%d writeid=%d\n",readid,writeid);
|
||||
|
||||
ourmesg.mesg_type=1;
|
||||
|
||||
if( (n=mesg_recv(readid, &ourmesg)) == 0)
|
||||
{
|
||||
printf("Server: can not read file name\n");
|
||||
exit(1);
|
||||
}
|
||||
ourmesg.mesg_data[n]='\0';
|
||||
|
||||
printf("Server: file name %s\n",ourmesg.mesg_data);
|
||||
|
||||
if( (fp=fopen(ourmesg.mesg_data,"r"))==NULL)
|
||||
{
|
||||
printf("Server: can not open file name\n");
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Server: %s is opened\n",ourmesg.mesg_data);
|
||||
|
||||
while(fgets(ourmesg.mesg_data, MAXMESGDATA,fp) != NULL)
|
||||
{
|
||||
ourmesg.mesg_len=strlen(ourmesg.mesg_data);
|
||||
printf("Server: %s\n",ourmesg.mesg_data);
|
||||
mesg_send(writeid,&ourmesg);
|
||||
}
|
||||
}
|
||||
fclose(fp);
|
||||
ourmesg.mesg_len=0;
|
||||
mesg_send(writeid,&ourmesg);
|
||||
}
|
||||
|
||||
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 mymesg *mptr)
|
||||
{
|
||||
ssize_t n;
|
||||
n=msgrcv(id, &(mptr->mesg_type),MAXMESGDATA,mptr->mesg_type, 0);
|
||||
mptr->mesg_len=n;
|
||||
|
||||
return(n);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user