Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

Linux Ask!

Oct 142010
 

What are the differences between Mutex and Semaphore?

Answer:

A very good example was given in the Wikipedia

Mutex:

Is a key to a toilet. One person can have the key - occupy the toilet - at the time. When finished, 
the person gives (frees) the key to the next person in the queue.

Semaphore:

Is the number of free identical toilet keys. Example, say we have four toilets with identical locks
and keys. The semaphore count - the count of keys - is set to 4 at beginning (all four toilets are free),
then the count value is decremented as people are coming in. If all toilets are full, ie. there are no 
free keys left, the semaphore count is 0. Now, when eq. one person leaves the toilet, semaphore is 
increased to 1 (one free key), and given to the next person in the queue.

So, A mutex is really a semaphore with value 1

Oct 132010
 

Is Perl pass by reference or pass by value?

Answer:

(Edit: Thanks Matt for the feedback, I have updated the answer.)

It depends on how your function is implemented

See the code below:


package Dog;

sub new {
	my ($class, $name) = @_;
	my $self = { "name" => $name };
	bless $self, $class;
	return $self;
}

// Pass by value
sub Foo {
	my $d = shift;
	$d = new Dog("Another");
}

// Pass by reference
sub Bar {
	$_[0] = new Dog("Another");
}

my $d = new Dog("Peter");

Foo($d); // Pass by value, $d not changed, so it print out "Peter"
print $d->{"name"};

Bar($d); // Pass by reference, $d is changed, so it print out "Another"
print $d->{"name"};
Oct 122010
 

Displays information about package in Ubuntu

Answer:

To display the package information about a package, in Ubuntu, type

# apt-cache show curl
Package: curl
Priority: optional
Section: web
Installed-Size: 308
Maintainer: Ubuntu Core Developers 
Original-Maintainer: Domenico Andreoli 
Architecture: amd64
Version: 7.19.5-1ubuntu2
Replaces: curl-ssl
Provides: curl-ssl
Depends: libc6 (>= 2.7), libcurl3 (>= 7.16.2-1), zlib1g (>= 1:1.1.4)
Filename: pool/main/c/curl/curl_7.19.5-1ubuntu2_amd64.deb
Size: 197480
MD5sum: 128a19e9ac7be831b7dd1b67b03dd495
SHA1: dcdec0bb1615465b93ee25368007e8613e3d9fe5
SHA256: 57dabdb5bde28c1c5e5ac96e56e95147f97817048c3be0f050f163af9c40473e
Description: Get a file from an HTTP, HTTPS or FTP server
 curl is a client to get files from servers using any of the supported
 protocols. The command is designed to work without user interaction
 or any kind of interactivity.
 .
 curl offers a busload of useful tricks like proxy support, user
 authentication, ftp upload, HTTP post, file transfer resume and more.
Homepage: http://curl.haxx.se
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Origin: Ubuntu
Task: uec
Oct 102010
 

How to clear cache from memory in Linux?

Answer:

From time to time, you might observed memory in Linux aren't free'ed after used, and they can be shown in the cached column of the free command.

# free -m
             total       used       free     shared    buffers     cached
Mem:          1033        468        564          0          0        343
-/+ buffers/cache:        124        908
Swap:          255          1        254

To manually free it, try

# sync; echo 3 > /proc/sys/vm/drop_caches

And try the free command again.

# free -m
             total       used       free     shared    buffers     cached
Mem:          1033        135        897          0          0         10
-/+ buffers/cache:        124        908
Swap:          255          1        254