Archive for April 2010

OCaml and Unix programming

If you are used to the Unix programming with C, then almost the same concepts apply to OCaml using the Unix module, with slightly different syntax for function calls and arguments.

It is pretty straight forward if you have already done some system programming in C before, to compile you should add unix.cma to the arguments of ocamlc.

Example 1: fork, getpid and getppid :

  1. open Unix;;
  2. open Printf;;
  3.  
  4. let a = fork () in
  5.   match a with
  6.     | 0 -> printf "child -- pid : %d ,ppid : %d\n" (getpid ()) (getppid ())
  7.     | -1 -> printf "%s" "error accured on fork\n"
  8.     | _ -> printf "parent process : %d\n" (getpid ())
  9. ;;

This example shows how to do a simple fork to create a new process, then each process prints its pId to the console, here we cannot ensure which one will print first, the parent or the child. Here we use the usual function fork, getpid to get the ID of the current process and getppid to get the parent process.

Compile :

  1. sky@sky-laptop:~/Desktop$ ocamlc unix.cma demo.ml -o demo

Output 1:

  1. sky@sky-laptop:~/Desktop$ ./demo
  2. parent process : 3249
  3. child -- pid : 3250 ,ppid : 3249

Output 2:

  1. sky@sky-laptop:~/Desktop$ ./demo
  2. child -- pid : 3250 ,ppid : 3249
  3. parent process : 3249

Example 2, wait:

To ensure that the child process finish before its parent we use the wait system call, which returns the id of the finished process and the status code returned from the process.

  1. open Unix;;
  2. open Printf;;
  3.  
  4. let a = fork () in
  5.   match a with
  6.     | 0 ->  printf "child -- pid : %d ,parent pid : %d\n" (getpid ()) (getppid ());
  7.         exit 13
  8.     | -1 -> printf "%s" "error accured on fork\n"
  9.     | _ -> let childid, returncode = wait () in
  10.         printf "parent process : %d\n" (getpid ());
  11.         printf "child %d closed with status code %d" childid (let WEXITED(code) = returncode in code)
  12. ;;

Output :

  1. sky@sky-laptop:~/Desktop$ ./demo
  2. child -- pid : 3268 ,parent pid : 3267
  3. parent process : 3267
  4. child 3268 closed with status code 13

As you see, the child exits with the code 13, the parent calls wait to wait for its unique child created with fork, then reads the returned code, note that OCaml will generate a warning here because we just check for WEXITED (we know it will exit normally) but we omit WSIGNALED and WSTOPPED. In C you don’t have this flexibility of warnings which can brings you a lot of trouble.

Exemple 3, exec*:

This example demonstrates how to use the exec* calls, these methods replaces the core of the process with the new code of the command to be executed thus the function never returns, when the function fails, an exception is thrown, check the specifications here.

  1. open Unix;;
  2. open Printf;;
  3.  
  4. let a = fork () in
  5.   match a with
  6.     | 0 -> (try
  7.           execvp "ls" [|"ls"; "-l"|]
  8.        with
  9.           _ -> printf "%s" "error while execv\n"; exit (-1))
  10.     | -1 -> printf "%s" "error accured on fork\n"
  11.     | _ -> ignore (wait ()); printf "%s" "parent exit...\n"
  12. ;;

Here we fork and replace the child code with the ls command’s, here is the output

  1. sky@sky-laptop:~/Desktop$ ./demo
  2. total 136
  3. -rwxr-xr-x 1 sky sky 82110 2010-04-20 00:26 demo
  4. -rw-r--r-- 1 sky sky   234 2010-04-20 00:26 demo.cmi
  5. -rw-r--r-- 1 sky sky   743 2010-04-20 00:26 demo.cmo
  6. -rw-r--r-- 1 sky sky   291 2010-04-20 00:26 demo.ml
  7. -rw-r--r-- 1 sky sky   410 2010-04-20 00:26 demo.ml~
  8. parent exit...

Posted in , |

Run Unix commands on Widows cmd

For those of you who can’t live a day without typing at least a thousand “ls” and few hundreds “ps”, “cat” etc… You can do it on Windows cmd too.

By installing Cygwin and setting the PATH variable to point the Cygwin’s bin directory, most of the commands will work as usual from the Windows cmd, not just Cygwin’s shell. Of course it won’t be as real as running these commands on a Unix machine, but it still handy to do so anyways.

For example the ps command will only show processes that were started from the current shell (cmd), what’s funny also is that you can type both windows and Unix commands together.

win cmd unix

Posted in , |

Swedish Greys - a WordPress theme from Nordic Themepark. Converted by LiteThemes.com.