init commit

This commit is contained in:
2024-03-19 01:05:51 +08:00
commit 199bbf2628
393 changed files with 34883 additions and 0 deletions

View File

@ -0,0 +1,103 @@
# this file is only for local docker
version: '3'
services:
kong_migrations: # 构建kong数据库,这个镜像和下面的镜像是一样的
image: kong
container_name: micro_kong_migrations
command: kong migrations bootstrap
depends_on:
- kong_database
environment:
- KONG_DATABASE=postgres
- KONG_PG_HOST=kong_database
- KONG_PG_DATABASE=kong
- KONG_PG_PASSWORD=kong
networks:
- micro
kong: # kong网关服务
image: kong
hostname: kong
container_name: micro_kong
environment:
- KONG_DATABASE=postgres
- KONG_PG_HOST=172.28.1.4
- KONG_PG_DATABASE=kong
- KONG_PG_PASSWORD=kong
- KONG_DNS_RESOLVER=172.28.1.3:8600
- KONG_ADMIN_LISTEN=0.0.0.0:8001, 0.0.0.0:8444 ssl
depends_on:
- kong_migrations
- kong_database
ports:
- "8000:8000"
- "8443:8443"
- "8001:8001"
- "8444:8444"
networks:
- micro
kong_dashboard: # kong管理面板
image: pantsel/konga
container_name: kong_dashboard
environment:
- TOKEN_SECRET=micro
- NODE_ENV=development
- DB_ADAPTER=postgres
- DB_HOST=kong_database
- DB_USER=kong
- DB_PASSWORD=kong
- DB_DATABASE=kong
depends_on:
- kong
ports:
- 1337:1337
networks:
- micro
kong_database: # kong数据库 采用的postgres
image: postgres
hostname: kong_database
container_name: micro_kong_database
environment:
- POSTGRES_USER=kong
- POSTGRES_DB=kong
- POSTGRES_PASSWORD=kong
ports:
- 5432:5432
volumes:
- pgdata:/var/lib/postgresql/data
networks:
micro:
ipv4_address: 172.28.1.4
reverse-proxy:
image: traefik # The official Traefik docker image
command: --api --docker # Enables the web UI and tells Traefik to listen to docker
ports:
- "8081:80" # The HTTP port
- "8080:8080" # The Web UI (enabled by --api)
volumes:
- /var/run/docker.sock:/var/run/docker.sock # So that Traefik can listen to the Docker events
networks:
- micro
whoami:
image: containous/whoami
labels:
- "traefik.frontend.rule=Host:whoami.docker.localhost"
networks:
- micro
networks:
micro:
ipam:
driver: default
config:
- subnet: 172.28.0.0/16
volumes:
pgdata:

24
archive/other/ssocket/.gitignore vendored Normal file
View File

@ -0,0 +1,24 @@
/.idea
/.vscode
*.o
*.log
# executable file
/src/main
/test/client
# automake
/autom4te.cache
.deps
stamp-h1
Makefile
aclocal.m4
/config.*
configure
Makefile.in
install-sh
compile
depcomp
missing
/cmake-build-debug

View File

@ -0,0 +1,12 @@
# 主要用于clion的cmake配置
cmake_minimum_required(VERSION 3.2)
project(ssocket)
include_directories(/usr/include)
add_executable(main src/main.c)
add_executable(client test/client.c)
TARGET_LINK_LIBRARIES(main pthread aio)

View File

@ -0,0 +1,2 @@
AUTOMAKE_OPTIONS=foreign
SUBDIRS=src/ test/

View File

@ -0,0 +1,3 @@
#!/bin/sh
autoreconf --install --force

View File

@ -0,0 +1,20 @@
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.69])
AC_INIT(ssocket, 1.0, love@xloli.top)
AC_CONFIG_SRCDIR([src/main.c])
AC_CONFIG_HEADERS([config.h])
AM_INIT_AUTOMAKE(ssocket,1.0)
# Checks for programs.
AC_PROG_CC
# Checks for libraries.
# Checks for header files.
# Checks for typedefs, structures, and compiler characteristics.
# Checks for library functions.
AC_OUTPUT([Makefile src/Makefile test/Makefile])

