• Linux操作系统编程 实验二 进程管理


    实验目的

    按要求编写2个C语言程序。

    程序1

    编制实现软中断通信的程序。父进程fork两个子进程。等待一段时间后,kill子进程形成软中断,并wait函数等待子进程退出信号。
    程序1流程图

    运行程序soft_ipc,直到输入ctrl+,产生如下的输出,其中进程的PID和顺序可以不同,但其他输出内容必须一致。

    程序2

    编制实现进程的管道通信的程序。父进程开通pipe,并fork两个子进程。接收来自于子进程的信息并输出在Shell里。
    程序2流程图

    运行pipe_ipc,输出如下所示内容。(注:子进程1和2的输出顺序可以不同。)

    实验过程

    exp02_1.c

    点击查看详细内容
    #include <stdio.h>
    #include <signal.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    
    int wait_flag;
    void stop();
    int main(void){
            int pid1, pid2, pid3;
            signal(3, stop);
            printf("Register a signal handler for signal 3.
    ");
            while((pid1 = fork()) == -1);   //创建子进程1
            if(pid1 > 0)    //是父进程
            {
                    printf("Process %d got a signal.
    ", pid1);
                    while((pid2 = fork()) == -1);   //创建子进程2
                    if(pid2 > 0)    //是父进程
                    {
                            printf("Process %d got a signal.
    ", pid2);
                            wait_flag = 1;
                            sleep(5);
                            kill(pid1, 16);
                            kill(pid2, 17);
                            wait(0);
                            printf("Parent processs exit normally!
    ");
                    }
                    else                    //是子进程2
                    {
                            wait_flag = 1;
                            signal(17, stop);
                            printf("Child process 2 is killed by parent !!
    ");     //打印信息
                            while((pid3 = fork()) == -1);
                            if(pid3 > 0){
                                    printf("Precess %d got a signal.
    ", pid3);
                            }
                    }
            }
            else                    //是子进程1
            {
                    wait_flag = 1;
                    signal(16, stop);
                    printf("Child process 1 is killed by parent !!
    ");     //打印信息
            }
            return 0;
    }
    void stop()
    {
            wait_flag = 0;
    }
    

    exp02_2.c

    点击查看详细内容
    #include <unistd.h>
    #include <signal.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/wait.h>
    
    int pid1, pid2;
    int main(void) {
            int fd[2];
            char OutPipe[100], InPipe[100];
            pipe(fd);
            while ((pid1 = fork()) == -1);
            if (pid1 == 0) {
                    lockf(fd[1], 1, 0);
                    sprintf(OutPipe, "Child process 1 is sending message!
    ");
                    write(fd[1], OutPipe, 50);
                    sleep(5);
                    lockf(fd[1], 0, 0);
                    exit(0);
            }
            else {
                    while ((pid2 = fork()) == -1);
                    if (pid2 == 0) {
                            lockf(fd[1], 1, 0);
                            sprintf(OutPipe, "Child process 2 is sending message!
    ");
                            write(fd[1], OutPipe, 50);
                            sleep(5);
                            lockf(fd[1], 0, 0);
                            exit(0);
                    }
                    else {
                            wait(0);
                            read(fd[0], InPipe, 50);
                            printf("%s
    ", InPipe);
                            wait(0);
                            read(fd[0], InPipe, 50);
                            printf("%s
    ", InPipe);
                            exit(0);
                    }
            }
    }
    

    实验结果

    程序1运行结果

    程序2运行结果

  • 相关阅读:
    自动发送邮件功能
    工作中常用的Android系统ADB命令收集
    商城系统必须知道的【订单、优惠金额、退货、实际营收】解释
    商城系统项目必须知道的专业数据指标
    接口加密思路
    Selenium使用Chrome模拟手机浏览器方法解析
    PHP上传图片基本代码示例
    iframe子页面获取父页面的点击事件
    javascript实现网页倒计时效果
    Crontab常用命令总结
  • 原文地址:https://www.cnblogs.com/ast935478677/p/14125442.html
Copyright © 2020-2023  润新知