为了账号安全,请及时绑定邮箱和手机立即绑定

作业社区

探索学习新天地,共享知识资源!

0 提交作业
0 布置作业
0 满分作业
得分 100
学习任务

Felixxx 的学生作业:

write: #include #include #include #include #include #include #include #define PATHNAME "." #define PRO_ID 100 int main(int argc,char *argv[]) { key_t key; int shmid,ret; void *addr = NULL; int fd; if(argc !=2) { perror("[ERROR] argc"); exit(EXIT_FAILURE); } key = ftok(PATHNAME,PRO_ID); if(key == -1) { perror("[ERROR] ftok(): "); exit(EXIT_FAILURE); } fd = open(argv[1],O_RDONLY); if(fd == -1) { perror("[ERROR] open():"); exit(EXIT_FAILURE); } struct stat st; if(fstat(fd,&st) == -1) { perror("[ERROR] fstat(): "); close(fd); exit(EXIT_FAILURE); } shmid = shmget(key,st.st_size,IPC_CREAT | 0666); if(shmid == -1) { perror("[ERROR] shmget(): "); close(fd); exit(EXIT_FAILURE); } char *shm = shmat(shmid,NULL,0); if(shm == (void *)-1) { perror("[ERROR] shmat(): "); close(fd); exit(EXIT_FAILURE); } ssize_t n = read(fd,shm,st.st_size); if(n == -1) { perror("[ERROR] read(): "); shmdt(shm); close(fd); exit(EXIT_FAILURE); } if(n != st.st_size) { fprintf(stderr,"读取不完全"); } close(fd); printf("文件已读取到共享内存:%d\n",shmid); shmdt(shm); return 0; } read: #include #include #include #include #include #include #include #include #define PATHNAME "." #define PRO_ID 100 int main(int argc,char *argv[]) { key_t key; int shmid; if(argc != 2) { perror("[ERROR] argc:"); exit(EXIT_FAILURE); } key = ftok(PATHNAME,PRO_ID); if(key == -1) { perror("[ERROR] ftok(): "); exit(EXIT_FAILURE); } shmid = shmget(key,0,0666); if(shmid == -1) { perror("[ERROR] shmget(): "); exit(EXIT_FAILURE); } struct shmid_ds shm_info; if(shmctl(shmid,IPC_STAT,&shm_info) == -1) { perror("[ERROR] shmctl(): "); exit(EXIT_FAILURE); } char *shm = shmat(shmid,NULL,0); if(shm == (void *)-1) { perror("[ERROR] shmat(): "); exit(EXIT_FAILURE); } FILE *fp = fopen(argv[1],"wb"); if(fp == NULL) { perror("[ERROR] fopen(): "); shmdt(shm); exit(EXIT_FAILURE); } size_t written = fwrite(shm,1,shm_info.shm_segsz,fp); if(written != shm_info.shm_segsz) fprintf(stderr,"未完全写入"); fclose(fp); printf("文件保存成功\n"); shmdt(shm); return 0; } 【图片】

得分 100
学习任务

qq_康斯坦丁_0 的学生作业:

