這個系列的博客貼的都是我大二的時候學習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、設計一個程序,要求創建進程,父子進程通過匿名映射實現共享內存。