View File

@ -0,0 +1,9 @@
> 学习Linux c 挖坑
## build
```bash
./autogen.sh && ./configure
make
```

View File

@ -0,0 +1,5 @@
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=main
LIBS=-laio
main_LDFLAGS=-pthread
main_SOURCES=main.c

View File

@ -0,0 +1,33 @@
#include <stdio.h>
#include <sys/shm.h>
#include <mhash.h>
#include <semaphore.h>
int main() {
sem_t sem_id;
sem_init(&sem_id, 1654, 1);
int shm = shmget(0, sizeof(int), 0666 | IPC_CREAT);
int *a = shmat(shm, 0, 0);
*a = 0;
for (int i = 0; i < 100; i++) {
int pid = fork();
if (pid == 0) {
int *b = shmat(shm, 0, 0);
while (true) {
sem_wait(&sem_id);
*b = *b + 1;
if (*b > 10000) {
sem_post(&sem_id);
break;
}
printf("pid:%d,%d\n", getpid(), *b);
sem_post(&sem_id);
}
return 0;
}
}
sleep(4);
sem_destroy(&sem_id);
shmctl(shm, IPC_CREAT, 0);
return 0;
}

View File

@ -0,0 +1,46 @@
#include <stdio.h>
#include <fcntl.h>
#include <libaio.h>
#include <malloc.h>
#include <mhash.h>
#define MAX_EVENT 10
#define BUF_LEN 1024
void callback(io_context_t ctx, struct iocb *iocb, long res, long res2) {
printf("test call\n");
printf("%s\n", iocb->u.c.buf);
}
int main() {
int fd = open("/home/huanl/client.ovpn", O_RDONLY, 0);
io_context_t io_context;
struct iocb io, *p = &io;
struct io_event event[MAX_EVENT];
char *buf = malloc(BUF_LEN);
memset(buf, 0, BUF_LEN);
memset(&io_context, 0, sizeof(io_context));
if (io_setup(10, &io_context)) {
printf("io_setup error");
return 0;
}
if (fd < 0) {
printf("open file error");
return 0;
}
io_prep_pread(&io, fd, buf, BUF_LEN, 0);
io_set_callback(&io, callback);
if (io_submit(io_context, 1, &p) < 0) {
printf("io_submit error");
return 0;
}
int num = io_getevents(io_context, 1, MAX_EVENT, event, NULL);
for (int i = 0; i < num; i++) {
io_callback_t io_callback = event[i].data;
io_callback(io_context, event[i].obj, event[i].res, event[i].res2);
}
return 0;
}

View File

@ -0,0 +1,79 @@
#include <stdio.h>
#include <ucontext.h>
#include <mhash.h>
#define STACK_SIZE 1024*128
#define CO_RUN 1
#define CO_HANG 2
#define CO_OVER 3
typedef void (*co_func)(struct coroutine *co);
struct coroutine {
char stack[STACK_SIZE];//栈
ucontext_t ctx;//ucp
ucontext_t ucp;
char status;//协程状态
};
void co_main(struct coroutine *co, co_func func) {
func(co);
co->status = CO_OVER;
}
void co_create(struct coroutine *co, co_func func) {
getcontext(&co->ucp);
co->ctx = co->ucp;
co->ctx.uc_stack.ss_sp = co->stack;
co->ctx.uc_stack.ss_size = STACK_SIZE;
co->ctx.uc_stack.ss_flags = 0;
co->ctx.uc_link = &co->ucp;
co->status = CO_RUN;
makecontext(&co->ctx, co_main, 2, co, func);
swapcontext(&co->ucp, &co->ctx);
}
void co_yield(struct coroutine *co) {
if (co->status == CO_OVER) {
return;
}
co->status = CO_HANG;
swapcontext(&co->ctx, &co->ucp);
}
void co_resume(struct coroutine *co) {
if (co->status == CO_OVER) {
return;
}
co->status = CO_RUN;
swapcontext(&co->ucp, &co->ctx);
}
int co_status(struct coroutine *co) {
return co->status;
}
void print1(struct coroutine *co) {
for (int i = 0; i < 50; i++) {
printf("1号协程:%d\n", i);
co_yield(co);
}
}
void print2(struct coroutine *co) {
for (int i = 100; i < 200; i++) {
printf("2号协程:%d\n", i);
co_yield(co);
}
}
int main() {
struct coroutine co1, co2;
co_create(&co1, print1);
co_create(&co2, print2);
while (co_status(&co1) != CO_OVER || co_status(&co2) != CO_OVER) {
co_resume(&co1);
co_resume(&co2);
}
return 0;
}

