Question: I am unable to list the locks associated with a process and information about the locks i.e How many threads share this lock etc.

Kindly tell my how to extract this information from a process ID.

My case:

I have run `sysbench` command 

```
sysbench --test=threads --thread-locks=100 --max-time=20s run

```

I want to list locks that sysbench just created and information about the locks i.e which threads the sharing this lock etc?

Answer: 
**`Awk`** solution:

```
awk 'BEGIN{
         h = "1 2 3 4 5 6 7 8 11 12 20 21";
         len = split(h, head);
         print "\t\t" h
     }
     {
         printf "%s\t", $1;
         for (i = 1; i <= len; i++)
             printf "%s%d", (i == 1? "" : OFS), ($2 ~ "\\<" head[i] "\\>");
         print "" 
     }' file

```

* `h = "1 2 3 4 5 6 7 8 11 12 20 21"` - *header* line
* `len = split(h, head)` - split `h` line into array `head` where indices are ordered positions starting from `1` and values are crucial values obtained via splitting; `len` contains an array size
* `print "\t\t" h` - print the *header* line with leading tab characters
* `printf "%s\t", $1;` - print the 1st field `$1`
* `for (i = 1; i <= len; i++)` - iterate through `head` items
	+ `$2 ~ "\\<" head[i] "\\>"` - check if the 2nd field `$2` contains currently accessed item `head[i]`

---

The output:

```
        1 2 3 4 5 6 7 8 11 12 20 21
name1   1 1 0 1 0 0 0 1 0 0 0 0
name2   0 0 0 1 1 0 1 0 0 0 0 0
name3   0 0 0 0 0 0 0 1 0 0 0 0
name4   0 0 0 0 0 0 0 0 1 1 0 0
namex   0 0 0 0 0 0 0 0 0 0 1 1

```
Another `awk` approach which generate header first by itself and then fill the matrix with **1** for each key in array `head` exist in current input line otherwise with **0** if doesn't exist.

We used `grep` to fetch only `-o` numbers which matches the empty string at the left edge of a number `\b` (same as `\<` we used in `awk` for both left&right sides).

```
awk 'NR==FNR { !head[$1]++; next } 
    { printf $1; for (x in head) { printf (x?FS:"") ($0 ~ "\\<" x "\\>") }; print ""
}' <(grep -o '\b[0-9]\+' infile) infile

```

The output is:

```
name1 1 1 1 0 0 1 0 0 0 0
name2 0 0 1 1 1 0 0 0 0 0
name3 0 0 0 0 0 1 0 0 0 0
name4 0 0 0 0 0 0 1 1 0 0
namex 0 0 0 0 0 0 0 0 1 1

```

---

You could generate the header and insert in first line in input file, then the `awk` would be easy as:

```
awk 'NR==1{ split($0, head); next }
    { printf $1; for (x in head){ printf (x?FS:"") ($2 ~ "\\<" head[x] "\\>" ) }; 
print "" }' infile

```