您的位置:首页 > 运维架构 > Linux

【Linux程序设计】之进程间的通信

2015-12-11 22:36 801 查看
这个系列的博客贴的都是我大二的时候学习Linux系统高级编程时的一些实验程序,都挺简单的。

实验题目:Linux环境下的进程间通信

实验目的:熟悉进程通信中信号概念及信号处理;掌握进程间的管道通信编程;了解进程间的内存共享编程。

实验内容:

一、信号

设计程序,满足如下要求:

1、编程程序:每隔1秒显示“running….”一次,显示8次后,程序结束。应用函数alarm,在程序开始运行5秒后发送信号SIGALRM,并实现:1)程序接收到SIGALRM信号就被终止;2)自定义信号处理函数,在程序接收到SIGALRM信号后,循环显示三次“handling SIGALRM”。

#include<stdio.h>
#include<unistd.h>
#include<signal.h>
#include<stdlib.h>
int main()
{
alarm(5);
int i;
for(i=0;i<=7;i++)
{
printf("running…\n");
sleep(1);
}
return 0;
}


#include<stdio.h>
#include<unistd.h>
#include<signal.h>
#include<stdlib.h>
void fun()
{
int i=0;
for(i=0;i<=2;i++)
{
printf("handling SIGALRM \n");
}
}
int main()
{
(void)signal(SIGALRM,fun);
alarm(5);
int i;
for(i=0;i<=7;i++)
{
printf("running…\n");
sleep(1);
}
return 0;
}


2、设计一个程序,要求用户进程创建一个子进程,子进程发送SIGSTOP将自身挂起,父进程向子进程发出SIGKILL信号,子进程收到此信号,结束子进程的运行。

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<signal.h>
int main()
{
pid_t pid;
pid=fork();
int ret;
if(pid <0)
{
printf("Error Exit!\n");
exit(1);
}
else if(pid==0)
{
raise(SIGSTOP);
exit(0);
}
else
{
printf("子进程的进程号是:%d\n",pid);
if(waitpid(pid,NULL,WNOHANG)==0)
{
if(ret=kill(pid,SIGKILL)==0)
{
ptintf("fun kill's return is %d,pid is%d\n",ret,pid);
}
}
}
return 0;
}


3、设计一个程序,要求程序运行后进入无限循环,要求主程序运行时,即使用户按下中断键(Ctrl+Z和Ctrl+\),也不能影响正在运行的程序,即让信号处于阻塞状态,当主体程序运行完毕后才进入自定义信号处理函数,当用户再次按下中断键(Ctrl+Z和Ctrl+\)后,结束程序运行。

#include<stdio.h>
#include<unistd.h>
#include<signal.h>
#include<sys/types.h>
#include<stdlib.h>
void fun_z()
{
printf("you press Ctrl+z\n");
printf("Ctrl + z is useable now!\n");
signal(SIGTSTP,SIG_DFL);

}
void fun_d()
{
printf("you press 'Ctrl+\' \n");
printf("Ctrl + d is useable now!\n");
signal(SIGQUIT,SIG_DFL);

}
int main()
{
int i;
sigset_t set,pendset;
struct sigaction action;
signal(SIGTSTP,fun_z);
signal(SIGQUIT,fun_d);
if(sigemptyset(&set)<0)
perror("init sign error!");
if(sigaddset(&set,SIGTSTP)<0)
perror("add ctrl+z error!\n");
if(sigaddset(&set,SIGQUIT)<0)
perror("ass 'ctrl+\' error!\n");
while(1)
{
printf("Ctrl +z and 'Ctrl +\' is zuse!\n");
sleep(2);
}

return 0;
}


二、管道

1、设计一个程序,要求创建一个管道,复制进程,父进程往管道中写入字符串“how are you!”,子进程从管道中读取并输入字符串“how are you!”。

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
#include<string.h>
int  main()
{
pid_t result;
int n;
int pipe_fd[2];
char buf1[100],buf2[100];
memset(buf1,0,sizeof(buf1));
if(pipe(pipe_fd)<0)
{
printf("error!\n");
return -1;
}
result=fork();
if(result<0)
{
printf("error!\n");
exit(0);
}
else if(result==0)
{
close(pipe_fd[1]);
if((n =read(pipe_fd[0],buf1,100))>0)
{
printf("child read %d char,char is %s\n",n,buf1);
close(pipe_fd[0]);
exit(0);
}
}
else
{
close(pipe_fd[0]);
printf("please input pipe word \n");
fgets(buf2,sizeof(buf2),stdin);
if(write(pipe_fd[1],buf2,strlen(buf2))!=-1)
printf("parent write to child is: %s\n",buf2);
close(pipe_fd[1]);
waitpid(result,NULL,0);
exit(0);
}

return 0;
}


2、设计一个程序,要求用popen创建管道,实现“rpm -qa | grep nfs”的功能。

3、设计一个程序,要求创建一个管道PIPE,复制进程,父进程运行命令“ls –l”,把运行结果写入管道,子进程从管道中读取“ls -l”的结果,把读出的作为输入接着运行“grep .c”。

三、共享内存

1、设计一个程序,要求创建进程,父子进程通过匿名映射实现共享内存
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: