第一题:现在有2个.c 文件 1.c负责输入2个非0数,a 和 b 2.c负责找出 a 到 b 之间的所有质数 要求使用无名管道实现
1.c
int main(int argc, const char *argv[])
{
int pfd[2]={0};
int res=pipe(pfd);
if(res==-1)
{
perror("pipe");
return -1;
}
res=fork();
if(res>0)
{
close(pfd[0]);
while(1)
{
int buf[2]={0};
printf("请输入:");
scanf("%d %d",buf,buf+1);
while(getchar()!=10);
write(pfd[1],buf,sizeof(int)*2);
sleep(1);
}
}
else{
execl("./find_p","find_p",NULL);
}
return 0;
}
2.c
int main(int argc, const char *argv[])
{
close(4);
while(1)
{
int count=0;
int buff[2]={0};
read(3,buff,sizeof(int)*2);
int max=buff[0]>buff[1]?buff[0]:buff[1];
int min=buff[0]<buff[1]?buff[0]:buff[1];
int i=min+1;
while(i<max)
{
for(int j=2;j<i;j++)
{
if(i%j==0){
count++;
}
}
if(count==0){
printf("其中%d是质数\n",i);
}
i++;
count=0;
}
}
return 0;
}
第二题:创建一对父子 父进程负责输入一串字符串 子进程负责判断这串字符串是否为回文字符串
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/wait.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <arpa/inet.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 pfd[2]={0};
int res=pipe(pfd);
res=fork();
if(res>0)
{
close(pfd[0]);
while(1){
char str[20];
printf("请输入字符串:");
scanf("%s",str);//输入要判断的字符串
while(getchar()!=10);
write(pfd[1],str,sizeof(str));
usleep(1000);
}
}
else
{
close(pfd[1]);
while(1){
char str[20];
char buff[20];
read(pfd[0],str,sizeof(str));
int len=strlen(str);
buff[len]='\0';
for (int i=len-1;i>=0;i--)
{
buff[len-1-i]=str[i];
if (i==0)
{
if (strncmp(buff,str,20)==0)
printf("是回文\n");
else
{
printf("不是回文\n");
}
}
}
usleep(1000);
}
}
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或者管道都可以
第四题: 使用有名管道,实现2个进程之间的互相聊天 注意:一定是一方可以一直发消息,另一方不回消息,或者在任意时间随意回复几条消息。千万不能是一方发送消息后,去等待另一方的回复后才能发送另一条消息
1.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/wait.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <arpa/inet.h>
int main(int argc, const char *argv[])//发起聊天
{
// 判断是否管道存在
int ret = access("talk1", F_OK);
if(ret == -1){
printf("管道talk1不存在,创建管道\n");
// 创建管道
ret = mkfifo("talk1", 0666);
if(ret == -1){
perror("mkfifo");
return -1;
}
}
// 以只写方式打开自己创建的管道
int fd_out = open("talk1", O_WRONLY);
if(fd_out == -1){
perror("open");
return -1;
}
// 以只读方式打开要连接的管道
int fd_in = open("talk2", O_RDONLY);
if(fd_in == -1){
perror("open_w2");
return -1;
}
// 开始读写数据
while(1){
//发送消息
char buf_talk1_out[1024] = {0};
printf("发送消息:");
scanf("%s", buf_talk1_out);
write(fd_out, buf_talk1_out, strlen(buf_talk1_out));
//收到消息
memset(buf_talk1_out,0,sizeof(buf_talk1_out));
read(fd_in, buf_talk1_out, sizeof(buf_talk1_out));
printf("收到消息:%s\n", buf_talk1_out);
}
return 0;
}
2.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/wait.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <arpa/inet.h>
int main(int argc, const char *argv[])//接收聊天
{
// 判断是否管道存在
int ret = access("talk2", F_OK);
if(ret == -1){
printf("管道talk2不存在,创建管道\n");
// 创建管道
ret = mkfifo("talk2", 0666);
if(ret == -1){
perror("mkfifo");
return -1;
}
}
// 以只读方式打开要连接的管道
int fd_in = open("talk1", O_RDONLY);
if(fd_in == -1){
perror("open");
return -1;
}
// 以只写方式打开自己创建的管道
int fd_out = open("talk2", O_WRONLY);
if(fd_out == -1){
perror("open");
return -1;
}
while(1){
//收到消息
char buf_talk1_out[1024] = {0};
read(fd_in, buf_talk1_out, sizeof(buf_talk1_out));
printf("收到消息:%s\n", buf_talk1_out);
//发送消息
memset(buf_talk1_out,0,sizeof(buf_talk1_out));
printf("发送消息:");
scanf("%s", buf_talk1_out);
write(fd_out, buf_talk1_out, strlen(buf_talk1_out));
}
return 0;
}