
作业社区
探索学习新天地,共享知识资源!
慕运维8597106 的学生作业:
#include #include #include #include #include typedef struct { char name[20]; int age; } person_t; void *do_thread(void *arg) { static person_t stu; strcpy(stu.name, "张三"); stu.age = 18; pthread_exit(&stu); } int main(int argc, char const *argv[]) { pthread_t tid; int ret; person_t *stu; ret = pthread_create(&tid, NULL, do_thread, NULL); if (ret != 0) { fprintf(stderr, "[ERROR] pthread_create: %s\n", strerror(ret)); } pthread_join(tid, (void **)&stu); if (stu != NULL) { printf("name:%s age:%d\n", stu->name, stu->age); } return 0; } 运行结果: linux@linux:~/learn/chapter12/thread$ gcc job1-2-new.c -l pthread linux@linux:~/learn/chapter12/thread$ ./a.out name:张三 age:18 linux@linux:~/learn/chapter12/thread$





浪潮君 的学生作业:
#include // 判断是否为闰年,返回1是闰年,0不是 int do_leap(int y) { return (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0); } int main() { int year = 0; printf(“输入年份:”); // 读取用户输入并进行校验 if (scanf("%d", &year) != 1) { // 如果scanf返回值不是1,说明输入错误 printf("输入错误!\n"); return 1; // 程序异常结束 } // 根据do_leap函数的返回结果判断输出 if (do_leap(year)) { printf("%d年是闰年\n", year); } else { printf("%d年不是闰年\n", year); } return 0; // 正常结束 }





慕运维8597106 的学生作业:
#include #include #include #include #include void *do_thread1(void *arg) { printf("thread1 start!\n"); pthread_exit(NULL); return NULL; } void *do_thread2(void *arg) { printf("thread2 start!\n"); pthread_exit(NULL); return NULL; } int main(int argc, char const *argv[]) { pthread_t tid1,tid2; int ret; ret = pthread_create(&tid1,NULL,do_thread1,NULL); if(ret != 0 ) { fprintf(stderr,"[ERROR] pthread_create : < %s >\n",strerror(ret)); } ret = pthread_create(&tid2,NULL,do_thread2,NULL); if(ret != 0 ) { fprintf(stderr,"[ERROR] pthread_create : < %s >\n",strerror(ret)); } printf("tid1 = %ld\n",tid1); printf("tid2 = %ld\n",tid2); pthread_detach(tid1); pthread_detach(tid2); while(1); return 0; }





慕运维8597106 的学生作业:
#include #include #include #include #include void *do_thread1(void *arg) { printf("thread1 start!\n"); return NULL; } void *do_thread2(void *arg) { printf("thread2 start!\n"); return NULL; } int main(int argc, char const *argv[]) { pthread_t tid1,tid2; int ret; ret = pthread_create(&tid1,NULL,do_thread1,NULL); if(ret != 0 ) { fprintf(stderr,"[ERROR] pthread_create : < %s >\n",strerror(ret)); } ret = pthread_create(&tid2,NULL,do_thread2,NULL); if(ret != 0 ) { fprintf(stderr,"[ERROR] pthread_create : < %s >\n",strerror(ret)); } printf("tid1 = %ld\n",tid1); printf("tid2 = %ld\n",tid2); return 0; } linux@linux:~/learn/chapter12/thread$ ./a.out tid1 = 139657435653888 tid2 = 139657427261184





胡汉三66 的学生作业:
seq(序列号) : 32位字段,表示数据字节流的起始编号。 ack (确认号) : 32位字段,表示接收方期望收到的下一个字节的序列号。具体数值。 A—ACK(确认标志位) :1位标志位,ACK=1时,表示前面的确认号字段有效。二进制位。TCP规定,连接建立后,ACK必须为1 S—SYN(同步标志位) :1位标志位,用于请求建立连接。二进制位。 前两次握手,SYN=1,表示请求建立连接或同意建立连接。 F—FIN(终止标志位) : 1位标志位,用于请求关闭连接。若FIN=1,表示数据已经发送完成,可以释放连接。 关键场景中的组合使用 三次握手: 第一次握手:客户端→服务器 SYN=1, seq=x 第二次握手:服务器→客户端 SYN=1, ACK=1, seq=y, ack=x+1 第三次握手:客户端→服务器 ACK=1, seq=x+1, ack=y+1 四次挥手 第一次挥手:主动方→被动方 FIN=1, seq=u 第二次挥手:被动方→主动方 ACK=1, ack=u+1 第三次挥手:被动方→主动方 FIN=1, ACK=1, seq=v, ack=u+1 第四次挥手:主动方→被动方 ACK=1, ack=v+1 总结: seq/ack:维护数据传输的顺序和完整性,确保可靠传输。 ACK:确认机制的基础,标记应答有效性。 SYN/FIN:分别控制连接的建立和释放,通过三次握手和四次挥手实现全双工通信的启停。 协同机制:这写字段通过组合使用(如SYN+ACK,FIN+ACK),实现TCP面向连接、可靠传输的核心特性。





阿大月 的学生作业:
#include using namespace std; class times{ private: int hours; int minute; int second; public: void set_hours(int _hour); void set_minute(int _minute); void set_second(int _second); void print_time(); }; void times::set_hours(int _hour){ hours = _hour; } void times::set_minute(int _minute){ minute = _minute; } void times::set_second(int _second){ second = _second; } void times::print_time(){ cout





浪潮君 的学生作业:
#include int main() { // 定义二维数组,2行3列 int a[2][3] = {10, 20, 30, 40, 50, 60}; // ------------------------------- // 使用数组指针(int (*)[3])来输出 // ------------------------------- int (*p)[3] = a; // 定义一个数组指针p,指向一维数组(3个int) printf("使用数组指针输出二维数组内容:\n"); for (int i = 0; i < 2; i++) { // 遍历行 for (int j = 0; j < 3; j++) { // 遍历列 printf("%d ", p[i][j]); // 通过p[i][j]访问元素 } } printf("\n"); // ------------------------------- // 使用指针数组(int *p_arr[2])来输出 // ------------------------------- int *p_arr[2]; // 定义一个指针数组,数组里每个元素是int * // 初始化指针数组,让每个指针分别指向a的一行 for (int i = 0; i < 2; i++) { p_arr[i] = a[i]; } printf("使用指针数组输出二维数组内容:\n"); for (int i = 0; i < 2; i++) { // 遍历行 for (int j = 0; j < 3; j++) { // 遍历列 printf("%d ", p_arr[i][j]); // 通过p_arr[i][j]访问元素 } } printf("\n"); return 0; }




