第一题:现在有2个.c 文件 1.c负责输入2个非0数,a 和 b 2.c负责找出 a 到 b 之间的所有质数 要求使用无名管道实现
#include "/home/a/Desktop/test/gencls/GenFunc.h"
void signalMain(int sig)
{
if(SIGCHLD == sig)
while(-1 != waitpid(-1,0,WNOHANG));
}
int main(int argc, const char *argv[])
{
signal(SIGCHLD,signalMain);
int fd[2];
pipe(fd);
int pid = fork();
if(0 == pid)
{
close(fd[1]);
char cfd[16]={0};
sprintf(cfd,"%d",fd[0]);
execl("./1.out","./1.out",cfd,NULL);
}else if(0 < pid)
{
close(fd[0]);
printf("输入:");
char str[1024]={0};
GetLineFunc(str,1023);
write(fd[1],str,strlen(str));
}if(pid < 0) return -1;
wait(0);
return 0;
}
第二题:创建一对父子
父进程负责输入一串字符串
子进程负责判断这串字符串是否为回文字符串
#include "/home/a/Desktop/test/gencls/GenFunc.h"
void signalMain(int sig)
{
if(SIGCHLD == sig)
while(-1 != waitpid(-1,0,WNOHANG));
}
int isPalindrome(char str[], int length) {
int start = 0;
int end = length - 1;
while(start < end) {
if(str[start] != str[end]) {
return 0;
}
start++;
end--;
}
return 1; // 所有字符都匹配,则是回文
}
int main(int argc, const char *argv[])
{
signal(SIGCHLD,signalMain);
int fd[2];
pipe(fd);
int pid = fork();
if(0 == pid)
{
close(fd[1]);
char str[1024]={0};
read(fd[0],str,1023);
if(1 == isPalindrome(str,strlen(str)))
printf("是回文字符串\n");
else
printf("不是回文字符串\n");
}else if(0 < pid)
{
close(fd[0]);
printf("输入:");
char str[1024]={0};
GetLineFunc(str,1023);
write(fd[1],str,strlen(str));
}if(pid < 0) return -1;
wait(0);
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 "/home/a/Desktop/test/gencls/GenFunc.h"
int fd1 = 0,fd2=0;
void signalMain(int sig)
{
if(SIGCHLD == sig)
while(-1 != waitpid(-1,0,WNOHANG));
else if(SIGINT == sig)
{
printf("a退出\n");
char str[1024]={0};
read(fd2,str,1023);
int pidn = atoi(str);
kill(pidn,SIGINT);
exit(0);
}else if(SIGTSTP == sig)
{
printf("A退出\n");
exit(0);
}
}
int main(int argc, const char *argv[])
{
signal(SIGCHLD,signalMain);
if(access("./pip1",0) < 0)
mkfifo("./pip1",0666);
if(access("./pip2",0) < 0)
mkfifo("./pip2",0666);
fd1 = open("./pip1",O_WRONLY|O_TRUNC);
fd2 = open("./pip2",O_RDONLY);
int pid = fork();
if(0 == pid)
{
signal(SIGINT,signalMain);
while(1);
}else if(0 < pid)
{
signal(SIGINT,SIG_IGN);
signal(SIGTSTP,signalMain);
int cpid = getpid();
char str[1024]={0};
sprintf(str,"%d",cpid);
write(fd1,str,strlen(str));
while(1);
}if(pid < 0)
{
close(fd1);
close(fd2);
return -1;
}
wait(0);
close(fd1);
close(fd2);
return 0;
}
第四题: 使用有名管道,实现2个进程之间的互相聊天 注意:一定是一方可以一直发消息,另一方不回消息,或者在任意时间随意回复几条消息。千万不能是一方发送消息后,去等待另一方的回复后才能发送另一条消息
#include "/home/a/Desktop/test/gencls/GenFunc.h"
void signalMain(int sig)
{
if(SIGCHLD == sig)
while(-1 != waitpid(-1,0,WNOHANG));
}
int main(int argc, const char *argv[])
{
signal(SIGCHLD,signalMain);
if(access("./pip1",0) < 0)
mkfifo("./pip1",0666);
if(access("./pip2",0) < 0)
mkfifo("./pip2",0666);
int fd1 = open("./pip1",O_WRONLY|O_TRUNC);
int fd2 = open("./pip2",O_RDONLY);
int pid = fork();
if(0 == pid)
{
while(1)
{
char str[1024]={0};
GetLineFunc(str,1023);
write(fd1,str,strlen(str));
}
}else if(0 < pid)
{
while(1)
{
char str[1024]={0};
read(fd2,str,1023);
printf("%s\n",str);
}
}if(pid < 0)
{
close(fd1);
close(fd2);
return -1;
}
wait(0);
close(fd1);
close(fd2);
return 0;
}