
作业社区
探索学习新天地,共享知识资源!
阿大月 的学生作业:
calc.c: int add(int x, int y) { return x + y; } int sub(int x, int y) { return x - y; } int multiply(int x, int y) { return x * y; } int divide(int x, int y) { return x / y; } head.h: #ifndef __HEAD_DEF__ #define __HEAD_DEF__ #include extern int add(int x, int y); extern int sub(int x, int y); extern int multiply(int x, int y); extern int divide(int x, int y); #endif main.c #include "head.h" int main(int argc, char *arcv[]) { int x, y; printf("Please enter two integers: "); scanf("%d%d", &x, &y); printf("两个数的和是:%d\n", add(x, y)); printf("两个数的差是:%d\n", sub(x, y)); printf("两个数的积是:%d\n", multiply(x, y)); printf("两个数的商是:%d\n", divide(x, y)); return 0; }





大禹123 的学生作业:
#include #include #include #include #include #include #define CMD_SIZE 128 #define ARG_NUM 4 typedef struct { char *cmd_arg_list[ARG_NUM]; int arg_count; }cmd_info_t; int parse_cmd_str(char *pcmdstr, cmd_info_t *pcmd_info) { if(NULL == pcmdstr) return -1; char *p_cmd_arg = NULL; int i = 0; p_cmd_arg = strtok(pcmdstr, " "); while(p_cmd_arg!=NULL) { pcmd_info->cmd_arg_list[i++] = p_cmd_arg; pcmd_info->arg_count++; p_cmd_arg = strtok(NULL," "); } return 0; } int start_cmd_fork(cmd_info_t *pcmd_info) { pid_t cpid; int i = 0; cpid = fork(); if(cpid == -1) { perror("[ERROR] fork():"); }else if(cpid == 0) { int ret; ret = execvp(pcmd_info->cmd_arg_list[0], pcmd_info->cmd_arg_list); if(ret == -1) { perror("[ERROR] execvp():"); exit(EXIT_FAILURE); } exit(EXIT_SUCCESS); //子进程 }else if(cpid>0) { wait(NULL); //父进程 } return 0; } int cmd_execute(char *pcmdstr) { cmd_info_t cmdinfo; if(NULL == pcmdstr) return -1; memset(&cmdinfo, 0, sizeof(cmdinfo)); cmdinfo.arg_count = 0; if(parse_cmd_str(pcmdstr, &cmdinfo) == -1) return -1; if(start_cmd_fork(&cmdinfo) == -1) return -1; return 0; } int main() { char command[CMD_SIZE] = {0}; while(1) { printf("minishell:"); fgets(command, CMD_SIZE, stdin); //去除'\n' command[strlen(command) - 1] = '\0'; if(strncmp(command, "quit", 4) == 0) break; cmd_execute(command); } return 0; }





大禹123 的学生作业:
#include #include #include #include #include int main() { pid_t pcid_A = fork(); if(pcid_A == -1) { //创建失败 perror("[ERROR]: fork():"); exit(EXIT_FAILURE); }else if(pcid_A == 0) { //子进程A printf("child process A \n", getpid()); sleep(5); exit(66); }else if(pcid_A>0) { //父进程 pid_t pcid_B = fork(); if(pcid_B == -1) { //创建失败 perror("[ERROR]: fork():"); exit(EXIT_FAILURE); }else if(pcid_B == 0) { //子进程B printf("child process B \n", getpid()); sleep(5); exit(88); }else if(pcid_B>0) { //父进程 int status_A = 0, status_B = 0; pid_t rcpid_A = 0, rcpid_B = 0; while((rcpid_B = waitpid(pcid_B, &status_B, WNOHANG))==0); printf("rcpid_B:%d exit code:%d\n", rcpid_B, WEXITSTATUS(status_B)); while((rcpid_A = waitpid(pcid_A, &status_A, WNOHANG))==0); printf("rcpid_A:%d exit code:%d\n", rcpid_A, WEXITSTATUS(status_A)); } } return 0; }