#include #include #include #include // 1. 定义 Person 结构体 struct Person { char name[50]; int age; }; // 线程函数:创建并返回一个 Person 结构体指针 void* create_person_in_thread(void* arg) { printf(“子线程: 开始执行…\n”); // 2. 在堆上动态分配内存给 Person 结构体 // 这是关键!如果在这里使用局部变量 (struct Person p;), // 那么当线程退出时,这块内存会被销毁,主线程将收到一个无效的指针。 struct Person* person_ptr = (struct Person*)malloc(sizeof(struct Person)); if (person_ptr == NULL) { perror("子线程: malloc 失败"); return NULL; // 返回 NULL 表示失败 } printf("子线程: 已在堆上为 Person 分配内存,地址: %p\n", person_ptr); // 3. 填充结构体数据 strcpy(person_ptr->name, "Alice"); person_ptr->age = 30; printf("子线程: 已填充数据 (Name: %s, Age: %d)\n", person_ptr->name, person_ptr->age); printf("子线程: 执行完毕,即将退出并返回结构体指针。\n"); // 4. 返回指向新创建的结构体的指针 // pthread_join 将会捕获这个返回值 return (void*)person_ptr; } int main() { pthread_t tid; void* thread_return_value = NULL; int ret; printf("主线程: 准备创建子线程...\n"); // 5. 创建一个可结合的 (joinable) 线程 // 我们必须使用可结合的线程,因为我们需要调用 pthread_join 来获取其返回值。 ret = pthread_create(&tid, NULL, create_person_in_thread, NULL); if (ret != 0) { perror("主线程: 创建线程失败"); exit(EXIT_FAILURE); } printf("主线程: 线程已创建,现在等待它完成...\n"); // 6. 等待子线程结束,并接收它返回的指针 // 第二个参数是 void** 类型,它会接收线程函数返回的 void* 指针。 ret = pthread_join(tid, &thread_return_value); if (ret != 0) { perror("主线程: pthread_join 失败"); exit(EXIT_FAILURE); } printf("主线程: 子线程已结束。\n"); // 7. 检查返回值并进行处理 if (thread_return_value != NULL) { // 将 void* 指针强制转换回正确的类型 struct Person* received_person = (struct Person*)thread_return_value; printf("\n--- 主线程打印接收到的数据 ---\n"); printf("接收到的 Person 地址: %p\n", received_person); printf("姓名: %s\n", received_person->name); printf("年龄: %d\n", received_person->age); printf("------------------------------\n\n"); // 8. 释放内存 // 由于内存是在子线程中通过 malloc 分配的, // 主线程在使用完毕后有责任释放它,以防止内存泄漏。 printf("主线程: 打印完毕,释放子线程分配的内存。\n"); free(received_person); received_person = NULL; // 好习惯:防止悬挂指针 } else { printf("主线程: 从子线程接收到 NULL,可能发生了错误。\n"); } printf("主线程: 任务完成,退出。\n"); return 0; }

得分 100
学习任务

Felixxx 的学生作业:

#include #include #include #include #include #include #include #include #define PATHNAME "." #define PRO_ID 10 #define MSG_TYPE_A 100 #define MSG_TYPE_B 200 #define MSG_SZ 64 struct msgbuf { long mtype; char mtext[MSG_SZ]; }; void main() { pid_t cpid; key_t key; int msgid,retA,retB; char buffer[MSG_SZ]; ssize_t rbytes; struct msgbuf msgA; struct msgbuf msgB; struct msgbuf rcv_msg; key = ftok(PATHNAME,PRO_ID); if(key == -1) { perror("[ERROR] ftok(): "); exit(EXIT_FAILURE); } msgid = msgget(key,IPC_CREAT | 0666); if(msgid == -1) { perror("[ERROR] msgget(): "); exit(EXIT_FAILURE); } printf("msgid is %d \n",msgid); cpid = fork(); if(cpid == -1) { perror("[ERROR] fork():"); exit(EXIT_FAILURE); } else if(cpid == 0) { while(1) { rbytes = msgrcv(msgid,(void *)&rcv_msg,MSG_SZ,MSG_TYPE_A,0); if(rbytes == -1) { perror("[ERROR] msgcv():"); exit(EXIT_FAILURE); } printf("进程A mtype: %ld\n",rcv_msg.mtype); printf("进程A mtext: %s\n",rcv_msg.mtext); } } else if(cpid > 0) { cpid = fork(); if(cpid == -1) { perror("[ERROR] fork():"); exit(EXIT_FAILURE); } else if(cpid == 0) { while(1) { rbytes = msgrcv(msgid,(void *)&rcv_msg,MSG_SZ,MSG_TYPE_B,0); if(rbytes == -1) { perror("[ERROR] msgcv():"); exit(EXIT_FAILURE); } printf("进程B mtype: %ld\n",rcv_msg.mtype); printf("进程B mtext: %s\n",rcv_msg.mtext); } } else if(cpid > 0) { while(1) { fgets(buffer,sizeof(buffer),stdin); buffer[strcspn(buffer,"\n")] = '\0'; if(strcmp(buffer,"quit") == 0) exit(EXIT_SUCCESS); msgA.mtype = MSG_TYPE_A; msgB.mtype = MSG_TYPE_B; strcpy(msgA.mtext,buffer); strcpy(msgB.mtext,buffer); retA = msgsnd(msgid,(const void *)&msgA,strlen(msgA.mtext)+1,0); retB = msgsnd(msgid,(const void *)&msgB,strlen(msgB.mtext)+1,0); if(retA == -1 || retB == -1) { perror("[ERROR] msgsnd(): "); exit(EXIT_FAILURE); } } } } }

得分 100
讨论题

cjozGV 的学生作业:

