作业社区
探索学习新天地,共享知识资源!
慕运维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; }
+87
慕运维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
+90
胡汉三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面向连接、可靠传输的核心特性。
+74
阿大月 的学生作业:
#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
+12
浪潮君 的学生作业:
#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; }
+22
慕运维8597106 的学生作业:
#include #include #include #include #include #include #include void do_user2_sig(int sig) { printf("Received : %s\n", strsignal(sig)); } int main(int argc, char const *argv[]) { pid_t cpid1, cpid2; cpid1 = fork(); if (signal(SIGUSR2, do_user2_sig) == SIG_ERR) { perror("[ERROR] signal():"); exit(EXIT_FAILURE); } if (cpid1 == -1) { perror("[ERROR] fork:"); exit(EXIT_FAILURE); } else if (cpid1 == 0) { pause(); printf("Child Process A Resume!\n"); exit(EXIT_SUCCESS); } else if (cpid1 > 0) { cpid2 = fork(); if (cpid2 == -1) { perror("[ERROR] fork:"); exit(EXIT_FAILURE); } else if (cpid2 == 0) { pause(); printf("Child Process B Resume!\n"); exit(EXIT_SUCCESS); } else if (cpid2 > 0) { sleep(3); kill(cpid1,SIGUSR1); kill(cpid2,SIGUSR2); waitpid(cpid1,NULL,0); waitpid(cpid2,NULL,0); } } return 0; } **运行结果:**只有Bpause后面的代码执行了,因为SIGUSR2被自定义了,而A进程收到SIGUSR1是终止进程,所以后面代码不执行 linux@linux:~/learn/chapter12$ ./a.out Received : User defined signal 2 Child Process B Resume!
+94
慕运维8597106 的学生作业:
#include #include #include #include #include #include int main(void) { pid_t cpid1,cpid2; cpid1 = fork(); if(cpid1 == -1) { perror("[ERROR] fork"); exit(EXIT_FAILURE); } else if(cpid1 == 0) { raise(SIGSTOP); } else if(cpid1 > 0) {// 主进程 cpid2 = fork(); if(cpid2 == -1) { perror("[ERROR] fork"); exit(EXIT_FAILURE); }else if(cpid2 == 0) { pause(); } else if(cpid2 > 0) {// 主进程 sleep(3); kill(cpid1,SIGKILL); printf("Father killed Child: %d\n",cpid1); kill(cpid2,SIGKILL); printf("Father killed Child: %d\n",cpid2); waitpid(cpid1,NULL,0); waitpid(cpid2,NULL,0); } } return 0; }
+100