BBBeaute 2016-12-25 05:27 采纳率: 0%
浏览 1053
已结题

linux课程设计,求大神帮忙写下备注。在网上下载的代码,但是自己没办法运行。

软中断通信
编写一段程序,使其实现进程的软中断通信。使用系统调用fork()创建两个子进程,再用系统调用signal()让父进程捕捉键盘上来的中断信号(即按Del键),当捕捉到中断信号后,父进程用系统调用Kill()向两个子进程发出信号,子进程捕捉到信号后分别输出下列信息后终止:
Child Processll is Killed by Parent!
Child Processl2 is Killed by Parent!
** 父进程等待两个子进程终止后,输出如下的信息后终止:**
Parent Process is Killed!
进程的管道通信
编制一段程序,实现进程的管理通信。
使用系统调用pipe()建立一条管道线。两个子进程P1和P2分别向管道中写一句话:
Child 1 is sending a message!
Child 2 is sending a message!
而父进程则从管道中读出来自于两个子进程的信息,显示在屏幕上。
要求父进程先接收子进程P1发来的消息,然后再接收子进程P2发来的消息。

程序1:

 #include <stdio.h>
 #include <signal.h>
 #include<unistd.h>
 void waiting(),stop();   //引用函数声明
int wait_mark;
main()
{ int p1,p2;
while((p1=fork())==-1);  //创建进程p1
if(p1>0)
{while((p2=fork())==-1);
if(p2>0)
{ wait_mark=1;  //以下为主进程
signal(SIGINT,stop);    //接受‘del’信号,并转stop
waiting(0);
kill(p1,17);     //向p1发中断信号17
kill(p2,17);     //向p2发中断信号17
wait(0);         //将当前进程挂起,直至子进程发出信号
wait(0);
 printf("parent process is killed!\n");
exit(0);       //主进程终止自己
}
else
{ wait_mark=1;    //以下为子进程p2
signal(17,stop);
waiting();
lockf(1,1,0);    //加锁
printf("child process 2 is killed by parent!\n");
lockf(1,0,0);   //解锁
exit(0);
}
}
else
{ wait_mark=1;   //进程p1
signal(17,stop);
waiting();
lockf(1,1,0);
printf("child process 1 is killed by parent!\n");
lockf(1,0,0);
exit(0);      //返回主进程,本进程自我终止
}
}
void waiting()
{ while(wait_mark=0);
}
void stop()
{ wait_mark=0;
}

运行时,从键盘输入中断信号‘del’键。
程序2:

#include <unistd.h>
#include <signal.h>
#include <stdio.h>
int pid1,pid2;
main()
{ int fd[2]; /fd[2]用于存放管道文件的描述符/
char Outpipe[100],Inpipe[100];
pipe(fd); /创建管道,fd[0]、fd[1]分别为读、写描述符/
while((pid1=fork())==-1);
if(pid1==0)
{ lockf(fd[1],1,0); /锁定写/
sprintf(Outpipe,”child 1 process is sending message!”);
write(fd[1],Outpipe,50); /将Outpipe中的数据写入管道/
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 2 process 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);  /将Iupipe中的数据读入管道/
printf(“%s\n”,Inpipe);
wait(0);
read(fd[0],Inpipe,50);


 printf(“%s\n”,Inpipe);
exit(0);
}
}
}

帮我测试一下,如果有错误帮我修改一下,最好能告诉我我需要什么才能把它运行出来

  • 写回答

1条回答 默认 最新

  • liupan6889 2016-12-27 13:18
    关注

    Gcc编译啊…………

    评论

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样