
作业社区
探索学习新天地,共享知识资源!
慕运维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!





慕运维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; }





慕运维8597106 的学生作业:
#include #include #include #include #include #include #define COMMADN_MAX 1024 #define ARGS_MAX 5 char * s_gets(char *str, int n); char ** parse_command(char *str, char **args, int args_number); int main(void) { pid_t pid; char command[COMMADN_MAX]; char *args[ARGS_MAX + 1]; while (s_gets(command, COMMADN_MAX)) { parse_command(command, args, ARGS_MAX); if ((pid = fork()) == -1) { perror("Fork: "); exit(EXIT_FAILURE); } else if (pid == 0) { if (execvp(args[0], args) == -1) { perror("Execvp: "); exit(EXIT_FAILURE); } exit(900); } int status; waitpid(pid, &status, 0); printf("Child process %d exit status %d\n", pid, WEXITSTATUS(status)); } return 0; } // 获取输入去掉换行符 char * s_gets(char *str, int n) { char *ret_val = fgets(str, n, stdin); if (ret_val) { char *find = strchr(str, '\n'); if (find) *find = '\0'; else while (getchar() != '\n') continue; } return ret_val; } // 解析输入并存储 char ** parse_command(char *str, char **args, int args_number) { char *temp; int index = 0; if (str) { temp = strtok(str, " "); while (temp && index < args_number) { args[index++] = temp; temp = strtok(NULL, " "); } } args[index] = NULL; return args; }





浪潮君 的学生作业:
#include int main() { int a[5] = {1, 3, 5, 7, 9}; int *p = NULL; int **q = NULL; // 通过p输出数组内容 p = a; printf("Output by p:\n"); for (int i = 0; i < 5; i++) { printf("%d ", *(p + i)); } printf("\n"); // 通过q输出数组内容 q = &p; printf("Output by q:\n"); for (int i = 0; i < 5; i++) { printf("%d ", *(*q + i)); } printf("\n"); return 0; }
浪潮君 的学生作业:
#include int main() { int a[5] = {10, 20, 30, 40, 50}; int *p = a; printf("使用 *(a + i) 输出数组元素:\n"); for (int i = 0; i < 5; i++) { printf("%d ", *(a + i)); } printf("\n"); printf("使用 a[i] 输出数组元素:\n"); for (int i = 0; i < 5; i++) { printf("%d ", a[i]); } printf("\n"); printf("使用 *(p + i) 输出数组元素:\n"); for (int i = 0; i < 5; i++) { printf("%d ", *(p + i)); } printf("\n"); printf("使用 p[i] 输出数组元素:\n"); for (int i = 0; i < 5; i++) { printf("%d ", p[i]); } printf("\n"); return 0; }





浪潮君 的学生作业:
01 #include 02 03 int main() { 04 int a[5] = {0}; // 定义数组并初始化 05 int *p_max = NULL; // 定义指针变量 06 07 // 输入数组元素 08 printf(“请输入5个整数:\n”); 09 for (int i = 0; i < 5; i++) { 10 scanf("%d", &a[i]); 11 } 12 13 // 找最大值的地址 14 p_max = &a[0]; // 先让p_max指向第一个元素 15 for (int i = 1; i < 5; i++) { 16 if (a[i] > *p_max) { 17 p_max = &a[i]; // 更新最大值地址 18 } 19 } 20 21 // 输出最大值 22 printf(“最大值是:%d\n”, *p_max); 23 24 return 0; 25 } #include int main() { unsigned int data = 0x11223344; unsigned short *q = NULL; unsigned short t1 = 0; unsigned short t2 = 0; // (1) q保存data的地址 q = (unsigned short *)&data; // (2) 读取低2字节赋值给t1,高2字节赋值给t2 t1 = *(q); // 低2字节 t2 = *(q + 1); // 高2字节 // (3) 输出t1和t2的和与差 printf("t1 = 0x%04x\n", t1); printf("t2 = 0x%04x\n", t2); printf("t1 + t2 = 0x%04x\n", t1 + t2); printf("t1 - t2 = 0x%04x\n", t1 - t2); return 0; }




