(Created page with "<syntaxhighlight lang="C" name="banker_algorithm"> #include <unistd.h> #include <stdio.h> #include <sys/types.h> #include <signal.h> #include <sys/wait.h> int main() { uns...")
 
Line 1: Line 1:
<syntaxhighlight lang="C" name="banker_algorithm">
+
The program should wait for a name to be entered by the user and then display "Thank you". If no input is given for 10s, the program should exit with output "Failed".
 +
 
 +
 
 +
<syntaxhighlight lang="C" name="fork">
  
  
Line 17: Line 20:
 
if(pid == 0)//pid = 0 for child only
 
if(pid == 0)//pid = 0 for child only
 
{
 
{
 +
                printf("Enter name: ");
 
fgets(name, 100, stdin);
 
fgets(name, 100, stdin);
 
printf("Thank you\n");
 
printf("Thank you\n");

Revision as of 18:17, 31 July 2014

The program should wait for a name to be entered by the user and then display "Thank you". If no input is given for 10s, the program should exit with output "Failed".


<syntaxhighlight lang="C" name="fork">


  1. include <unistd.h>
  2. include <stdio.h>
  3. include <sys/types.h>
  4. include <signal.h>
  5. include <sys/wait.h>

int main() { unsigned i, l = 0, j; int status; char name[100]; pid_t pid; pid = fork();//child starts if(pid == 0)//pid = 0 for child only {

               printf("Enter name: ");

fgets(name, 100, stdin); printf("Thank you\n"); kill(getppid(), 9);//kills the parent return;// child finishes here } for(i = 0; i < 500000; i++) for(j = 0; j < 10000; j++) l++; // l is used so that compiler just won't remove the loop for optimization if(waitpid(pid, &status, WNOHANG) == 0)//Child still running { kill(pid, 9);// kills the child

if(! printf("Failed\n")) printf("l = %u",l);// for ensuring the code remains after optimization }

}


</syntaxhighlight>




blog comments powered by Disqus

<syntaxhighlight lang="C" name="banker_algorithm">


  1. include <unistd.h>
  2. include <stdio.h>
  3. include <sys/types.h>
  4. include <signal.h>
  5. include <sys/wait.h>

int main() { unsigned i, l = 0, j; int status; char name[100]; pid_t pid; pid = fork();//child starts if(pid == 0)//pid = 0 for child only { fgets(name, 100, stdin); printf("Thank you\n"); kill(getppid(), 9);//kills the parent return;// child finishes here } for(i = 0; i < 500000; i++) for(j = 0; j < 10000; j++) l++; // l is used so that compiler just won't remove the loop for optimization if(waitpid(pid, &status, WNOHANG) == 0)//Child still running { kill(pid, 9);// kills the child

if(! printf("Failed\n")) printf("l = %u",l);// for ensuring the code remains after optimization }

}


</syntaxhighlight>




blog comments powered by Disqus