View File

@ -0,0 +1,79 @@
#include <stdio.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <netinet/in.h>
#include <unistd.h>
#include <malloc.h>
#include <mhash.h>
#include <fcntl.h>
#define MAX_EVENT 20
#define BUFF_LEN 1024
void log(const char *str) {
printf("%s\n", str);
}
int main() {
int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (fd == -1) {
log("socket create error");
return 0;
}
//设置为非阻塞因为ET模式的原因一次accept可能会接收多个用户
int flag = fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, flag | O_NONBLOCK);
struct sockaddr_in server_sockaddr;
server_sockaddr.sin_family = AF_INET;
server_sockaddr.sin_port = htons(1234);
server_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(fd, (const struct sockaddr *) &server_sockaddr, sizeof(struct sockaddr_in)) == -1) {
log("bind socket error");
return 0;
}
listen(fd, SOMAXCONN);
struct sockaddr_in client_sockaddr;
int size = 0, client_fd = 0, fd_num = 0;
int epfd = epoll_create(1);
size_t ret_size;
char *buf = malloc(BUFF_LEN);
struct epoll_event ev, evs[MAX_EVENT];
ev.data.fd = fd;
ev.events = EPOLLIN | EPOLLET;
epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev);
int count = 0;
while (1) {
fd_num = epoll_wait(epfd, evs, MAX_EVENT, -1);
printf("fd_num:%d\n", fd_num);
for (int i = 0; i < fd_num; i++) {
if (evs[i].data.fd == fd) {
//server 收到链接,非阻塞循环accept,直到-1
while ((client_fd = accept(fd,
(struct sockaddr *) &client_sockaddr,
(socklen_t *) &size)) != -1) {
ev.data.fd = client_fd;
ev.events = EPOLLIN | EPOLLET;
printf("connect:%d\n", client_fd);
epoll_ctl(epfd, EPOLL_CTL_ADD, client_fd, &ev);
}
if (errno != EAGAIN) {
printf("accept error\n");
return 0;
}
} else if (evs[i].events) {
count++;
//应该是客户端了
client_fd = evs[i].data.fd;
memset(buf, 0, BUFF_LEN);
ret_size = (size_t) recv(client_fd, buf, BUFF_LEN, 0);
printf("fd:%d,ret:%d,count:%d,recv\n", client_fd, ret_size, count);
ret_size = send(client_fd, "hello", 5, 0);
close(client_fd);
ret_size = epoll_ctl(epfd, EPOLL_CTL_DEL, client_fd, &ev);
}
}
}
return 0;
}

View File

