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

 

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

 1 #include<stdio.h>
 2 #include<unistd.h>
 3 #include<stdlib.h>
 4 #include<signal.h>
 5 int main()
 6 {
 7     pid_t pid;
 8     pid=fork();
 9     int ret;
10     if(pid <0)
11     {
12         printf("Error Exit!\n");
13         exit(1);
14     }
15     else if(pid==0)
16     {
17         raise(SIGSTOP);
18         exit(0);
19     }
20     else
21     {
22         printf("子进程的进程号是:%d\n",pid);
23         if(waitpid(pid,NULL,WNOHANG)==0)
24         {
25             if(ret=kill(pid,SIGKILL)==0)
26             {
27                 ptintf("fun kill's return is %d,pid is%d\n",ret,pid);
28             }
29         }
30     }
31     return 0;
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>
 6 void fun_z()
 7 {
 8     printf("you press Ctrl+z\n");
 9     printf("Ctrl + z is useable now!\n");
10     signal(SIGTSTP,SIG_DFL);
11 
12 }
13 void fun_d()
14 {
15     printf("you press 'Ctrl+\' \n");
16     printf("Ctrl + d is useable now!\n");
17     signal(SIGQUIT,SIG_DFL);
18 
19 }
20 int main()
21 {
22     int i;
23     sigset_t set,pendset;
24     struct sigaction action;
25     signal(SIGTSTP,fun_z);
26     signal(SIGQUIT,fun_d);
27     if(sigemptyset(&set)<0)
28         perror("init sign error!");
29     if(sigaddset(&set,SIGTSTP)<0)
30         perror("add ctrl+z error!\n");
31     if(sigaddset(&set,SIGQUIT)<0)
32             perror("ass 'ctrl+\' error!\n");
33     while(1)
34     {
35         printf("Ctrl +z and 'Ctrl +\' is zuse!\n");
36         sleep(2);
37     }
38 
39     return 0;
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>
 7 int  main()
 8 {
 9     pid_t result;
10     int n;
11     int pipe_fd[2];
12     char buf1[100],buf2[100];
13     memset(buf1,0,sizeof(buf1));
14         if(pipe(pipe_fd)<0)
15         {
16             printf("error!\n");
17             return -1;
18         }
19     result=fork();
20     if(result<0)
21     {
22         printf("error!\n");
23         exit(0);
24     }
25     else if(result==0)
26     {
27         close(pipe_fd[1]);
28         if((n =read(pipe_fd[0],buf1,100))>0)
29         {
30             printf("child read %d char,char is %s\n",n,buf1);
31             close(pipe_fd[0]);
32             exit(0);
33         }
34     }
35     else
36     {
37         close(pipe_fd[0]);
38         printf("please input pipe word \n");
39         fgets(buf2,sizeof(buf2),stdin);
40         if(write(pipe_fd[1],buf2,strlen(buf2))!=-1)
41             printf("parent write to child is: %s\n",buf2);
42         close(pipe_fd[1]);
43         waitpid(result,NULL,0);
44         exit(0);
45     }
46 
47     return 0;
48 }

 

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

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

三、共享内存

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

 

posted @ 2015-12-11 22:36  马三小伙儿  阅读(2376)  评论(0编辑  收藏  举报