
作业社区
探索学习新天地,共享知识资源!
小铭同志 的学生作业:
// 1.设计一个input_array(),自己定义参数和返回值,输⼊数据存放到数组a中. // 2.设计一个output_array(),自己定义参数和返回值,输出数组a的内容. // 3.设计一个find_max(),自己定义参数和返回值,找到数组中的最大值,并返回给main函数输出。 #include //创建输入函数 void input_array(int *p, const int plen) { printf(“请输入数组元素:”); for (int i = 0; i < plen; i++) { scanf("%d",&p[i]); } printf(“输入完成\n”); return ; } //创建输出函数 void output_array(int *q,const int qlen) { for (int i = 0; i < qlen; i++) { printf(“数组元素为:%d”,q[i]); } return ; } //查找最大值 int find_max(int *r,const int rlen) { int max = 0; for (int i = 0; i < rlen; i++) { if (r[i] > max) { max = r[i]; } } return max; } //主函数 int main() { int a[5]; int len = sizeof(a)/sizeof(a[0]); input_array(a,len); output_array(a,len); int smax = find_max(a,len); printf(“最大值为:%d”,smax); return 0; }





阿大月 的学生作业:
seq(Sequence Number) 序列号 作用:标识当前报文段中第一个数据字节的编号,跟踪数据字节的顺序,确保接收方能正确重组数据流 规则: 每个字节的数据都会被粉喷唯一的序列号 初始序列号(ISN)在建立连接时随机生成(防止历史报文干扰) 序列号是循环递增的(达到最大值后归零) ack(Acknowledgement Number)确认号 作用:表示期望接收的下一个字节的序列号。隐含意义:确认此前的所有数据(直到ack-1)以正确接收 规则: 仅当ACK标志位为1时,确认号字段才有效。 接受方通过ack告知对方:“我已经接收到ack-1之前的所有数据,请发送从ack开始的数据” 控制位 SYN(synchronize) 所用:用于建立连接(三次握手阶段) 规则:SYN=1表示该报文段是连接请求(第一次握手)或连接响应(第二次握手) 携带SYN的报文会消耗一个序列号(即下一个报文的seq需+1) ACK(Acknowledgement) 作用:表示确认号字段有效(用于确认数据接收) 规则: 除了初始的SYN报文外,其他所有报文必须设置ACK=1 接收方通过ACK告知发送方数据已成功接收 FIN(Finish) 作用:用于关闭连接(四次挥手阶段) 规则: FIN=1表示发送方已经完成数据发送,请求终止连接 携带FIN的报文会消耗掉一个序列号





Linkus 的学生作业:
作业与内容不对,交练习:【图片】 /* 创建两个子进程 A B,父进程分别给两个子进程发消息,消息类型为100 200 */ /* 父进程从键盘读入数据,发送给子进程,输入quit则退出 */ #include #include #include #include #include #include #include #include #define PATH "." #define PROJ_ID 3 #define MSG_TYPE_A 100 #define MSG_TYPE_B 200 #define MSG_SIZE 64 struct msgbuf{ long type; char text[MSG_SIZE]; }; int main(void) { key_t key; int msgid,ret; pid_t apid,bpid; struct msgbuf msg; ssize_t bytes; key = ftok(PATH,PROJ_ID); if(key == -1) { perror("ftok(): "); exit(EXIT_FAILURE); } msgid = msgget(key,IPC_CREAT | 0644); if(msgid == -1) { perror("msgget(): "); exit(EXIT_FAILURE); } printf("msgid \n",msgid); apid = fork(); if(apid == -1) { perror("fork(): "); exit(EXIT_FAILURE); } else if (apid == 0) { do { bytes = msgrcv(msgid,(void *)&msg,MSG_SIZE,MSG_TYPE_A,0); printf("process A rev msg \n",msg.text); } while(strcmp(msg.text,"quit") != 0); printf("process A exit.\n"); exit(EXIT_SUCCESS); } bpid = fork(); if(bpid == -1) { perror("fork(): "); exit(EXIT_FAILURE); } else if (bpid == 0) { do { bytes = msgrcv(msgid,(void *)&msg,MSG_SIZE,MSG_TYPE_B,0); printf("process B rev msg \n",msg.text); } while(strcmp(msg.text,"quit") != 0); printf("process B exit.\n"); exit(EXIT_SUCCESS); } do { printf("Enter > "); fgets(msg.text,sizeof(msg.text),stdin); msg.text[strlen(msg.text)-1] = '\0'; printf("(Input )\n",msg.text); msg.type = MSG_TYPE_A; if(msgsnd(msgid,(const void *)&msg,strlen(msg.text)+1,0) == -1) { perror("msgsnd(): "); exit(EXIT_FAILURE); } msg.type = MSG_TYPE_B; if(msgsnd(msgid,(const void *)&msg,strlen(msg.text)+1,0) == -1) { perror("msgsnd(): "); exit(EXIT_FAILURE); } sleep(1); } while(strcmp(msg.text,"quit") != 0); wait(NULL); wait(NULL); ret = msgctl(msgid,IPC_RMID,NULL); if(ret == -1){ perror("msgctl(): "); exit(EXIT_FAILURE); } printf("Main Exit.\n"); return 0; }