• 1
    0
    Unix Shell commands are pretty powerful in doing lots of great jobs for you as a user or programmer. you can do so many things from simple to complicated problems with shell script. In this post i'll give a brief information about piping in unix shell and a small insight about pipes in C programming language.
    as you should already know when a program runs in Unix it creted 3 standard files, 0 for input(stdin), 1 for output(stdout), and 2 for error(stderr). Symbol > or < use for these purposes. for example imagine you have a executive program name a.out, you can have something like this:
    bash$ a.out < data 1 >res1 2>errors
    that means, program a.out gets the input from 'data' put its output to 'res1' and if any error happened redirect that to 'errors'. if the files used in the redirection don't exit it'll be create or if exist it'll be overwitten. you cn use >> to append the new data to the file, keeping the already existed data.
    there is also another operand i.e | which just pipe the output of one command to the input of the other command e.g imagine next command
    ls –ltr | tail –5
    this command list the content of current directory, based on the time, in reverse order and then passes the out put to 'tail' command using | operator . then tail command shows the last 5 entries. you can have multiple of | operators between multiple of commands. so far so easy.
    I give another example about > operator and then move to pipe in C programming language. Imagine next a few commands:

    bash$ cat >name_list
    Wang Ng Smith Lee
    Ng Howard Lee
    this command creates 'name_list' file and save the given data in that.

    bash$ fmt -1 name_list > names_split

    this command format the 'name_list' file in one line and save them in a file
    'names_split'. if not exists, create that file otherwise overwites.

    bash$ sort -u names_split > names_sort

    this command sorts the 'name_split' file and saves the data in 'names_sort' files. I hope this clear things a bit.
    three is lso another character ` (backward qoutation mark, down to Esc key) which is used when u want to execute one command and send its out put to the command follows it.
    like: myprog `cat argfile`
    will run the cat command, take the output (ie the contents of "argfile") and
    use it as the arguments for "myprog".
    now Pipe in C
    I'm not going into details, I just give some basic information.
    In c we can have Pipes for comunication between processes.
    you can use this : int pipe(int filedes[2]) in C.
    this creates a pipe, which is 2 element arrray.filedes[0] is for reading
    , and filedes[1] is for writing. fot using you just creat that pipe and use for comunication between processes. this is simple. we can also have named pipes, which their name is FIFO. FIFO is special files which can be make by command mknod, or mkfifi in C. and then processes can open one end for writing and ther other end for reading. although there needs to be a writer so reader can read data, otherwise reader blocked untill a writer writes some data to the pipe. normally they use FIFO along with fork to comunicate between parent and child process, or with signal for comunication between processes, or combination of both. for example imagine a process is running  opens a FIFO file(in read or write or combination) and it goes in the waiting stage for some signal to arrive from other process like SIGUSR1 or other signals . then in the other side, it opens the other end of FIFO then they can comunicate. you should have signal handler to handle the signal though. this seems a bit complicated...to be honest it is...but with more practice you can grasp the idea. there are some good resources online which can be helpful. I include a URL address regarding FIFO, which is just a named pipes.
    good luck
    pamador out
    Tags : Unix C programming
    Refrence : 1 item Show Refrence
    • 2013-06-09 04:27:24Z
    • Root Comment : 3 Sub Comment : 1
    • Tree
    • Visited : 2
    • Send Comment
    zagros said 2013-06-21 16:22:01Z
    I am so happy that you are keep posting new article. I have another idea which is useful for you mostly. we are keep improving to meet your basic requirement ... Read morebut still we are out of schedule.
    Reply No Idea 0 0 Tree + Open 1 sub comment
    Pamador said 2013-06-09 05:04:30Z
    C is really strong language. there are some typos, and error in this post. i just sat down, typed and poted this stuff. it needs some editing which i can do ... Read morethat, all site admin fault not mine..quran haggi.
    i say it here. function to creat FIFO filoes are mknod and mkfifo. FIFO needs to be open in both ends for comunication, by reader and writer.signals can be used by either side to have more control over the operation. howvere need signal handler which makes things a bit more comlicated.
    Reply Rate : 5 0 0 + Open 0 sub comment
    zagros said 2013-06-09 04:35:08Z
    I did not know that is this much strong!
    Reply Rate : 1 0 0 + Open 0 sub comment