Tuesday, 15 January 2019

fork() System call programs - ubuntu


/* Process creation - fork.c */
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>  // for datatypes
#include<sys/types.h> // for standard symbolic constants & types
void main()
{
  int x;
  x=fork();
  printf(" \n process pid =%d", getpid());
  printf(" \n returned value =%d\n", x);
}


output



/* Process creation - fork.c */
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>  // for datatypes
#include<sys/types.h> // for standard symbolic constants & types
void main()
{
 int x;
 x=fork();
 if(x == 0)
  {
   printf("\n\n CHILD PROCESS STARTS");
   printf(" \n process pid =%d", getpid());
   printf(" \n returned value =%d\n", x);
  }
 else
  {
   printf("\n\n PARENT PROCESS STARTS ");
   printf(" \n process pid =%d", getpid());
   printf(" \n returned value =%d\n", x);
  }
}

output



/* Process creation - fork.c */
#include<stdio.h>
#include<unistd.h>  // for datatypes
#include<sys/types.h> // for standard symbolic constants & types
void main()
 {
  int x=10;
    if(fork() == 0)
    {
             sleep(10);
            x=x+10;
            printf("\n from child =%d", x);
    }
    else
        printf("\n from parent =%d", x);
 }

output



/* Process creation - fork.c */
#include<stdio.h>
#include<unistd.h>  // for datatypes
#include<sys/types.h> // for standard symbolic constants & types
void main()
{
fork();
fork();
fork();
printf("\n Hi !");
}

output:

No comments:

Post a Comment