实验一 进程控制
简单了解:
信号量:https://blog.csdn.net/dlutbrucezhang/article/details/8821690
管道机制:https://blog.csdn.net/rengui1228/article/details/72977797
1、编写程序,演示多进程并发执行和进程软中断、管道通信
#include<signal.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#define N 30
#define MAX 100
int count=0;
int pid1,pid2;
int fd[2];
void func1(int sig1)
{
kill(pid1,SIGUSR1);
kill(pid2,SIGUSR1);
}
void func2(int sig2)
{
close(fd[0]);
close(fd[1]);
if(pid1==0){
printf("child1 end!\n");
exit(0);
}
if(pid2==0){
printf("child2 end!\n");
exit(0);
}
}
int pid1_read_pipe(int fd)
{
char buf[N];
int n=0;
while(1){
n=read(fd,buf,sizeof(buf));
buf[n]='\0';
printf("Read %d bytes : %s.\n",n,buf);
sleep(1);
}
exit(0);
}
int pid2_write_pipe(int fd)
{
char buf[MAX];
while(1){
sprintf(buf,"I send you %d times",count);
printf("write: %s.\n",buf);
count++;
buf[strlen(buf)] = '\0';
write(fd,buf,strlen(buf));
sleep(1);
}
exit(0);
}
int main()
{
if(pipe(fd) < 0){
perror("Fail to pipe");
exit(EXIT_FAILURE);
}
signal(SIGINT,func1);
if((pid1=fork())<0){
perror("Fail to fork");
exit(EXIT_FAILURE);
}else if(pid1==0){
signal(SIGINT,SIG_IGN);
signal(SIGUSR1,func2);
close(fd[1]);
pid1_read_pipe(fd[0]);
}
else{
if((pid2=fork())<0){
perror("Fail to fork");
exit(EXIT_FAILURE);
}
else if(pid2==0){
signal(SIGINT,SIG_IGN);
signal(SIGUSR1,func2);
close(fd[0]);
pid2_write_pipe(fd[1]);
}
}
waitpid(pid1,NULL,0);
printf("Child Process 1 is killed by parent!\n");
waitpid(pid2,NULL,0);
printf("Child Process 2 is killed by parent!\n");
return 0;
}
2、父进程使用系统调用pipe( )建立一个管道,然后使用系统调用fork()创建两个子进程,子进程1和子进程2;
子进程1每隔1秒通过管道向子进程2发送数据:
I send you x times. (x初值为1,每次发送后做加一操作)
子进程2从管道读出信息,并显示在屏幕上。
3、父进程用系统调用signal()捕捉来自键盘的中断信号(即按Ctrl+C键);当捕捉到中断信号后,父进程用系统调用Kill()向两个子进程发出信号,子进程捕捉到信号后分别输出下列信息后终止:
Child Process l is Killed by Parent!
Child Process 2 is Killed by Parent!
4、父进程等待两个子进程终止后,释放管道并输出如下的信息后终止
Parent Process is Killed!
5、运行结果
附:
父进程写,两个子进程读,代码如下:
#include<signal.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#define N 30
#define MAX 100
int pid1,pid2;
int fd[2];
void func1(int sig1)
{
kill(pid1,SIGUSR1);
kill(pid2,SIGUSR1);
}
void func2(int sig2)
{
close(fd[0]);
close(fd[1]);
if(pid1==0){
printf("child1 end!\n");
exit(0);
}
if(pid2==0){
printf("child2 end!\n");
exit(0);
}
}
int pid1_read_pipe(int fd)
{
char buf[N];
int n=0;
while(1){
n=read(fd,buf,sizeof(buf));
buf[n]='\0';
printf("pid1 Read %d bytes : %s.\n",n,buf);
sleep(1);
}
exit(0);
}
int pid2_read_pipe(int fd)
{
char buf[N];
int n=0;
while(1){
n=read(fd,buf,sizeof(buf));
buf[n]='\0';
printf("pid2 Read %d bytes : %s.\n",n,buf);
sleep(1);
}
exit(0);
}
int write_pipe(int fd)
{
char buf[MAX];
int count=0;
for (int i=0; i < 10; i++){
sprintf(buf,"I send you %d times",count);
printf("write: %s.\n",buf);
count++;
buf[strlen(buf)] = '\0';
write(fd,buf,strlen(buf));
sleep(1);
}
return 0;
}
int main()
{
if(pipe(fd) < 0){
perror("Fail to pipe");
exit(EXIT_FAILURE);
}
signal(SIGINT,func1);
if((pid1=fork())<0){
perror("Fail to fork");
exit(EXIT_FAILURE);
}else if(pid1==0){
signal(SIGINT,SIG_IGN);
signal(SIGUSR1,func2);
close(fd[1]);
pid1_read_pipe(fd[0]);
}
else{
if((pid2=fork())<0){
perror("Fail to fork");
exit(EXIT_FAILURE);
}
else if(pid2 == 0){
signal(SIGINT,SIG_IGN);
signal(SIGUSR1,func2);
close(fd[0]);
pid2_read_pipe(fd[0]);
}
}
write_pipe(fd[1]);
waitpid(pid1,NULL,0);
printf("Child Process 1 is killed by parent!\n");
waitpid(pid2,NULL,0);
printf("Child Process 2 is killed by parent!\n");
return 0;
}
运行结果: