实验 Linux进程通信

实验 Linux进程通信

实验内容
1、使用fork、kill和signal函数编写一个程序进行执行,要求:1)使用fork函数创建一个子进程,2)父进程一直忽略Ctrl+c信号,输出句子“Please wait 10 seconds to press Ctrl+c!”,3)子进程在休眠10秒后,向父进程发送SIGUSR1信号,然后结束,4)父进程捕获信号后将Ctrl+c 的动作恢复为默认,输出句子”You can press Ctrl+c now!“,按下 Ctrl+c 结束程序。
2、使用fork 函数创建一个子进程,使用pipe函数创建一个无命名管道,父进程提示“Parent:”,将输入内容通过管道传递给子进程,子进程输出“Child:<输入字符串>,are you sure? y/n”,输入y,输出 “Command completed” 后结束;输入n,输出“OK, anyway.”后结束。
3、使用mkfifo创建一个命名管道,编写两个程序,在第一个程序中输入的所有字符串,通过命名管道传递给第二个程序,第二个程序将字符串转换为大写形式后,将处理完的数据传递给第一个程序。
四、实验过程
1.#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
static int flag=1;
void sig_int(int sig)
{
flag=0;printf(“You can press Ctrl+c now!\n”);
signal(SIGINT, SIG_DFL);
}
int main()
{
pid_t pid;
pid=fork();
if(pid<0)
{perror(“fork fail”);exit(1);}
else if(pid0)
{
sleep(10);
kill(getppid(),SIGUSR1);
exit(0);
}
else
{
while(1)
{
if(flag
1)
{
flag=2;
printf(“Please wait 10 seconds to press Ctrl+c!\n”);
signal(SIGINT,SIG_IGN);
}
else if(flag==2)
{signal(SIGUSR1,sig_int);}
}
}
}

2.#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main()
{
int fd[2];
int ret = pipe(fd);
if (ret == -1)
{
perror(“pipe error\n”);
exit(1);
}
pid_t pid;
pid=fork();
if(pid<0)
{perror(“fork error\n”);exit(1);}
else if (pid > 0)
{
close(fd[0]);
char p[1024];
while(1)
{
printf(“Parent:”);
fgets(p,1024,stdin);p[strlen§-1]=’\0’;
write(fd[1], p, strlen§);sleep(1);
if((p[0]‘y’||p[0]‘n’)&&strlen§1)
{close(fd[1]);exit(0);}
memset(p,0,1024);
}
wait(NULL);
}
else if (pid == 0)
{
close(fd[1]);
char buf[1024] = { 0 };
while(1)
{
memset(buf,0,1024);
ret = read(fd[0], buf, sizeof(buf));
if(buf[0]
‘y’&&strlen(buf)1)
{printf(“Command completed.\n”);close(fd[0]);exit(0);}
else if(buf[0]
‘n’&&strlen(buf)==1)
{printf(“OK, anyway.\n”);close(fd[0]);exit(0);}
else {
printf(“Child:%s,are you sure? y/n\n”,buf);
}
}
}
return 0;
}

3.fifo-write.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
int ret,fd;
if (argc < 2)
{
printf("./w.out fifoname\n");
exit(1);
}
ret = access(argv[1], F_OK);
if (ret == -1)
{
int r = mkfifo(argv[1], 0664);
if (r == -1){
perror(“mkfifo”);
exit(1);
}
else{
printf(“fifo creat success!\n”);
}
}
fd = open(argv[1], O_RDWR);
if(fd==-1){perror(“open error”);exit(1);}
char p[1024]={0};
printf(“请输入小写字母组:”);
fgets(p,1024,stdin);
p[strlen§-1]=’\0’;
write(fd,p,strlen§);
sleep(3);
memset(p,0,1024);
read(fd,p,1024);
printf(“转换成大写字母组:%s\n”,p);
close(fd);
return 0;
}

fifo-read.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
int ret,fd;
if (argc < 2)
{
printf("./r fifoname\n");
exit(1);
}
ret = access(argv[1], F_OK);
if (ret == -1)
{
int r = mkfifo(argv[1], 0664);
if (r == -1){
perror(“mkfifo”);
exit(1);
}
else{
printf(“fifo creat success!\n”);
}
}
fd = open(argv[1], O_RDWR);
if (fd == -1){
perror(“open”);
exit(1);
}
char buf[1024];
read(fd,buf,1024);
int i;
for(i=0;i<strlen(buf);i++)
{
buf[i]-=32;
}
write(fd,buf,strlen(buf));
close(fd);
return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值