01 noiembrie 2016

FDISK comenzi pt repartitionarea unui memory stick, in Linux

Afisare toate partitiile:
> lsblk
Sa spunem ca /dev/sdc reprezinta flash-ul.

Stergere totala:
> dd if=/dev/zero of=/dev/sdc bs=512 count=1

Refacere structura unui flash drive:
> fdisk /dev/sdc
  > m   (toate comenzile)
  > n    (partitie noua)
  > p    (primary partition)
  > 1    (index, adica sdc1)
  (o singura partitie care sa ocupe tot discul)
  > t     (change partition type)
  > L    (list all, perhaps choose option #4 FAT16 <32M)
  > p     (print current config)
  > w    (write)

Daca apare eroare dupa write:
> sudo partprobe /dev/sdc

30 septembrie 2016

Linux: mount USB

> sudo fdisk -l
> sudo mkdir /media/usb
> sudo mount /dev/sdb1 /media/usb

> sudo umount /media/usb

23 septembrie 2016

Remember GIT

git config --global user.name "name"
git config --global user.email "email@intel.com"
git config --global http.proxy proxyAddress  # or "" to override computer proxies

git init
git clone <repo> <directory> # it will stay fixed on this repo
git remote add origin <server> # to make another repo

git add *
git commit -m "message"
git push origin master

git pull # update local repo to the newest commit
git remote -v # all the repos

Also check this

21 septembrie 2016

Valgrind (notite)

tools available: memcheck (for memory leaks), cachegrind (detects cache misses), massif (heap and stack profiling), helgrind, callgrind, DRD
- cannot be attached to a already running process (Valgrind wants to have full control from the very start)
- results are given for the whole process but you can find the errors coming from a specific shared object
> valgrind --tool=memcheck --leak-check=yes --track-children=yes --log-file=log.txt java Example
Types of errors (counting the occurrences):
  • illegal read/write
    cat log.txt | grep "Invalid" | wc -l
  • use of uninitialised values
    cat log.txt | grep "uninitialised" | wc -l 
Results at the end of log file:
  • definitely lost = program is leaking memory
  • indirectly = leaking memory in a pointer-based structure
  • possibly = leaking memory unless pointers do something awesome
  • still rechable = it just didn't free some memory it could have freed
  • suppressed = to be ignored
> valgrind --tool=cachegrind --log-file=log.txt java -Djava.library.path=/home/gtache/workspace/MKL/lib Example
Results in the log file, miss rates for L1, L2, L3. 

valgrind --tool=massif --stacks=yes --time-unit=B --log-file=log.txt java --massif-out-file=massif.out.txt -Djava.library.path=/home/gtache/workspace/MKL/lib Example
Results in massif.out
> vim massif.out  or  ms_print massif.out # snapshots of total memory consumption for heap/stack at different moments of time
  • mem_heap_B = the number of useful heap bytes allocated at that point
  • mem_heap_extra_B = the number of bytes allocated in the excess of what the program asked for (might be associated with alignment)
  • mem_stacks_B = size of the stack
Sometimes it gives the detailed heap tree for a snapshot.
massif_visualizer massif.out # graphical output

Linux: send process to run in background

[ctr+z] to pause it
> bg # to send it in the background

16 martie 2016

Code jam, for women (solutiile mele, #1)

Problema 1) link

Cody are mai multe produse in magazin si vrea sa faca o reducere de 75%. Ea tipareste niste etichete atat pt preturile vechi cat si pt cele noi, dar comanda ii vine cu preturile amestecate, de la cel mai mic la cel mai mare, iar ea trebuie sa gaseasca perechile (pret nou, pret vechi).

2 pointeri p1 si p2, unul aflat la inceput (pe pretul minim, care mereu va fi cel la reducere), iar altul flotant, in cautarea originalului de la pretul redus. Pointerii se misca pana cand ajung la celalalt capat.

Rezolvare (Python)

f = open("A-large-practice.in", 'r')
T = eval(f.readline())

for i in range (T):
N = eval(f.readline())
v = f.readline().split(' ')
p1 = 0
p2 = 1
print("Case #", i+1, ": ", sep="", end="")
while (p1 < 2*N):
nr = 4/3 * eval(v[p1]) # the previous price value
while (eval(v[p2]) < nr):
p2 += 1 # put p2 there
print(v[p1], " ", sep="", end="")
v[p2] = "-1" # mark as visited by p2
# reposition p1
p1 += 1
while (p1 < 2*N and eval(v[p1]) == -1):
p1 += 1 # skip where p2 already was positioned