linux/test/linux.c
2024-12-18 17:24:29 +08:00

239 lines
6.0 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <openssl/evp.h>
#include <openssl/sha.h>
#include <zlib.h>
#include <time.h>
// 线程、信号量全局变量
pthread_t thread1, thread2;
sem_t semaphore;
// 消息队列结构
struct message {
long mtype;
char mtext[100];
};
// 函数声明
void file_system_demo();
void thread_control_demo();
void process_communication_demo();
void socket_communication_demo();
void encryption_demo();
int main() {
int choice;
while (1) {
printf("\n请选择要演示的功能模块:\n");
printf("1. 文件系统操作\n");
printf("2. 线程控制与信号量\n");
printf("3. 进程间通信\n");
printf("4. Socket通信\n");
printf("5. 加密解密与哈希\n");
printf("0. 退出\n");
printf("输入选择:");
scanf("%d", &choice);
switch (choice) {
case 1:
file_system_demo();
break;
case 2:
thread_control_demo();
break;
case 3:
process_communication_demo();
break;
case 4:
socket_communication_demo();
break;
case 5:
encryption_demo();
break;
case 0:
printf("退出程序。\n");
exit(0);
default:
printf("无效选择,请重试。\n");
}
}
return 0;
}
//文件系统操作
void file_system_demo() {
const char *filename = "testfile.txt";
const char *data = "这是一个文件系统操作的测试文件。\n";
// 创建文件并写入数据
int fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0644);
if (fd == -1) {
perror("创建文件失败");
return;
}
write(fd, data, strlen(data));
close(fd);
printf("文件已创建并写入数据。\n");
// 读取文件数据
fd = open(filename, O_RDONLY);
if (fd == -1) {
perror("打开文件失败");
return;
}
char buffer[256];
ssize_t bytes_read = read(fd, buffer, sizeof(buffer) - 1);
if (bytes_read > 0) {
buffer[bytes_read] = '\0';
printf("文件内容:%s", buffer);
}
close(fd);
// 删除文件
if (remove(filename) == 0) {
printf("文件已删除。\n");
} else {
perror("删除文件失败");
}
}
//线程控制与信号量
void *thread_function(void *arg) {
sem_wait(&semaphore);
printf("线程 %d: 信号量已获取。\n", *(int *)arg);
sleep(2);
printf("线程 %d: 任务完成,释放信号量。\n", *(int *)arg);
sem_post(&semaphore);
return NULL;
}
void thread_control_demo() {
sem_init(&semaphore, 0, 1);
int id1 = 1, id2 = 2;
pthread_create(&thread1, NULL, thread_function, &id1);
pthread_create(&thread2, NULL, thread_function, &id2);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
sem_destroy(&semaphore);
}
//进程间通信 (消息队列)
#define PORT 8080
#define BUFFER_SIZE 256
void process_communication_demo() {
key_t key = ftok("msgqueue", 65);
int msgid = msgget(key, 0666 | IPC_CREAT);
if (msgid == -1) {
perror("创建消息队列失败");
return;
}
struct message msg;
msg.mtype = 1;
strcpy(msg.mtext, "来自进程间通信的消息!");
if (msgsnd(msgid, &msg, sizeof(msg.mtext), 0) == -1) {
perror("发送消息失败");
} else {
printf("消息已发送:%s\n", msg.mtext);
}
if (msgrcv(msgid, &msg, sizeof(msg.mtext), 1, 0) == -1) {
perror("接收消息失败");
} else {
printf("接收到消息:%s\n", msg.mtext);
}
msgctl(msgid, IPC_RMID, NULL);
}
//Socket通信
void socket_communication_demo() {
int server_fd, client_fd;
struct sockaddr_in server_addr, client_addr;
socklen_t client_addr_len = sizeof(client_addr);
char buffer[BUFFER_SIZE];
// 创建套接字
server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd == -1) {
perror("创建Socket失败");
exit(EXIT_FAILURE);
}
// 配置服务器地址
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = INADDR_ANY;
// 绑定地址
if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
perror("绑定失败");
close(server_fd);
exit(EXIT_FAILURE);
}
// 开始监听
if (listen(server_fd, 5) == -1) {
perror("监听失败");
close(server_fd);
exit(EXIT_FAILURE);
}
printf("服务器已启动,等待客户端连接...\n");
// 接受客户端连接
client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &client_addr_len);
if (client_fd == -1) {
perror("接受连接失败");
close(server_fd);
exit(EXIT_FAILURE);
}
printf("客户端已连接。\n");
// 接收消息
ssize_t bytes_received = recv(client_fd, buffer, BUFFER_SIZE - 1, 0);
if (bytes_received > 0) {
buffer[bytes_received] = '\0';
printf("收到客户端消息:%s\n", buffer);
// 发送回应
const char *response = "Hello from server!";
send(client_fd, response, strlen(response), 0);
printf("回应已发送。\n");
}
// 关闭套接字
close(client_fd);
close(server_fd);
}
//加密解密与哈希
void encryption_demo() {
const char *plaintext = "这是需要加密的文本。";
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256((unsigned char *)plaintext, strlen(plaintext), hash);
printf("SHA-256 哈希值:");
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
printf("%02x", hash[i]);
}
printf("\n");
}