OS实验一 进程控制

实验一  进程控制

简单了解:

信号量:https://blog.youkuaiyun.com/dlutbrucezhang/article/details/8821690

管道机制:https://blog.youkuaiyun.com/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;
}

运行结果:

(1)进程的软中断通信 #include #include #include #include int wait_flag; void stop(); main( ) { int pid1, pid2; // 定义两个进程号变量 signal(2,stop); // 或者 signal (14,stop); while((pid1 = fork( )) == -1); // 若创建进程1不成功,则空循环 if(pid1 > 0) { // 进程创建成功,pid1为进程号 while((pid2 = fork( )) == -1); // 创建进程2 if(pid2 > 0) { wait_flag = 1; //sleep(1); // 父进程等待5秒 kill(pid1,SIGUSR1); // 杀死进程1 kill(pid2,SIGUSR2); // 杀死进程2 wait(0); wait(0); printf("\n Parent process is killed !!\n"); exit(0); // 父进程结束 } else { wait_flag = 1; signal(SIGUSR2,stop); // 等待进程2被杀死的中断号17 printf("\n Child process 2 is killed by parent !!\n"); exit(0); } } else { wait_flag = 1; signal(SIGUSR1,stop); // 等待进程1被杀死的中断号16 printf("\n Child process 1 is killed by parent !!\n"); exit(0); } } void stop() { wait_flag = 0; } (2)进程的管道通信 #include #include #include int pid1,pid2; // 定义两个进程变量 main( ) { int fd[2]; char OutPipe[100],InPipe[100]; // 定义两个字符数组 pipe(fd); // 创建管道 while((pid1 = fork( )) == -1); // 如果进程1创建不成功,则空循环 if(pid1 == 0) { lockf(fd[1],1,0); // 锁定管道 sprintf(OutPipe,"\n Child process 1 is sending message!\n"); write(fd[1],OutPipe,50); // 向管道写入数据 sleep(5); // 等待读进程读出数据 lockf(fd[1],0,0); // 解除管道的锁定 exit(0); // 结束进程1 } else { while((pid2 = fork()) == -1); // 若进程2创建不成功,则空循环 if(pid2 == 0) { lockf(fd[1],1,0); sprintf(OutPipe,"\n Child process 2 is sending message!\n"); write(fd[1],OutPipe,50); sleep(5); lockf(fd[1],0,0); exit(0); } else { wait(0); // 等待进程1 结束 read(fd[0],InPipe,50); // 从管道中读出数据 printf("%s\n",InPipe); // 显示读出的数据 wait(0); // 等待进程2 结束 read(fd[0],InPipe,50); printf("%s\n",InPipe); exit(0); // 父进程结束 } } }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值