Linux - What and how to kill a zombie process

1. March 2013 10:12

 

What is a zombie?

 

A zombie process is simply a process that has exited but its parent has not yet read the processes exit code. Since the process has exited it does not have any open files or uses any memory in fact the only resources it is using is a entry in the task list. So in most Linux systems will mean that it is using around 4k-8k of memory.

 

Until the parent process reads the exit code the zombie will exist. Typically this points to a software bug in the parent process or an issue that caused the child (which is now the zombie) to exit unexpectedly. Which would also point to a bug or issue in the program. In either case the parent process should have actually dealt with the error an acted on it to read the exit status of the process which will clean up the zombie process and allow it to exit.

 

Why is there any issue?

 

Since the program isn't really using any resources it really doesn't cause any issues. However if you have a lot of zombie's being generated inside a system due to a bug in the software it will cause an issue because it is still running as a process and sooner or later you will hit the limit of the maximum number of processes permitted to run in the system. If this limit is reached you will not be able to start any more processes. This means you won't be able to run commands from the command line any more.

 

Why does kill now work on the zombie process?

 

Kill simply does not work on a zombie process since it is in fact already "dead"

 

How to kill it?

 

Since the issue really exists in the parent process the solution is to remove the parent process either by killing it or shutting it down. This will cause the child "zombie" processes to get a new parent process which will be the parent of the parent.

 

If you are working in a complex system you may need to kill several parent process in order to eliminate the zombies until the zombies get a parent process that will read the exit code from the processes which will cause the zombie process to be remove.

 

 

Hint: if you use the command "ps axfu" the "f" in the argument list will print the output as a tree which will make finding the parent somewhat simpler.

E-mail Kick it! DZone it! del.icio.us Permalink


C - Some Simple Examples of using strcat

16. February 2013 11:06

 

This post shows some simple example of using strcat to join two strings together in C

 

The msot common example you will find on the internet will work by just making an array that is "big enough" to store both the strings. However this can create problems when the strings you use are too long so it would need these conditions handled correctly in C. So it probably isn't the best example to use.

 

 

void basic(char *s1, char *s2)
{
	char str[200] = "";
	
	strcat(str, s1);
	strcat(str, s2);
	printf("basic: %s\n", str);
}

 

 

A better example of using strcat is to allocate an array dynamically. In this case on the stack. So this will work slightly better however it can cause a significant amount of stack space to be used and can also cause crashes if the strings are too long. However it will execute very quickly because dynamic allocation of memory on the stack will perform well.

 

 

void better1(char *s1, char *s2)
{
	char str[strlen(s1) + strlen(s2) * sizeof(*s1) + 1];
	str[0] = '\0';

	strcat(str, s1);
	strcat(str, s2);
	printf("basic: %s\n", str);
}

 

 

A much better example of this is to use malloc / free for the allocation of string. This will be slightly slower than the previoud example but will be able to handler much larger strings and much safer to use. It can of course still crash but only if the machine run's out of memory. How ever this error can be handled to prevent it crashing by changing the call to abort() to use proper error handling.

 

 

void better2(char *s1, char *s2)
{
	char *str = malloc(strlen(s1) + strlen(s2) + 1 * sizeof(*s1));
	if (str == NULL)
		abort();
	str[0] = '\0';
	
	strcat(str, s1);
	strcat(str, s2);
	printf("basic: %s\n", str);
		
	free(str);
}

 

Finally this is an example about joining multiple strings so we can just keep making our string longer and longer or join it from multiple different sources. Note that the following code really needs a check around the realloc function to see if it returns NULL. If it does then it should fail by returning the previous pointer. Or call abort because the system has run out of memory.

 

 

char *multiple(char *str, char *s2)
{
	int len;
	char *s;
	if (str != NULL)
		len = strlen(str);
	len += strlen(s2) + 1 * sizeof(*s2);
	s = realloc(str, len);
	strcat(s, s2);
	return s;
}

 

With the above code we can call it in the following way

 

 

char *tmp = multiple(NULL, "Hello ");
	tmp = multiple(tmp, " World");
	tmp = multiple(tmp, " We");
	tmp = multiple(tmp, " Can");
	tmp = multiple(tmp, " Join");
	tmp = multiple(tmp, " strings");
	printf("%s\n", tmp);
	free(tmp);

 

E-mail Kick it! DZone it! del.icio.us Permalink


Debian - Getting sshfs to work

14. February 2013 20:23

 

This is a short guide to get ssh to work on debian. So this should also work with some other linux varients like ubuntu. It can be a very useful tool to be able to access your home directory from a 2nd linux machine or from your raspberry pi. I have been using it so that you can run your normal powerful code editors which the raspberry pi does not have enough resources to run them fast enough.

 

 

To Install / Setup

 

sudo apt-get install sshfs 

sudo addgroup $USER fuse

 

Note: You don't need to add your self to the fuse ground. But it is required if you want to be able to mount / umount the filesystem without root permissions.

 

 

To Use

 

mkdir Raspberry

sshfs <username>@<ip address>:/home/<username>/Raspberry  -o uid=1001,gid=1002 Raspberry

 

 

Note: in the above you should change the uid and gid to your user id and group id. You can look thoose up in the /etc/passwd and /etc/group files on the machine that you are mounting the remote filesystem on.

 

 

To unmount

 

fusermount -u /home/james/Raspberry/

 

 

 

Another useful tip is that it will also work with ssh key authentication so that you do not even require a password or so that you can have it configured to automount on boot using the crontab.

E-mail Kick it! DZone it! del.icio.us Permalink


Nice Job AMD. Heat Sink Failure

8. February 2013 18:55

 

This seemed to happen today to my computer at work. Which is odd since there was not even any graphics load on the board at the time. The machine was running linux and a different graphics card was in use at the time. I guess the AMD heat sinks are just not up to spec. The reason why I would say that is because the other identical graphics card also had a heat sink failure which caused the board to fail. The result was the machine locking up then failing to post on reboot. Though I don't think I have ever seen a heat sink split in quite this way before!

 

 

Note: The other card had a different failure. It's heat sink had failed on the leg of the heat sink that attached it to the board.

E-mail Kick it! DZone it! del.icio.us Permalink


Linux - List / Copy group membership for users

12. December 2012 08:00

 

A quick guide on how to copy group member ship in linux to another user. Which can be useful when setting up new users on a linux machine to make sure that users. It is also a way to find out what groups a user is a member of.

 

Part 1 - Get a list of groups

 

To get a list of groups as user is a member of can be done by reading the /etc/group file and doing a little bit of processing. This can be done using the following command.

 

grep -E "(:|,)<username>(:,|$)" /etc/group|cut -f1 -d:

 

The complex expression above is built to match specific username in each line of the file. Of which there is 3 different cases ":<username>"  ",<username>,"  ",<username>". So we search for the specific username beginning with a ":" or a "," and also ending in a "," or a "$" which is a newline. Then it cuts on the first field using ":" as a separator.

 

This will output  a list of group that "<username>" is a member of

 

Part 2 - Add another user to the same list of groups

 

Since we know that addgroup can be used to add a user to a group by doing "addgroup <username> <groupname>" then we can expand the above to add a username to each group in the list like this

 

for i in `grep -E "(:|,)<username>(:,|$)" /etc/group|cut -f1 -d:` ; do

  addgroup <newuser> $i

done

 

This will then make sure that the user <newuser> is a member of all the same groups that <username> is.

 

You should take care using the above as you may give somebody access well beyond what you thought you might have by giving them access to additional groups.

 

E-mail Kick it! DZone it! del.icio.us Permalink