Delete all SYSTEM V shared memory and semaphores on UNIX-like systems

前端 未结 11 2501
情话喂你
情话喂你 2020-12-23 14:01

How can I delete all not used semaphores and shared memory with a single command on a UNIX-like system, e.g., Ubuntu?

11条回答
  •  清歌不尽
    2020-12-23 14:42

    In addition to bvamos's answer, according to the documentation the use of sem is deprecated :

    NAME ipcrm - remove a message queue, semaphore set or shared memory id SYNOPSIS ipcrm [ -M key | -m id | -Q key | -q id | -S key | -s id ] ... deprecated usage

    ipcrm [ shm | msg | sem ] id ...

    remove shared memory

    us ipcrm -m to remove a shared memory segment by the id

    #!/bin/bash
    
    set IPCS_M = ipcs -m | egrep "0x[0-9a-f]+ [0-9]+" | grep $USERNAME | cut -f2 -d" "
    
    for id in $IPCS_M; do
      ipcrm -m $id;
    done
    

    or ipcrm -M to remove a shared memory segment by the key

    #!/bin/bash
    
    set IPCS_M = ipcs -m | egrep "0x[0-9a-f]+ [0-9]+" | grep $USERNAME | cut -f1 -d" "
    
    for id in $IPCS_M; do
      ipcrm -M $id;
    done
    

    remove message queues

    us ipcrm -q to remove a shared memory segment by the id

    #!/bin/bash
    
    set IPCS_Q = ipcs -q | egrep "0x[0-9a-f]+ [0-9]+" | grep $USERNAME | cut -f2 -d" "
    
    for id in $IPCS_Q; do
      ipcrm -q $id;
    done
    

    or ipcrm -Q to remove a shared memory segment by the key

    #!/bin/bash
    
    set IPCS_Q = ipcs -q | egrep "0x[0-9a-f]+ [0-9]+" | grep $USERNAME | cut -f1 -d" "
    
    for id in $IPCS_Q; do
      ipcrm -Q $id;
    done
    

    remove semaphores

    us ipcrm -s to remove a semaphore segment by the id

    #!/bin/bash
    
    set IPCS_S = ipcs -s | egrep "0x[0-9a-f]+ [0-9]+" | grep $USERNAME | cut -f2 -d" "
    
    for id in $IPCS_S; do
      ipcrm -s $id;
    done
    

    or ipcrm -S to remove a semaphore segment by the key

    #!/bin/bash
    
    set IPCS_S = ipcs -s | egrep "0x[0-9a-f]+ [0-9]+" | grep $USERNAME | cut -f1 -d" "
    
    for id in $IPCS_S; do
      ipcrm -S $id;
    done
    

提交回复
热议问题