Jun 15 2007

Do OS programming…Get the essence of programming

osprogramming.jpg

Operating system level programming has a different taste than any other
programming. It is very simple to understand the operating systems layers if
you do os programming in linux. You can start that with system calls such as
fork, pipe, memget, kill etc .

For example, you know pipe. In linux command prompt ‘|’
is using for pipe. For example ls is the command for list the directory …
ls | more means ?? It means give the output of ls command to more command. “more”
command’s input will be the output of “ls” command. Here the
output and input are piped. Standard input and output are changed.
(“more” command is using for showing page by page data)

Example Code:

Click read more for sample code

#include 
#include 
#include 
#include 

int main(){

pid_t pid;
int rv;
int commpipe[2]; /* This holds the fd for the input & output of the pipe
*/

/* Setup communication pipeline first */
if(pipe(commpipe)){
fprintf(stderr,"Pipe error!\n");
exit(1);
}

/* Attempt to fork and check for errors */
if( (pid=fork()) == -1){
fprintf(stderr,"Fork error. Exiting.\n"); /* something went wrong
*/
exit(1);
}

if(pid){
/* A positive (non-negative) PID indicates the parent process */
dup2(commpipe[1],1); /* Replace stdout with out side of the pipe */
close(commpipe[0]); /* Close unused side of pipe (in side) */
setvbuf(stdout,(char*)NULL,_IONBF,0); /* Set non-buffered output on stdout */
sleep(2);
printf("Hello\n");
sleep(2);
printf("Goodbye\n");
sleep(2);
printf("exit\n");
wait(&rv); /* Wait for child process to end */
fprintf(stderr,"Child exited with a %d value\n",rv);
}
else{
/* A zero PID indicates that this is the child process */
//dup2(commpipe[0],0); /* Replace stdin with the in side of the pipe */
close(commpipe[1]); /* Close unused side of pipe (out side) */
/* Replace the child fork with a new process */
if(execl("child","child",NULL) == -1){
fprintf(stderr,"execl Error!");
exit(1);
}
}
return 0;
}

TAGS:

LEAVE A COMMENT

Subscribe Form

Subscribe to Blog

Sponsors

    Advt on sajithmr.com
    Advt on sajithmr.com
    Itslife Online
    Advt on sajithmr.com

Recent Comments

  • sexasion: Best of the Best sexypicturess.blogspot.com
  • memo ezzo: live sex hot video
  • muhammadashraf44: i am ashraf from karachi i have only one girl in sex and one night plzz
  • Arun Basil Lal: Dude, The comments to this post are ever flowing in, I am gonna post some hot pics on my blog too.....
  • badar: plz send me the hot pic

Recent Readers

JOIN MY COMMUNITY!

Recent Posts