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

前端 未结 11 2425
情话喂你
情话喂你 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:32

    This works on my Mac OS:

    for n in `ipcs -b -m | egrep ^m | awk '{ print $2; }'`; do ipcrm -m $n; done
    
    0 讨论(0)
  • 2020-12-23 14:38

    I don't know how to delete all at once, but you can use ipcs to list resources, and then use loop and delete with ipcrm. This should work, but it needs a little work. I remember that I made it work once in class.

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-23 14:43

    This is how I do it in FreeBSD:

    #!/usr/local/bin/bash
    for i in $(ipcs -a | grep "^s" | awk '{ print $2 }');
    do
            echo "ipcrm -s $i"
            ipcrm -s $i
    done
    
    0 讨论(0)
  • 2020-12-23 14:43

    to remove all shared memory segments on FreeBSD

    #!/bin/sh
    for i in $(ipcs -m | awk '{ print $2 }' | sed 1,2d);
    do
        echo "ipcrm -m $i"
        ipcrm -m $i
    done
    

    to remove all semaphores

    #!/bin/sh
    for i in $(ipcs -s | awk '{ print $2 }' | sed 1,2d);
    do
        echo "ipcrm -s $i"
        ipcrm -s $i
    done
    
    0 讨论(0)
  • 2020-12-23 14:49

    Since you mentioned that you're working on a NFS system, do you have access to those semaphores and shared memory? I think you misunderstood what they are, they are an API code that enables processes to communicate with each other, semaphores are a solution for preventing race conditions and for threads to communicate with each other, in simple answer, they do not leave any residue on any filesystem.

    Unless you are using an socket or a pipe? Do you have the necessary permissions to remove them, why are they on an NFS system?

    Hope this helps, Best regards, Tom.

    0 讨论(0)
提交回复
热议问题