题目

编写一个Linux C程序,在主进程中创建一个子进程,子进程中死循环输出“Hello CSU”字符串,主进程休眠10s后,向子进程发送信号结束子进程,随后主进程退出。(用信号实现进程间的通信,kill函数)

代码

#include<stdio.h>
#include<signal.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
void main(){
    pid_t pid;
    printf("program is starting\n");
    pid=fork();

    switch(pid){
        case -1:
            exit(0);
            break;
        case 0:
            while(1){
                sleep(1);
                printf("hello world\n");
            }
            break;
        default:
            sleep(10);
            //发出终止信号
printf("结束子进程\n"); kill(pid,SIGTERM); break; } }

运行结果如图

linux下c语言实现双进程运行

 

相关文章:

  • 2022-12-23
  • 2021-12-09
  • 2021-10-11
  • 2022-01-04
  • 2021-06-07
  • 2021-11-27
  • 2021-05-31
  • 2021-05-24
猜你喜欢
  • 2021-08-12
  • 2021-12-28
  • 2021-12-19
  • 2021-07-18
相关资源
相似解决方案