当前位置:首页 > 操作系统 > Linux

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

这个系列的博客贴的都是我大二的时候学习Linux系统高级编程时的一些实验程序,都挺简单的。

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

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

实验内容:

一、信号

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

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

             1 #include<stdio.h>
 2 #include<unistd.h>
 3 #include<signal.h>
 4 #include<stdlib.h>
 5int main()
 6{
 7     alarm(5);
 8int i;
 9for(i=0;i<=7;i++)
10    {
11     printf("running…n");
12     sleep(1);
13    }
14return0;
15 }
             1 #include<stdio.h>
 2 #include<unistd.h>
 3 #include<signal.h>
 4 #include<stdlib.h>
 5void fun()
 6{
 7int i=0;
 8for(i=0;i<=2;i++)
 9    {
10         printf("handling SIGALRM n");
11    }
12}
13int main()
14{
15     (void)signal(SIGALRM,fun);
16     alarm(5);
17int i;
18for(i=0;i<=7;i++)
19    {
20     printf("running…n");
21     sleep(1);
22    }
23return0;
24 }

 

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

             1 #include<stdio.h>
 2 #include<unistd.h>
 3 #include<stdlib.h>
 4 #include<signal.h>
 5int main()
 6{
 7    pid_t pid;
 8     pid=fork();
 9int ret;
10if(pid <0)
11    {
12         printf("Error Exit!n");
13         exit(1);
14    }
15elseif(pid==0)
16    {
17        raise(SIGSTOP);
18         exit(0);
19    }
20else21    {
22         printf("子进程的进程号是:%dn",pid);
23if(waitpid(pid,NULL,WNOHANG)==0)
24        {
25if(ret=kill(pid,SIGKILL)==0)
26            {
27                 ptintf("fun kill‘s return is %d,pid is%dn",ret,pid);
28            }
29        }
30    }
31return0;
32 }

 

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

             1 #include<stdio.h>
 2 #include<unistd.h>
 3 #include<signal.h>
 4 #include<sys/types.h>
 5 #include<stdlib.h>
 6void fun_z()
 7{
 8     printf("you press Ctrl+zn");
 9     printf("Ctrl + z is useable now!n");
10    signal(SIGTSTP,SIG_DFL);
1112}
13void fun_d()
14{
15     printf("you press ‘Ctrl+‘ n");
16     printf("Ctrl + d is useable now!n");
17    signal(SIGQUIT,SIG_DFL);
1819}
20int main()
21{
22int i;
23     sigset_t set,pendset;
24struct sigaction action;
25    signal(SIGTSTP,fun_z);
26    signal(SIGQUIT,fun_d);
27if(sigemptyset(&set)<0)
28         perror("init sign error!");
29if(sigaddset(&set,SIGTSTP)<0)
30         perror("add ctrl+z error!n");
31if(sigaddset(&set,SIGQUIT)<0)
32             perror("ass ‘ctrl+‘ error!n");
33while(1)
34    {
35         printf("Ctrl +z and ‘Ctrl +‘ is zuse!n");
36         sleep(2);
37    }
3839return0;
40 }

 

二、管道

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

             1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<sys/types.h>
 4 #include<sys/wait.h>
 5 #include<unistd.h>
 6 #include<string.h>
 7int  main()
 8{
 9    pid_t result;
10int n;
11int pipe_fd[2];
12char buf1[100],buf2[100];
13     memset(buf1,0,sizeof(buf1));
14if(pipe(pipe_fd)<0)
15        {
16             printf("error!n");
17return -1;
18        }
19     result=fork();
20if(result<0)
21    {
22         printf("error!n");
23         exit(0);
24    }
25elseif(result==0)
26    {
27         close(pipe_fd[1]);
28if((n =read(pipe_fd[0],buf1,100))>0)
29        {
30             printf("child read %d char,char is %sn",n,buf1);
31             close(pipe_fd[0]);
32             exit(0);
33        }
34    }
35else36    {
37         close(pipe_fd[0]);
38         printf("please input pipe word n");
39         fgets(buf2,sizeof(buf2),stdin);
40if(write(pipe_fd[1],buf2,strlen(buf2))!=-1)
41             printf("parent write to child is: %sn",buf2);
42         close(pipe_fd[1]);
43         waitpid(result,NULL,0);
44         exit(0);
45    }
4647return0;
48 }

 

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

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

三、共享内存

1、设计一个程序,要求创建进程,父子进程通过匿名映射实现共享内存。

 

原文:http://www.cnblogs.com/msxh/p/5040446.html


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!