@ -0,0 +1,38 @@
#include <stdio.h>
#include <sys/shm.h>
#include <mhash.h>
#include <pthread.h>
int main() {
int shm = shmget(0, sizeof(int), 0666 | IPC_CREAT);
int *a = shmat(shm, 0, 0);
*a = 0;
pthread_mutex_t mutex;
pthread_mutexattr_t mutexattr;
//配置选项
pthread_mutexattr_init(&mutexattr);
pthread_mutexattr_setpshared(&mutexattr, PTHREAD_PROCESS_SHARED);//比较重要的一步,进程间共享
pthread_mutex_init(&mutex, &mutexattr);
for (int i = 0; i < 100; i++) {
int pid = fork();
if (pid == 0) {
int *b = shmat(shm, 0, 0);
while (true) {
int ret = pthread_mutex_lock(&mutex);
if (*b >= 10000) {
pthread_mutex_unlock(&mutex);
break;
}
*b = *b + 1;
printf("pid:%d,%d,%d\n", getpid(), *b, ret);
pthread_mutex_unlock(&mutex);
}
return 0;
}
}
sleep(4);
shmctl(shm, IPC_CREAT, 0);
pthread_mutexattr_destroy(&mutexattr);
pthread_mutex_destroy(&mutex);
return 0;
}

View File

@ -0,0 +1,109 @@
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <mhash.h>
#define MAX_CLIENT 1023
#define BUFF_LEN 1024
void log(const char *str) {
printf("%s\n", str);
}
int main() {
int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
int on = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
if (fd == -1) {
log("socket create error");
return 0;
}
struct sockaddr_in server_sockaddr;
server_sockaddr.sin_family = AF_INET;
server_sockaddr.sin_port = htons(1234);
server_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(fd, (const struct sockaddr *) &server_sockaddr, sizeof(struct sockaddr_in)) == -1) {
log("bind socket error");
return 0;
}
listen(fd, 20);
struct sockaddr_in client_sockaddr;
int client_fd[MAX_CLIENT] = {-1}, size;
memset(client_fd, -1, MAX_CLIENT * sizeof(int));
char *buf = malloc(BUFF_LEN);
//select
fd_set allset;
struct timeval timeout;
timeout.tv_sec = 2;
timeout.tv_usec = 0;
FD_ZERO(&allset);
FD_SET(fd, &allset);
fd_set tmp_set;
int maxfd = fd;
int count = 0;
while (1) {
tmp_set = allset;
int ret = select(maxfd + 1, &tmp_set, NULL, NULL, NULL);
if (ret > 0) {
//大于0判断是否有数据进入了
if (FD_ISSET(fd, &tmp_set)) {
//如果是fd这个那么代表有新链接进入
int tmp_client_fd = accept(fd, (struct sockaddr *) &client_sockaddr, &size);
//将客户fd存入数组
int i = 0;
for (i = 0; i < MAX_CLIENT; i++) {
if (client_fd[i] == -1) {//就是你了
client_fd[i] = tmp_client_fd;
break;
}
}
if (client_fd[i] != tmp_client_fd) {
//超出限制了,直接丢弃因为select一般最多只能操作1024个文件
//__FD_SETSIZE决定的更多需要修改内核
close(tmp_client_fd);
} else {
if (client_fd[i] > maxfd) {
maxfd = client_fd[i];
}
FD_SET(client_fd[i], &allset);
}
} else {
//不是服务器的句柄,那么就是客户端的句柄咯,要遍历客户端句柄
maxfd = fd;
for (int i = 0; i < MAX_CLIENT; i++) {
if (client_fd[i] != -1) {
if (FD_ISSET(client_fd[i], &tmp_set)) {
//是这个客户端,接受消息
memset(buf, 0, BUFF_LEN);
count++;
ret = recv(client_fd[i], buf, BUFF_LEN, 0);
printf("fd:%d,ret:%d,count:%d,recv\n", client_fd[i], ret, count);
if (ret == 0) {
//从集合中删除
printf("error,fd:%d,ret:%d,count:%d,recv\n", client_fd[i], ret, count);
close(client_fd[i]);
FD_CLR(client_fd[i], &allset);
client_fd[i] = -1;
continue;
}
ret = (int) send(client_fd[i], "hello", 5, 0);
//从集合中删除
close(client_fd[i]);
FD_CLR(client_fd[i], &allset);
client_fd[i] = -1;
}
if (client_fd[i] > maxfd) {
maxfd = client_fd[i];
}
}
}
}
} else {
break;
}
}
free(buf);
return 0;
}

View File

@ -0,0 +1,33 @@
#include <stdio.h>
#include <sys/shm.h>
#include <mhash.h>
#include <semaphore.h>
int main() {
sem_t sem_id;
sem_init(&sem_id, 1654, 1);
int shm = shmget(0, sizeof(int), 0666 | IPC_CREAT);
int *a = shmat(shm, 0, 0);
*a = 0;
for (int i = 0; i < 100; i++) {
int pid = fork();
if (pid == 0) {
int *b = shmat(shm, 0, 0);
while (true) {
sem_wait(&sem_id);
*b = *b + 1;
if (*b > 10000) {
sem_post(&sem_id);
break;
}
printf("pid:%d,%d\n", getpid(), *b);
sem_post(&sem_id);
}
return 0;
}
}
sleep(4);
sem_destroy(&sem_id);
shmctl(shm, IPC_CREAT, 0);
return 0;
}

View File

@ -0,0 +1,29 @@
#include <stdio.h>
#include <sys/shm.h>
#include <mhash.h>
int main() {
int shm = shmget(0, sizeof(int), 0666 | IPC_CREAT);
int *a = shmat(shm, 0, 0);
*a = 6;
for (int i = 0; i < 2; i++) {
int pid = fork();
if (pid == 0) {
int *b = shmat(shm, 0, 0);
printf("sub:%d,%d\n", *b, b);
if (i == 1) {
*b = 109;
printf("sub1\n");
}
sleep(2);
printf("sub:%d,%d\n", *b, b);
return 0;
}
}
sleep(1);
*a = 10987;
printf("father:%d,%d", *a, a);
sleep(2);
shmctl(shm, IPC_CREAT, 0);
return 0;
}

View File

@ -0,0 +1,4 @@
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=client
client_SOURCES=client.c

View File

@ -0,0 +1,63 @@
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <malloc.h>
#include <mhash.h>
#include <sys/wait.h>
#define FORK_NUM 10
int sub_pid[FORK_NUM] = {0};
void sigint_deal(int sig) {
for (int i = 0; i < FORK_NUM; i++) {
kill(sub_pid[i], SIGINT);
}
exit(0);
}
int main() {
struct hostent *host = gethostbyname("127.0.0.1");
void *buff = malloc(1024);
signal(SIGINT, sigint_deal);
for (int i = 0; i < FORK_NUM; i++) {
int pid = fork();
if (pid == 0) {
//子进程
printf("in sub process\n");
for (int j = 0; j < 100; j++) {
int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
struct sockaddr_in server_sock;
server_sock.sin_family = AF_INET;
server_sock.sin_port = htons(1234);
server_sock.sin_addr = *((struct in_addr *) host->h_addr);
int ret = connect(fd, (const struct sockaddr *) &server_sock, sizeof(struct sockaddr_in));
if (ret == -1) {
printf("connect error\n");
continue;
}
ret = (int) send(fd, "test", 4, 0);
printf("pid:%d,send test:%d\n", getpid(), ret);
memset(buff, 0, 1024);
ret = (int) recv(fd, buff, 1024, 0);
printf("pid:%d,recv:%s\n", getpid(), (char *) buff);
close(fd);
}
printf("sub process over\n");
return 0;
} else if (pid > 0) {
sub_pid[i] = pid;
printf("my father\n");
} else {
printf("create process error\n");
}
}
for (int i = 0; i < FORK_NUM; i++) {
wait((int *) sub_pid[i]);
}
printf("over\n");
free(buff);
return 0;
}

10
archive/other/test.c Normal file
View File

@ -0,0 +1,10 @@
int main()
{
int a = 0;
for (int i = 0; i < 3; i++)
{
a += i;
}
}
//gcc -g -c test.c
//objdump -d -M intel -S test.o