为了账号安全,请及时绑定邮箱和手机立即绑定

操作系统练习——进程管理

标签:
操作系统

首先祝各位小伙伴们开工大吉,慕课网越来越好。今天笔者将为大家带来几道简单的操作系统练习题目,通过这四道题目你可以学习到:
1.深对进程概念的理解,明确进程和程序的区别;
2.进一步认识并发执行的实质;
3.分析进程争用资源的现象,学习解决进程互斥的方法;
4.了解Linux系统中进程通信的基本原理。
笔者在后边附上了自己写的代码,欢迎大家讨论交流。
一、进程的创建
编写一段程序,使用系统调用fork()创建两个子进程。当此程序运行时,在系统中有一个父进程和两个子进程活动。让每一个进程在屏幕上显示一个字符:父进程显示字符”a”;子进程分别显示字符”b”和字符”c”。试观察记录屏幕上的显示结果,并分析原因。
二、进程的控制
修改已编写的程序,将每个进程输出一个字符改为每个进程输出一句话,再观察程序执行时屏幕上出现的现象,并分析原因。如果在程序中使用系统调用lockf()来给每一个进程加锁,可以实现进程之间的互斥,观察并分析出现的现象。
三、进程的软中断通信
要求:使用系统调用fork()创建两个子进程,再用系统调用signal()让父进程捕捉键盘上来的中断信号(即按DEL键);当捕捉到中断信号后,父进程用系统调用Kill()向两个子进程发出信号,子进程捕捉到信号后分别输出下列信息后终止
Child Processl2 is killed by parent!
Child Processl1 is killed by parent!
父进程等待两个子进程终止后,输出如下的信息后终止:
Parent Process is killed!
②在上面的程序中增加语句signal(SIGINT,SIG-IGN) 和signal(SIGQUIT,SIG-IGN), 观察执行结果,并分析原因。
四、程的管道通信
编制一段程序,实现进程的管道通信。使用系统调用pipe()建立一条管道线;两个子进程P1和P2分别向管道各写一句话:
Child 1 is sending a message!
Child 2 is sending a message!
而父进程则从管道中读出来自与两个子进程的信息,显示在屏幕上。要求父进程先接收子进程P1发来的消息,然后再接收子进程P2发来的消息。
(1)

#include<stdio.h>
int main(){
    int pid1,pid2;
    while((pid1=fork())==-1);
    if(pid1>0)printf("a\n");
    else{
        if(pid1==0)printf("b\n");
        while((pid2=fork())==-1);
        if(pid2==0)printf("c\n");
}
return 0;
}

(2)

#include<stdio.h>
#include<unistd.h>
int main()
{
	pid_t p1,p2;
	p1=fork();
        int i;
	if(p1<0)
	{
		printf("failed\n");
	}
	else if(p1==0)//jincheng1
	{
                lockf(1,1,0);
                for(i=0;i<5;i++){
		   printf("子1\n");
                lockf(1,0,0);}
	}
	else
	{
		p2=fork();
		if(p2<0)
		{
			printf("failed\n");
		}
		else if(p2==0)//jincheng2 
		{ 
                        lockf(1,1,0);
                       for(i=0;i<5;i++){
		           printf("子2\n");
                        lockf(1,0,0);}
		}
		else//fujincheng
		{ 
                        lockf(1,1,0);
                       for(i=0;i<5;i++){
		           printf("父\n");
                        lockf(1,0,0);}
		}
	}
}

(3)

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<signal.h>
int wait_mark;
           void waiting()
           {
                 while(wait_mark==1);
            }
          void  stop()
          {
           wait_mark=0;
           }
       int main()
          {
               int p1,p2;
               while((p1=fork())==-1);
               if(p1==0)
               {
                   wait_mark=1;
                   signal(SIGINT,SIG_IGN);
                   signal(16,stop);
                   waiting();
                   printf("Child Process1 is Killed by Parent!\n");
                   exit(0);
                }
               else
               {
                   while((p2=fork())==-1);
                   if(p2==0)
                   {
                      wait_mark=1;
                      signal(SIGINT,SIG_IGN);
                      signal(17,stop);
                      waiting();
                      printf("Child Process2 is Killed by Parent!\n");
                      exit(0);
                    }
                   else
                   {
                      wait_mark=1;
                      signal(SIGINT,stop);
                      waiting();
                      kill(p1,16);
                      kill(p2,17);
                      wait(0);
                      wait(0);
                      printf("Parent Process is Killed!\n");
                     exit(0);
                   }
               }
         }

(4)

#include<stdio.h>
#include<signal.h>
#include<unistd.h>
#include<stdlib.h>

void waiting(),stop(),alarming();
int wait_mark;
main()
{
	int p1,p2;
	if(p1=fork())
	{
		if(p2=fork())
		{
			wait_mark=1;
			signal(SIGINT,stop);
			signal(SIGALRM,alarming);
			waiting();
			kill(p1,16);
			kill(p2,17);
			wait(0);
			wait(0);
			printf("parent process is killed!\n");
			exit(0);
		}
		else 
		{
			wait_mark=1;
			signal(17,stop);
			signal(SIGQUIT,SIG_IGN);
			while(wait_mark!=0);
			lockf(1,1,0);
			printf("child process2 is killed by parent!\n");
			lockf(1,0,0);
			exit(0);
		}
	}
	else
	{
		wait_mark=1;
		signal(16,stop);
		signal(SIGQUIT,SIG_IGN);
		while(wait_mark!=0);
		lockf(1,1,0);
		printf("child process1 is killed by parent!\n");
		lockf(1,0,0);
		exit(0);
	}
}
void waiting()
{
	sleep(5);
	if(wait_mark!=0);
	kill(getpid(),SIGALRM);
}
void alarming()
{
	wait_mark=0;
}
void stop()
{
	wait_mark=0;
}

(5)

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<signal.h>
int pid1,pid2;
int main(){
	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 1 is sending a message!");
                write(fd[1],OutPipe,50);
                sleep(1);
                lockf(fd[1],0,0);
                exit(0);
	} else {
		while((pid2=fork())==-1);
		if(pid2==0) {
                        lockf(fd[1],1,0);
                        sprintf(OutPipe,"Child 2 is sending a message!");
                        write(fd[1],OutPipe,50);
                        sleep(1);
                        lockf(fd[1],0,0);
                        exit(0);

		} else {
		        wait(0);
		        read(fd[0],InPipe,50);
		        printf("%s\n",InPipe);
		        wait(0);
		        read(fd[0],InPipe,50);
		        printf("%s\n",InPipe);
		        exit(0);
		}
	}
}

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
Linux系统工程师
手记
粉丝
7202
获赞与收藏
414

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消