Sunday, December 8, 2013

Working of strstr function

strstr() is a function used to copy location of a specific word in a string to a pointer variable.
Header file: string.h
The function's prototype is as follows
 char * strstr (char * str1, const char * str2 );  
str1 is the string to be scanned
str2 is string containing the specific word or character

For example:
Let str[30]="This is a sample"
Let address of str[0] be equal to 2000
Let 'ptr' be a pointer variable pointing to character data[char * ptr]

Now using strstr()
 ptr=strstr(str,"sample");  
This returns address 2010 to ptr (because in str address 2010 points to 's' of sample)
If the match is not found, strstr() returns a NULL pointer.

Tags:-strstr(),c programming,address copying

 

 






Thursday, October 31, 2013

wait() and fork() Function

Wait function is used to wait for state changes in a child process of the calling process, and obtain information about the child whose state has been changed. A state change can be termination of a child or termination by a signal or resume of child by a signal. If a child has already changed the state these return immediately.

Header file:<sys/types.h>,<sys/wait.h>

While going through http://programmingundersecurity.blogspot.in/2013/10/understanding-fork-in-c.html you must have come across a point that for controlling the execution order(that is 'parent'->'child'->'parent resumes') we must wait until the child process has completed execution. So in order to work with that we use wait() function. wait() takes the address of an int and puts the exit status of the child it waited for.
Consider the code:

 #include <stdio.h>   
 #include <unistd.h>   
 #include <stdlib.h>  
 int main ()   
 {   
  int pid,status;   
  printf("Hello World\n");   
  pid = fork();   
 wait(&status);  
  if(pid==-1)  
     printf("\nError unable call fork");  
  else if(pid != 0)   
   printf("I'm the Father,my son's PID is %d and exit status is %d\n",pid,status);   
  else   
   { printf("I'm the Son\n");   
     exit(0);}  
  printf("Goodbye Cruel World\n");   
 }  


Output

 Hello World  
 I'm the Son  
 I'm the Father,my son's PID is 2750 and exit status is 0  
 Goodbye Cruel World  

This was the oupt without using wait:
  Hello World   
  I'm the Father and my son's PID is 2749   
  Goodbye Cruel World   
  I'm the Son   
  Goodbye Cruel World   
Clearly the execution order has changed and the parent waited for the child to finish execution.
tags:wait, fork, execution, process, programming, c

Wednesday, October 30, 2013

Understanding fork in C

The function fork has exactly the meaning it has on English dictionary.It is the point where something, a road or river divides into two parts. In programming the process divides into two parts thus creating a child process and parent process. The parent process  is actually the process we are running.

Header File: <unistd.h>

Whenever we use fork() a child process is created. The working of fork can be understood by considering the following example:
 #include <unistd.h>   
 int main ()  
 {  
  printf("Hello World\n");  
  fork();  
  printf("Goodbye Cruel World\n");  
 }  

Output

 Hello World  
 Goodbye Cruel World  
 Goodbye Cruel World  

You can see that the line after which the function 'fork()' is called, is printed twice or rather the next statement after the function is executed twice. When the function is called it creates the copy of that function and name it as child and starts executing it (child) after the fork() function and then it returns to the parent function and continue its execution. The child process will be run in a separate memory space and any change made to variables in te child won't affect the parent variable.
Consider the following example:

Code

 #include <stdio.h>   
 #include <unistd.h>   
 int main ()  
 {  
  int i=0;  
  printf("Hello World %d\n",i++);  
  fork();  
  printf("Goodbye Cruel World %d\n",i++);  
 }  

Output

 Hello World 0  
 Goodbye Cruel World 1   
 Goodbye Cruel World 1  

Lets analyse the output:
Hello World 0                   -PARENT
Goodbye Cruel World 1     -CHILD
Goodbye Cruel World 1    -PARENT

This order can change that is, this can also be the case: We execute the program, comes across fork(), complete execution of parent and then call the child to execute. The acually depends on the OS we cannot predict it.But we can do this thing: Execute program,comes across fork, ask parent to wait until child process get completed(click here to know more about wait()) ,now execute the remaining part of parent.

Identifying parent and child process

fork() will return a value 0 when it is called in child and will return pid of child when it is in parent process.If fork() fails then it returns -1 in parent process.(To know about PID click here) The following example illustrate the fact:

Code


 #include <stdio.h>   
 #include <unistd.h>   
 int main ()   
 {   
  int pid;   
  printf("Hello World\n");   
  pid = fork();   
  if(pid==-1)  
     printf("\nError unable call fork");  
  else if(pid != 0)   
   printf("I'm the Father and my son's PID is %d\n",pid);   
  else   
   printf("I'm the Son\n");   
  printf("Goodbye Cruel World\n");   
 }  

Output 

 Hello World  
 I'm the Father and my son's PID is 2749  
 Goodbye Cruel World  
 I'm the Son  
 Goodbye Cruel World  


Applications of Fork

  • Shell uses fork to run programs
  • Google chrome uses fork in order to handle the pages in separate process
  • Apache use fork to create multiple server process
Tags:-fork(), fork,c fork, programming, parent, child, process, wait