1.24.2006

Forking in UNIX/Linux/OS X

Here is some code to fork a new process in UNIX and similar operating systems. Note: Forking is not most efficient method for multiprogramming because it is inefficient, but it is easy. The problem with forking is that it copies the whole address space of the parent process before resuming execution. In most cases people want to execute another program after forking. When an exec (or some variant of exec) is called, the first thing that happens is the address space of that process is wiped out and the new program is loaded into that address space. This is very inefficient because the kernel has to do all of this for the processes. If you don't want to copy the whole address space, check out the
vfork()
command. Here is the code anyways.


#include <stdio.h>
#include <unistd.h>

int main(void)
{
pid_t pid;
int wait_for_child = 1;
int exec_res;
int value = 1;

char* command = "/bin/ls";
char* command_name = "ls";

// Fork new process, copies address space of parent
// process so static data, stack and heap are not shared.
// See
pid = fork();

// Both child and parent processes start here after the
// fork is called

if (pid == 0)
{
// Increment value in childs address space
value++;

// This is the child process
printf("I am the child. [%i]\n", value);

exec_res = execlp(command, command_name, NULL);

// Need to exit on error
if (!exec_res)
{
printf("Error. Could not exec.");
}

// Exit child process
exit(0);

}
else if (pid > 0)
{

// wait_for_child if exec is synchronous
if (wait_for_child)
{
printf("Wait for child to finish before resuming.\n");
wait(NULL);
}

printf("I am the parent. My child is %i. [%i]\n", pid, value);
}
else
{
printf("Error. Could not fork.");
}

return 1;
}

1.21.2006

Replace Colors with ImageMagick

ImageMagick has a great utility called convert. It allows you to do many things, including replacing one color in an image with another.

convert -fill #FFC232 -opaque #000000 inputfile outputfile

This will relace all the black pixels in an image with a dark yellow color. This is only a small part of what ImageMagick utilities can do. I recommend downloading them all.

1.09.2006

Reverse a linked list

This algorithm is a little more efficient than the last I posted about sorting a linked list, running in O(N) time. The algorithm requires a doubly linked list.


queue_length = queue_size(q);

front = q->head;
back = q->tail;

for (i = 0; i < queue_length / 2; i++) {

temp = front->e;
front->e = back->e;
back->e = temp;

front = front->next;
back = back->prev;
}

Sort a linked list

This is not an efficient way to sort a linked list (runs in O(N2), but it works when you don't care about efficiency (sometimes homework for a class). This code sorts a linked list in place with bubble sort.


do {
num_swaps = 0;
cur = q->head;
while (cur) {
if (cur->next) {
res = queue_element_compare(cur->e, cur->next->e);
if (res == 1) {
temp = cur->e;
cur->e = cur->next->e;
cur->next->e = temp;
num_swaps++;
}
}
cur = cur->next;
}
} while (num_swaps > 0);


queue_element_compare returns 0 on equal, 1 if e1 > e2 and -1 when e1 < e2.