
作业社区
探索学习新天地,共享知识资源!
weixin_慕姐6158179 的学生作业:
#include #include //线程库 #include using namespace std; vector parse_line(string &line){ stringstream ss(line); vector cmd; while(getline(cin,token,' ')){ cmd.push_back(token); } return cmd; } void excute_cmd(vector &cmd){ pid_t pid=fork(); if(pid





Linkus 的学生作业:
该作业位于 进程间信号的发送。由于位置错了就直接交对应练习了 /* 创建两个子进程,由父进程分别给两个子进程发送 SIGKILL 信号 */ #include #include #include #include #include int main(void) { pid_t cpida,cpidb; cpida = fork(); if(cpida == -1){ perror("[ERROR] fork():"); exit(EXIT_FAILURE); } if(cpida == 0) { printf("cpida: raise(SIGSTOP)\n"); raise(SIGSTOP); exit(0); } cpidb = fork(); if(cpidb == -1){ perror("[ERROR] fork():"); exit(EXIT_FAILURE); } if(cpidb == 0) { printf("cpidb: pause()\n"); pause(); exit(0); } sleep(1); printf("kill cpida.\n"); kill(cpida,SIGKILL); printf("kill cpidb.\n"); kill(cpidb,SIGKILL); printf("Main process end.\n"); return 0; }
Linkus 的学生作业:
这个作业位于:进程间通信,有名管道, 练习: 设计两个没有血缘关系的进程,使用有名管道一个进程获取当前系统时间给另外一个进程 #include #include #include #include #include #include #include #include int main() { int fd; char *fifo_2 = "./fifo_2"; char time_str[100]; if(access(fifo_2,F_OK) == -1) mkfifo(fifo_2,0644); fd = open(fifo_2,O_RDONLY); if (fd == -1){ perror("[ERROR] open():"); exit(EXIT_FAILURE); } read(fd,time_str,sizeof(time_str)); printf("Received: %s\n",time_str); close(fd); return 0; } #include #include #include #include #include #include #include #include #define FIFO "./fifo_2" int main(void) { time_t now; char time_str[100]; int fd; int wbytes; if(access(FIFO,F_OK) == -1) mkfifo(FIFO,0644); fd = open(FIFO,O_WRONLY); if (fd == -1){ perror("[ERROR] open():"); exit(EXIT_FAILURE); } time(&now); snprintf(time_str,sizeof(time_str),"Current time from 2_b : %s",ctime(&now)); wbytes = write(fd,time_str,strlen(time_str) + 1); if (wbytes < 0) perror("write():"); close(fd); return 0; }
FishKylin 的学生作业:
#define _DEFAULT_SOURCE #include #include #include #include #include #include #include static void handler(int sig) { printf("捕获信号 (%d):%s\n", sig, strsignal(sig)); } int main(void) { pid_t apid, bpid; int status; if ((apid = fork()) == -1) { perror("fork: "); exit(EXIT_FAILURE); } else if (apid == 0) { if (signal(SIGUSR1, SIG_DFL) == SIG_ERR) { perror("signal: "); exit(EXIT_FAILURE); } pause(); exit(EXIT_SUCCESS); } else { sleep(2); kill(apid, SIGUSR1); waitpid(apid, &status, 0); printf("%d 退出状态为:%d\n", apid, WEXITSTATUS(status)); } if ((bpid = fork()) == -1) { perror("fork: "); exit(EXIT_FAILURE); } else if (bpid == 0) { if (signal(SIGUSR2, handler) == SIG_ERR) { perror("signal: "); exit(EXIT_FAILURE); } pause(); exit(EXIT_SUCCESS); } else { sleep(3); kill(bpid, SIGUSR2); waitpid(bpid, &status, 0); printf("%d 退出状态为:%d\n", bpid, WEXITSTATUS(status)); } return 0; }





FishKylin 的学生作业:
#define _DEFAULT_SOURCE #include #include #include #include #include #include int main(void) { pid_t apid, bpid; int status; if ((apid = fork()) == -1) { perror("fork: "); exit(EXIT_FAILURE); } else if (apid == 0) { pause(); exit(EXIT_SUCCESS); } else { sleep(2); kill(apid, SIGKILL); waitpid(apid, &status, 0); printf("%d 退出状态为:%d\n", apid, WEXITSTATUS(status)); } if ((bpid = fork()) == -1) { perror("fork: "); exit(EXIT_FAILURE); } else if (bpid == 0) { pause(); exit(EXIT_SUCCESS); } else { sleep(3); kill(bpid, SIGKILL); waitpid(bpid, &status, 0); printf("%d 退出状态为:%d\n", bpid, WEXITSTATUS(status)); } return 0; }





慕粉8395673 的学生作业:
练习1: 1 #include 2 3 int main() 4 { 5 int a[5] = {0}; 6 int *p_max; 7 int i = 0; 8 9 printf("请输入5个正整数,以空格分开:\n"); 10 scanf("%d %d %d %d %d",&a[0],&a[1],&a[2],&a[3],&a[4]); 11 printf("%d %d %d %d %d\n",a[0],a[1],a[2],a[3],a[4]); 12 13 p_max = &a[0]; 14 for(i = 1;i < 5;i++) 15 { 16 if(*p_max < a[i]) 17 { 18 *p_max = a[i]; 19 } 20 } 21 printf ("数组中最大值是: %d\n", *p_max); 22 23 return 0; 24 } 请输入5个正整数,以空格分开: 11 15 32 135 100 11 15 32 135 100 数组中最大值是: 135 练习2: 1 #include 2 3 int main() { 4 5 unsigned int data = 0x11223344; 6 unsigned short *q = NULL; 7 unsigned short t1 = 0; 8 unsigned short t2 = 0; 9 unsigned int sum; 10 unsigned int sub; 11 q = (unsigned short *)&data; 12 13 t1 = *q; 14 t2 = *(q + 1); 15 16 sum = (unsigned int) t1 + (unsigned int) t2; 17 sub = (unsigned int)t1 -(unsigned int) t2; 18 19 printf("t1 和 t2 的和为: %u\n", sum); 20 printf("t1 和 t2 的差为: %u\n", sub); 21 22 return 0; 23 } t1 和 t2 的和为: 17510 t1 和 t2 的差为: 8738




