Showing posts with label UNIX. Show all posts
Showing posts with label UNIX. Show all posts

17 October 2010

SVN Tutorial fo Unix

Posted from Diigo. The rest of my favorite links are here.

12 October 2010

The differences between write() and C library IO functions?

write() is a system call, the strings or characters it writes doesn't get buffered by a user-space buffer. C library IO functions like fputs() ,fprintf() or puts() is user space code; all the data they write is written to a user space buffer (you don't notice this if all you are writing is user space codes, if you are doing system programming with some user space codes you will find that the data actually gets buffered). To make such data out of buffer, use fflush(). Check the man page of fflush() yourself.

IPC using UNIX pipes

Pipe is the easiest form of UNIX interprocess communication, but there is a few tips to be remembered before using it.

Pipes inside a single process is basically useless; therefore we usually do piping with fork(). And here comes one important concept: pipes can only help RELATED processes communicate, meaning only processes that forked or created by the same ancestor can communicate using pipes. Non-related processes can communicate with each other by other forms of channel like shared memory or sockets.

If you want n processes to communicate through pipes, you'll need at least n-1 different pipes. This is because one pipe can only connect two processes.

So how do with use pipes with forks? First you need to create all the pipes (using pipe()) you need in the 'oldest' parent before any forking occurs. This way all the pipes created get inherited by all the descendants. A bit of caution here: 'pipes get inherited' meaning that all the file descriptors have their distinct copies for each child process; however, each copy actually refers to the SAME file (an end/terminal of a pipe/channel in the kernel); in other words, the channel inside the kernel does not have copies, the channel is the channel, both ends of the channel are referenced/referred by several copies of file descriptors having identical names.

So now you can do forks. But how? different ways of forking creates different architecture/processes tree. If you want to create a pipeline among a series of processes, it is better you use a 'chain' of processes architecture. For reference (a nice one), check this out: http://tinyurl.com/2fonwzn.

After the necessary setup mentioned in the previous paragraphs, you need to do some necessary 'closing' (close()) and 'duping' (dup2()) in each child process to actually make each pipe works correctly.

To visualize the concepts mentioned above, here is a link: http://tinyurl.com/6xx88e.

15 September 2010

Important concepts about Unix Pipes architectures

  • UNIX pipe concepts

    • One important thing to note here is that each process in the pipeline is a child of the original child of the shell (pid 202)

    • They are not children of each other the further down the pipeline we go

    • Another thing to note is that only the shell (process 163) executes a wait. All the others simply die after they exec their respective command

Posted from Diigo. The rest of my favorite links are here.

07 August 2010

How to access environments variables in your C program under UNIX

"The ISO/ANSI C standard requires that a C library include the getenv function, which retrieves the value of a named environment variable. Both the name and value are character strings."

Use getenv function! Don't use getpwuid(getuid())->pw_dir to access struct passwd! getenv is a handy function to access environments variables like HOME and PWD.