#include "stdio.h" #include "stdlib.h" #include typedef struct { int data[1024]; int top; } Stack; //初始化栈 void init_stack(Stack *s){ s->top = -1; } //检查栈是否为空 int is_empty(Stack *s){ return s->top == -1; } //压栈 void push(Stack *s,int value){ s->data[++s->top] = value; } //出栈 int pop(Stack *s){ return s->data[s->top--]; } //查看栈顶元素 int peek(Stack *s){ return s->data[s->top]; } //运算符优先级和计算函数 int get_level(char op){ switch (op) { case '+': case '-': return 1; case '*': case '/': return 2; default: return -1; } } //根据运算符执行相应的计算 int compute(int a,int b,int op){ switch (op) { case '+': return a + b; case '-': return a - b; case '*': return a * b; case '/': return a / b; default: return 0; } } //表达式求值函数 int evaluate_expression(const char *expr){ //operands操作数字 operators操作字符 初始化两个栈 Stack operands,operators; init_stack(&operands); init_stack(&operators); while (*expr) { if (isdigit(*expr)) { int num = 0; while (isdigit(*expr)) { num = num * 10 + (*expr - '0'); expr++; } push(&operands, num); continue; } if (*expr == '(') { push(&operators, *expr); } else if (*expr == ')') { while (!is_empty(&operators) && peek(&operators) != '(') { int b = pop(&operands); //从操作数栈弹出 int a = pop(&operands); //从操作数栈弹出 char op = pop(&operators); //从操作符栈弹出 push(&operands, compute(a, b, op)); // 结果压入操作数栈 } pop(&operators); //弹出左括号 } else if (*expr == '+' || *expr == '-' || *expr == '*' || *expr == '/') { //处理运算符:先计算栈中优先级 >= 当前运算符的运算 while (!is_empty(&operators) && get_level(peek(&operators)) >= get_level(*expr)) { int b = pop(&operands); int a = pop(&operands); char op = pop(&operators); push(&operands, compute(a, b, op)); } push(&operators, *expr); //当前运算符压栈 } expr++; //处理下一个字符 } // 处理剩余运算符 while (!is_empty(&operators)) { int b = pop(&operands); int a = pop(&operands); char op = pop(&operators); push(&operands, compute(a, b, op)); } return pop(&operands); // 最终结果在操作数栈顶 } int main() { const char *expression = "(4+8)*2-3"; int result = evaluate_expression(expression); printf("Result: %d\n", result); return 0; }

得分 100
讨论题

cjozGV 的学生作业:

#include "stdio.h" #include "stdlib.h" #include typedef struct { int data[1024]; int top; } Stack; //初始化栈 void init_stack(Stack *s){ s->top = -1; } //检查栈是否为空 int is_empty(Stack *s){ return s->top == -1; } //压栈 void push(Stack *s,int value){ s->data[++s->top] = value; } //出栈 int pop(Stack *s){ return s->data[s->top--]; } //查看栈顶元素 int peek(Stack *s){ return s->data[s->top]; } //运算符优先级和计算函数 int get_level(char op){ switch (op) { case '+': case '-': return 1; case '*': case '/': return 2; default: return -1; } } //根据运算符执行相应的计算 int compute(int a,int b,int op){ switch (op) { case '+': return a + b; case '-': return a - b; case '*': return a * b; case '/': return a / b; default: return 0; } } //表达式求值函数 int evaluate_expression(const char *expr){ //operands操作数字 operators操作字符 初始化两个栈 Stack operands,operators; init_stack(&operands); init_stack(&operators); while (*expr) { if (isdigit(*expr)) { int num = 0; while (isdigit(*expr)) { num = num * 10 + (*expr - '0'); expr++; } push(&operands, num); continue; } // 如果是运算符,处理优先级并压入运算符栈 if (*expr == '+' || *expr == '-' || *expr == '*' || *expr == '/'){ //如果栈顶运算符的优先级大于或等于当前运算符,则先计算栈顶的运算 while (!is_empty(&operators) && get_level(peek(&operators)) >= get_level(*expr)){ int b = pop(&operands); int a = pop(&operands); char op = pop(&operators); push(&operands, compute(a,b,op)); } push(&operators,*expr); } expr++; } //处理剩余的运算符 while (!is_empty(&operators)){ int b = pop(&operands); int a = pop(&operands); char op = pop(&operators); push(&operands, compute(a,b,op)); } return pop(&operands); } int main() { const char *expression = "1+123*5"; int result = evaluate_expression(expression); printf("Result: %d\n", result); return 0; }

微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号