一、思维导图
二、作业第一题
现在有2个.c 文件 1.c负责输入2个非0数,a 和 b 2.c负责找出 a 到 b 之间的所有质数 要求使用无名管道实现
#include <myhead.h>
typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;
int main(int argc, const char *argv[])
{
int arr[2] = {0};
int res = pipe(arr);
if(res == -1){
perror("pipe");
return 1;
}
res = fork();
if(res > 0){
//父进程
close(arr[0]); //关闭读端
while(1){
int buf[2] = {0};
printf("请输入2个非0数:");
scanf("%d %d", buf, buf+1);
while(getchar()!=10);
write(arr[1], buf, sizeof(buf));
usleep(1000);
}
}else if(res == 0){
//子进程
execl("./child", "child", NULL);
}else{
perror("fork");
return 1;
}
return 0;
}
#include <myhead.h>
typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;
int main(int argc, const char *argv[])
{
close(4);
while(1){
int buf[2] = {0};
read(3, buf, sizeof(buf));
printf("所有质数: ");
for(int i=buf[0]; i<=buf[1]; i++){
if(i == 2 || i == 3){
printf("%d ", i);
continue;
}
for(int j=2; j<i; j++){
if(i % j == 0){
printf("%d ", i);
}
}
}
putchar(10);
}
return 0;
}
三、作业第二题
创建一对父子 父进程负责输入一串字符串 子进程负责判断这串字符串是否为回文字符串
#include <myhead.h>
typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;
int main(int argc, const char *argv[])
{
int arr[2] = {0};
int res = pipe(arr);
if(res == -1){
perror("pipe");
return 1;
}
res = fork();
if(res > 0){
// 父进程
close(arr[0]); //关闭读端
while(1){
char buf[64] = {0};
printf("请输入一串字符串:");
scanf("%s", buf);
while(getchar() != 10);
write(arr[1], buf, 64);
usleep(1000);
}
}else if(res == 0){
// 子进程
close(arr[1]); //关闭写端
while(1){
char buf[64] = {0};
read(arr[0], buf, 64);
int left = 0, right = strlen(buf) - 1;
int flg = 0;
while(left < right){
if(buf[left] != buf[right]){
flg = 1;
}
}
if(flg == 1){
printf("这是回文字符串\n");
}else{
printf("这不是回文字符串\n");
}
}
}
return 0;
}
四、作业第三题
有2个.c文件,每个.c文件都拥有一对父子进程,总共4个进程 A a B b 现在要求实现一个多米诺骨牌的效果: 按ctrl+c结束a进程的运行,a进程结束运行之前,通过kill函数向b进程发送SIGINT信号,b进程死亡后,B进程回收b进程的资源后,使用kill函数向A进程发送SIGTSTP信号后,结束运行。A进程接受到B进程的SIGTSTP信号后,会后a进程的资源后也结束运行 注意:kill函数要求获得另一个进程的pid,使用文件IO或者管道都可以
#include <myhead.h>
typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;
void handler(int signum){
if(signum == SIGINT){
int rfd = open("./myfifoA", O_RDONLY);
int b_pid = 0;
read(rfd, &b_pid, 4);
sleep(1);
kill(b_pid, SIGINT);
close(rfd);
exit(0);
}else if(signum == SIGTSTP){
while(waitpid(-1, 0, WNOHANG) != -1);
printf("A进程结束运行\n");
exit(0);
}
}
int main(int argc, const char *argv[])
{
if(access("./myfifoA", F_OK) == -1){
mkfifo("./myfifoA", 0666);
}
if(access("./myfifoB", F_OK) == -1){
mkfifo("./myfifoB", 0666);
}
int res = fork();
if(res > 0){
//A进程
signal(SIGINT, SIG_IGN); //忽视信号
signal(SIGTSTP, handler);
int wfd = open("./myfifoB", O_WRONLY | O_TRUNC);
int A_pid = getpid();
write(wfd, &A_pid, 4);
close(wfd);
}else{
//a进程
signal(SIGINT, SIG_IGN);
}
while(1);
return 0;
}
#include <myhead.h>
typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;
void handler(int signum){
if(signum == SIGINT){
printf("b进程结束运行\n");
sleep(1);
exit(0);
}else if(signum == SIGCHLD){
printf("B进程结束运行\n");
int A_pid = 0;
int rfd = open("./myfifoB", O_RDONLY);
read(rfd, &A_pid, 4);
close(rfd);
while(waitpid(-1, 0, WNOHANG) != -1);
sleep(1);
kill(A_pid, SIGTSTP);
exit(0);
}
}
int main(int argc, const char *argv[])
{
if(access("./myfifoA", F_OK) == -1){
mkfifo("./myfifoA", 0666);
}
if(access("./myfifoB", F_OK) == -1){
mkfifo("./myfifoB", 0666);
}
int res = fork();
if(res > 0){
//B进程
signal(SIGCHLD, handler);
}else{
//b进程
signal(SIGINT, handler);
int wfd = open("./myfifoA", O_WRONLY | O_TRUNC);
int b_pid = getpid();
write(wfd, &b_pid, 4);
close(wfd);
}
while(1);
return 0;
}
五、作业第四题
使用有名管道,实现2个进程之间的互相聊天 注意:一定是一方可以一直发消息,另一方不回消息,或者在任意时间随意回复几条消息。千万不能是一方发送消息后,去等待另一方的回复后才能发送另一条消息
#include <myhead.h>
typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;
void *run(void *arg){
int rfd = open("./myfifo2", O_RDONLY);
while(1){
char buf[64] = {0};
int res = read(rfd, buf, 64);
if(res == 0){
exit(0);
}
printf("\b\b\b\b\b\b\b读取到的数据为:%s\n", buf);
printf("请输入:");
fflush(stdout);
}
}
int main(int argc, const char *argv[])
{
if(access("./myfifo1", F_OK) == -1){
mkfifo("./myfifo1", 0666);
}
if(access("./myfifo2", F_OK) == -1){
mkfifo("./myfifo2", 0666);
}
pthread_t id;
pthread_create(&id, 0, run, 0);
pthread_detach(id);
int wfd = open("./myfifo1", O_WRONLY | O_TRUNC);
while(1){
char buf[64] = {0};
printf("请输入:");
scanf("%64s", buf);
while(getchar() != 10);
write(wfd, buf, strlen(buf));
}
close(wfd);
return 0;
}
#include <myhead.h>
typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;
void *run(void *arg){
int rfd = open("./myfifo1", O_RDONLY);
while(1){
char buf[64] = {0};
int res = read(rfd, buf, 64);
if(res == 0){
exit(0);
}
printf("\033[7D读取到的数据为:%s\n", buf);
printf("请输入:");
fflush(stdout);
}
close(rfd);
}
int main(int argc, const char *argv[])
{
if(access("./myfifo1", F_OK) == -1){
mkfifo("./myfifo1", 0666);
}
if(access("./myfifo2", F_OK) == -1){
mkfifo("./myfifo2", 0666);
}
pthread_t id;
pthread_create(&id, 0, run, 0);
pthread_detach(id);
int wfd = open("./myfifo2", O_WRONLY | O_TRUNC);
while(1){
char buf[64] = {0};
printf("请输入:");
scanf("%64s", buf);
while(getchar() != 10);
write(wfd, buf, strlen(buf));
}
return 0;
}