作业社区
探索学习新天地,共享知识资源!
城仔 的学生作业:
#include #include #include #include #include int main() { pid_t cpid,rpid; int statusA,statusB; cpid = fork(); if(cpid == -1){ perror("[error] fork()"); exit(EXIT_FAILURE); }else if(cpid == 0){ printf("the cpid < %d > running ...\n",getpid()); sleep(2); exit(EXIT_SUCCESS); }else if(cpid > 0){ waitpid(cpid,&statusA,0); rpid = fork(); if(rpid == -1){ perror("[error] for()"); exit(EXIT_FAILURE); }else if(rpid == 0){ printf("the rpid < %d > running ...\n",getpid()); sleep(5); exit(EXIT_SUCCESS); }else if(rpid > 0 ){ waitpid(rpid,&statusB,0); } printf("the child process < %d > has exit code < %d >.\n",cpid,WEXITSTATUS(statusA)); printf("the child process < %d > has exit code < %d >.\n",rpid,WEXITSTATUS(statusB)); } return 0; } 【图片】
+114
weixin_慕九州3042664 的学生作业:
#include #include #include #include int main(void) { char cmd[64] = {0}; while(1) { memset(cmd, 0, sizeof(cmd)); scanf("%s", cmd); if(strcmp(cmd, "q") == 0 || strcmp(cmd, "Q") == 0) { break; } if(strcmp(cmd, "") == 0) { continue; } int pid = fork(); if(pid < 0) { perror("fork"); exit(-1); } else if(pid == 0) { char *str[10] = {NULL}; int count = 0; str[0] = strtok(cmd, " \t"); for(int i = 1; i < 10; i++) { str[i] = strtok(NULL, " \t"); if(str[i] == NULL) { break; count = i; } } execvp(str[0], str); } } return 0; }
weixin_慕九州3042664 的学生作业:
#include #include #include #include int main(void) { int pid1 = fork(); if(pid1 < 0) { perror("fork pid1"); exit(-1); } else if(pid1 == 0) { printf("process 1(%d) is running\n", getpid()); sleep(2); exit(0); } else { int pid2 = fork(); if(pid2 < 0) { perror("fork pid2"); exit(-1); } else if(pid2 == 0) { printf("process 2(%d) is running\n", getpid()); sleep(5); exit(0); } else { printf("waiting for child process\n"); int status;; waitpid(pid1, &status, 0); waitpid(pid2, &status, 0); exit(0); } } return 0; }