作业社区
探索学习新天地,共享知识资源!
浪潮君 的学生作业:
HTTPS: 访问网页时使用的加密通信协议,保护数据在传输过程中不被窃听或篡改。DNS 用于将用户输入的网址转换为服务器的 IP 地址,从而实现网站访问。 SMTP: 负责把邮件从客户端发送到邮件服务器或其他邮件服务器。IMAP 允许用户从多个设备访问和管理保存在服务器上的邮件。 FTP: 用于在客户端和服务器之间传输文件,常用于网站文件上传。SSH 是一种加密的远程登录协议,安全地控制远程服务器。 DHCP: 自动为设备分配 IP 地址,简化网络接入配置。Telnet 也是远程登录协议,但不加密,安全性差,已基本被 SSH 替代。
+7
浪潮君 的学生作业:
seq(序列号):表示当前发送的数据流中的字节序号,用于确保数据顺序和重组。理解为“数据包编号”,告诉对方“我这是第几个包”。 ack(确认好):表示期望接收的下一个字节序号,确认对方已经收到的数据。理解为“收到信号”,告诉对方“你发到这里我收到了,下一个包该发哪个”。 ACK(标识位):用于标识这是一个确认报文,表示确认号有效。无该标志确认号不生效。通常 ACK=1 表示确认包。 SYN(同步标志):用于建立连接的同步序列号,开启 TCP 三次握手过程。相当于“握手请求”,告诉对方“我想建立连接”。 FIN(结束标志):用于关闭连接,表示发送方已经没有数据发送了。相当于“结束信号”,告诉对方“我这边数据发送完了,可以断开了”。
+9
沫颖 的学生作业:
space.h #ifndef __SPACE_H__ #define __SPACE_H__ namespace A_Space { int calc(int a, int b); } namespace B_Space { int calc(int a, int b); } #endif a.cpp #include "space.h" namespace A_Space { int calc(int a, int b) { return a + b; } } b.cpp #include "space.h" namespace B_Space { int calc(int a, int b) { return a - b; } } main.cpp #include #include "space.h" int main(void) { int a = 10, b = 40; std::cout
+100
浪潮君 的学生作业:
#include #include #include #include #include // ------------------- 宏定义 ------------------- #define MAX_CONSUMERS 3 // 消费者线程的最大数量 // ------------------- 全局共享资源 ------------------- static int number = 0; // 当前库存产品数量(共享变量) static int total_of_produce = 0; // 所有生产者线程总共要生产的产品数量 static int total_of_consume = 0; // 所有消费者线程总共已消费的产品数量 // ------------------- 线程同步工具 ------------------- static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; // 互斥锁,保护临界区(库存、总量等) static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; // 条件变量,用于线程等待/唤醒 // ------------------- 结构体:线程参数 ------------------- typedef struct { int count; // 当前生产者线程要生产的产品数量 int id; // 当前线程编号(从 1 开始) } ProducersArg; // ------------------- 生产者线程函数 ------------------- void *producer_thread(void *arg) { ProducersArg *parg = (ProducersArg *) arg; for (int i = 0; i < parg->count; i++) { pthread_mutex_lock(&mtx); // 进入临界区,锁定资源 number++; // 库存 +1 printf("生产者[%d] 生产了一个产品,总库存:%d\n", parg->id, number); pthread_cond_broadcast(&cond); // 唤醒所有消费者线程(库存可能可消费了) pthread_mutex_unlock(&mtx); // 解锁,退出临界区 usleep(random() % 500000); // 模拟生产耗时(0~0.5 秒) } free(parg); // 释放传入的参数结构体(堆内存) return NULL; } // ------------------- 消费者线程函数 ------------------- void *consumer_thread(void *arg) { int id = *(int *) arg; // 取出线程编号 free(arg); // 释放编号内存(主线程 malloc 的) while (true) { pthread_mutex_lock(&mtx); // 锁住临界区 // 如果已经消费完所有产品,就退出等待 while (total_of_consume >= total_of_produce) pthread_cond_wait(&cond, &mtx); // 等待生产者唤醒 // 二次判断:是否所有产品都消费完 if (total_of_consume >= total_of_produce) { pthread_mutex_unlock(&mtx); // 解锁后退出 break; } number--; // 消费一个产品 total_of_consume++; // 消费计数器 +1 printf("消费者[%d] 消费了一个产品,剩余库存:%d\n", id, number); pthread_mutex_unlock(&mtx); // 解锁临界区 usleep(random() % 500000); // 模拟消费耗时 } return NULL; } // ------------------- 主线程函数 ------------------- int main(int argc, char *argv[]) { if (argc < 2) { fprintf(stderr, "用法: %s ...\n", argv[0]); exit(EXIT_FAILURE); } int num_producers = argc - 1; // 生产者线程数 = 参数数量 - 程序名 pthread_t producers[num_producers]; // 生产者线程 ID 数组 pthread_t consumers[MAX_CONSUMERS]; // 消费者线程 ID 数组 // 创建生产者线程 for (int i = 0; i < num_producers; i++) { char *endptr = NULL; long val = strtol(argv[i + 1], &endptr, 10); // 解析参数 // 参数非法判断:必须是正整数 if (*endptr != '\0' || val count = count; parg->id = i + 1; // 线程编号从 1 开始 // 创建线程 if (pthread_create(&producers[i], NULL, producer_thread, parg) != 0) { perror("pthread_create(生产者)"); free(parg); exit(EXIT_FAILURE); } } // 创建消费者线程 for (int i = 0; i < MAX_CONSUMERS; i++) { int *cid = malloc(sizeof(int)); if (!cid) { perror("malloc"); exit(EXIT_FAILURE); } *cid = i + 1; // 编号从 1 开始 if (pthread_create(&consumers[i], NULL, consumer_thread, cid) != 0) { perror("pthread_create(消费者)"); free(cid); exit(EXIT_FAILURE); } } // 等待所有生产者线程结束 for (int i = 0; i < num_producers; i++) { pthread_join(producers[i], NULL); } // 等待所有消费者线程结束 for (int i = 0; i < MAX_CONSUMERS; i++) { pthread_join(consumers[i], NULL); } // 销毁同步资源 pthread_mutex_destroy(&mtx); pthread_cond_destroy(&cond); printf("所有产品已被消费完毕,总共消费 %d 个\n", total_of_consume); return 0; }
+7
Linkus 的学生作业:
#include #include #include #include static int global = 0; static pthread_mutex_t mutex; // = PTHREAD_MUTEX_INITIALIZER; void *do_thread(void *arg) { int loops = *(int *)arg; int i,tmp = 0; for (i = 0;i < loops;i++){ pthread_mutex_lock(&mutex); tmp = global; tmp++; global = tmp; pthread_mutex_unlock(&mutex); } pthread_exit(NULL); } int main(int argc,char *argv[]) { int err,i = 0; pthread_t tid[2] = {0}; int loops = 0; if (argc != 2){ fprintf(stderr,"Usage : < %s > < count loops>\n",argv[0]); exit(EXIT_FAILURE); } pthread_mutex_init(&mutex,NULL); loops = atoi(argv[1]); for (i = 0;i < 2;i++){ err = pthread_create(&tid[i],NULL,do_thread,&loops); if (err != 0){ fprintf(stderr,"[ERROR] pthread_create(): < %s > \n",strerror(err)); exit(EXIT_FAILURE); } } pthread_join(tid[0],NULL); pthread_join(tid[1],NULL); pthread_mutex_destroy(&mutex); printf("global = %d\n",global); return 0; }
+9