第一题:
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;
}
结果:
第三题:
代码:
Aa.c:
Bb.c:
结果:
第四题:
代码1:一发一收循环
聊天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;
}
代码2:任意一方可以多次输入
聊天1.c
聊天2.c
将主线程的"1to2"改成"2to1"
将子线程的"2to1"改成"1to2"