Prasanth Madhavan
Prasanth Madhavan

Reputation: 13309

Cleaning semaphores in Mac OS X

I have a program that leaves some semaphores uncleaned and hence if i run it a couple of times, the program will seg fault. I used to use the following command in linux to clean them up.

ipcs -s | grep root |grep 666| cut -f2 -d' ' | xargs -I {} sudo ipcrm -s {}

but this doesnt work on mac. What command should i use to resolve this issue in mac osx?

Upvotes: 4

Views: 2699

Answers (3)

michael3.14
michael3.14

Reputation: 333

This worked for me on MacOS Ventura: I had to use an upper case -S for the key (third entry in the table)

ipcs -s | grep root | cut -f3 -d' ' | xargs -I {} sudo ipcrm -S {}

and the lower case -s with the id (second entry on the table)

ipcs -s | grep root | cut -f2 -d' ' | xargs -I {} sudo ipcrm -s {}

Replace root with another username or any other filtering.

Upvotes: 0

user3672051
user3672051

Reputation:

I was running into the same issue with some C code that I am working on so I wrote a simple C program to remove semaphores by name.

here is the code:

 #include <semaphore.h>
 #include <stdio.h>
 #include <errno.h>

 int main(int argc, char *argv[]) {
     for (int i = 1; i < argc; i++) {
         if (sem_unlink(argv[i]) != 0) {
             fprintf(stderr, "%s: ", argv[1]);
             perror("");
         }
     }
 }

once compiled you can call the program from a terminal with a list of semaphore names to remove like such:

$ ./semrm <name_1> <name_2> ... <name_n>

assuming the executable is named semrm

Upvotes: 1

Dominic Watkins
Dominic Watkins

Reputation: 11

Some systems appear to add additional spaces between fields in the output, so you may need to specify the 3rd field instead:

ipcs -s | grep root |grep 666| cut -f3 -d' ' | xargs -I {} sudo ipcrm -s {}

I would consider this only a short-term fix.

Upvotes: 1

Related Questions