07 April 2013

learn how you can keep your computer Tidy


first of all lets say you have written a code like this..
[cpp]
int main(){
float a,b;
scanf("%f %f",&a,&b);
printf("%f",(a/b));
printf("hello another worldn");
}
[/cpp]
everything was going right until your enemy came and input 0 for b.Dam a/0 error your program continues to run and print "hello another world".
what a shame this program is ,you enemy says..
you thought why it is a shame ,my program didn't crash and stopped and still it is running despite of error..
what a strong programmer i am.
but hey mate things didn't go this way in real programs.You should take necessary action if something goes wrong.for eg.
[cpp]
#include
int main(){
float radians;
int angle;
scanf("%f",&radians);
angle=(180/3.14)*radians;
printf("%d",angle);
printf("projecting rocket with this angle");
}
[/cpp]
what happens when this resulting angle is going to be used as the projection angle of a rocket?
if someone (may be another program ) input 0 radians then rocket will be rushing in streets right away
if instead the program would have been stopped and exited as soon as radians =0 is detected and inform its caller (may be some ground station operating system or program) so that the ground crew can take necessary action like rerun the program with correct input or analyze the formulas etc etc,then that would be a saving of lots of lives and money.
lets re write the code
[cpp]
int main(){
int radians;
int angle;
scanf("%d",&radians);
if(!radians)
exit(EXIT_FAILURE);
else{
angle=(180/3.14)*radians;
printf("%d",angle);
printf("start rocket with this angle");
exit(EXIT_SUCCESS);
}
}
[/cpp]
now this program does two things..
first it will exit on both successful completion or in successful completion and it will inform its caller (operating system in our case ) about it.
EXIT_SUCCESS and EXIT_FAILURE are constants with value 0 and -1.These values will be available to calling program.in our case it is ground station operating system.
for eg.
[cpp]
int main(){
printf("exiting with success");
exit(0);//this could also be exit(EXIT_SUCCESS)
}
type at linux shell
$ echo $?
you will get 0
[/cpp]
[cpp]
int main(){
printf("exiting with failure");
exit(-1);//this could also be exit(EXIT_FAILURE)
}
type at linux shell
$ echo $?
you will get -1
[/cpp]
only low order 8 bits of a number inside exit(number) is used as return value
so you can put error values from 0-255
[cpp]
#define ERROR_MY_OWN 255
int main(){
printf("exiting with some error");
exit(ERROR_MY_OWN);//this could also be exit(255)
}
type at linux shell
$ echo $?
you will get 255
[/cpp]
you can do some bad stuff like taking values greater then 255
for eg
[cpp]
# define ERROR_MY_OWN 356
int main(){
printf("exiting with some error");
exit(ERROR_MY_OWN);//this could also be exit(356)
}
type at linux shell
$ echo $?
you will get 100
[/cpp]
how
356 binary equivalent --> 0101100100
low order 8 bits --> 01100100
decimal equivalent of these low order 8 bits --> 100
now based on these return status the calling program (like another program on ground station) can take necessary steps.(like whether to launch rocket or not).
now since operating system is the parent process of all the above programs we have executed till now and so it is getting exit status of our programs (you see them by typing echo $?).
what if your programs are themselves parents and are creating child processes .There must be some way of knowing the return status of child processes so that the parent process can take further action.
for eg
you have a parent that creates two child processes one for writing and one for reading.Both of processes does their job and returns..
The child process that reads the data must be created after the child process that writes the data.
But with normal approach parent have no control over this.may be reading process starts its execution before writing process does.This is a complete mesh
lets see small example
[cpp]
/* program to write 200000 characters to a file abc.c
* and read them through two different processes
*/
void writeprocess(char *filename){
int n=200000;// no of characters to be print
FILE *fp=fopen(filename,"w");
while(n--){
fputc('t',fp);
}
close(fp);
exit(EXIT_SUCCESS);
}
void readprocess(char *filename){
FILE *fp=fopen(filename,"r");
char ch;
int c=0;
while ( 1 )
{
ch = fgetc ( fp ) ;
if ( ch == EOF )
break ;
else
c++;
}
printf("nNo oF Characters read : %d n",c);
if(c==200000)
exit(EXIT_SUCCESS);
else
exit(EXIT_FAILURE);
}
int main(){
pid_t t;
t=fork();
if(!t)
writeprocess("abc.c");
t=fork();
if(!t)
readprocess("abc.c");
exit(0);
}
[/cpp]
If you run this program multiple times you will find most of the time value of c (no of characters ) to be less then 200000
why?
since there is no guarantee that read process will run only after write process gets terminated.
suppose write process has written 100000 characters and now it is switched with read processs in processor(because processor can run only one process at a time) as soon as read process will find eof after 100000 it will exit and give the output.
Then again writeprocess is switched in processor which will write remaining 100000 characters but now there is no read process to read these characters.
In this problem we have to consider some cases...

SHARE THIS

Author:

0 comments: