在c++代码中关闭和启动另外一个pid进程号,共享内存数据使用

char * of_program_name= "./AB";

1、 pid = getPidFromName(of_program_name);

if (pid > 0)
            {
                ERROR("AB process still exispid = %d\n", pid);

                if (kill(pid, SIGKILL))  //杀死进程

            {
                    fprintf(stderr, "kill(SIGKILL)error\n");

                    continue;
                }

wait(NULL);
            }
 

2、重启另外一个进程
            if (0 == fork())
            {
                ERROR("start ABagain\n");

                if (execl(of_program_name, of_program_name, NULL))
                {
                    fprintf(stderr, "exec %s error\n", of_program_name);
                }
            }

             usleep(100000);
        }

                

3、//关闭共享内存
    if (shmctl(shmid, IPC_RMID, 0) == -1)
    {
       fprintf(stderr, "shmctl(IPC_RMID) failed\n");
        exit(EXIT_FAILURE);

}

 

4、  //share memory initialzie
    void* shm = NULL;
    struct shared_use_st* shared = NULL;
    int shmid;

    //get share memory
    shmid = shmget((key_t)6666, sizeof(struct shared_use_st), 0666 | IPC_CREAT);

if (shmid == -1)
    {
        fprintf(stderr, "shmget failed\n");
        exit(EXIT_FAILURE);
    }

// map the share memory to local address
    shm = shmat(shmid, (void*)0, 0);

 if (shm == (void*) -1)
    {
        fprintf(stderr, "shmat failed\n");
    

    exit(EXIT_FAILURE);
    }

    shared = (struct shared_use_st*)shm;

 

 

 

 


    

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值