Main (Fork Printf (,getpid ) )

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

#include<stdio.

h>
void main()
{

fork();
printf(" the PID is %d \n",getpid());

#include<stdio.h>
void main()
{
int i=0,pid;

pid = fork();

if(pid == 0)
{

for(i=0;i<500;i++) printf(" %d \n ",i);

}
else
{

wait(0);
printf(" parent process syncs \n ");

}
#include<stdio.h>
void main()
{

int pid,ppid;
pid = getpid();
ppid = getppid();
printf(" the process id and parent id of this process \n %d
% d",pid,ppid);
}

#include<stdio.h>
void main()
{
fork();
printf("\n hello world");

#include<stdio.h>
void main()
{

fork();
printf("\n hello world");
printf("\n %d \n %d",getpid(),getppid());

}
#include<stdio.h>
void main()
{

printf(" this is demonstrat the fork \n");


fork();
printf(" Hello world \n");

#include<stdio.h>
void main()
{

fork();
fork();
printf(" hello world \n");

#include<stdio.h>
void main()
{

int pid;

pid = fork();

if(pid ==0)
{

printf(" i am in the child %d \n %d ",getpid(),getppid());

}
else
{

printf( " i am in the parent %d \n %d ",getpid(),getppid());

#include<stdio.h>
void main()
{

int pid;

pid = fork();

if(pid == 0)
{

printf(" I am in the child %d Id %d \n",getpid(),getppid());

sleep(20);

printf(" I am in the child %d Id %d \n",getpid(),getppid());


}
else
{
printf(" I am in the parent %d Id %d
\n",getpid(),getppid());

}
}

#include<stdio.h>
void main()
{

int pid;

pid = fork();

if(pid==0)
{

printf(" i am the child process :\n%d


%d",getpid(),getppid());

sleep(20);

printf(" in the child : \n %d",getppid());

}
else
{

sleep(1);
printf("\n parent terminates altogether %d
%d",getpid(),getppid());

}
#include<stdio.h>
void main()
{

if( fork()>0)
{
printf(" i am the parent \n ");
sleep(50);
}

#include<stdio.h>
void main()
{
int pid;
int i = 0, j = 0;
pid = fork();

if(pid == 0)
{

for( i=0;i<500;i++) { printf ("child %d \t",i); }

else
{

for( i=0;i<500;i++) { printf ("parent%d \t",i); }

}
#include<stdio.h>
void main()
{

int pid,dip;
pid = fork();

if( pid == 0)
{

printf(" first child is created \n%d",getpid());

}
else
{

dip = fork();

if( dip == 0)
{

printf("second process is created \n %d",getpid());

}
else
{

sleep(15);

printf(" I am parent and dying \n");

}
#include<stdio.h>
void main()
{

int pid,dip,cpid;
pid = fork();
if(pid == 0)
{

printf("\n first child is created %d",getpid());

}
else
{

dip = fork();

if(dip == 0)
{

printf("\n second process is creatred %d",getpid());

}
else
{
cpid = wait(0);

printf("\n child with pid %d ", cpid);

cpid = wait(0);

printf("\n child with pid %d ",cpid);


printf("\n I am parent \n");

}
}
}
#include<stdio.h>
void main()
{

int pid,dip,cpid;
pid = fork();
if(pid == 0)
{

printf("\n first child is created %d",getpid());

}
else
{

dip = fork();

if(dip == 0)
{

printf("\n second process is creatred %d",getpid());

}
else
{

sleep();
cpid = wait(0);

printf("\n child with pid %d ", cpid);

cpid = wait(0);

printf("\n child with pid %d ",cpid);


printf("\n I am parent \n");

}
}
}
#include<stdio.h>
void main()
{

int pid,dip,cpid;
pid = fork();
if(pid == 0)
{

printf("\n first child is created %d",getpid());

}
else
{

dip = fork();

if(dip == 0)
{

printf("\n second process is creatred %d",getpid());

}
else
{

sleep();
cpid = wait(0);

printf("\n child with pid %d ", cpid);

cpid = wait(0);

printf("\n child with pid %d ",cpid);


printf("\n I am parent \n");

}
}
}
#include<stdio.h>
void main()
{

int i,pid,j=0;
int status;

pid = fork();

if(pid == 0)
{

i = 10 / j;

}
else
{

wait(&status);

if(status & 0x80)


printf(" core dumperd");

}
#include<stdio.h>
int i = 10;
void main()
{

int pid;

pid = fork();

if(pid == 0)
{

i = i + 10;

printf(" \n child terminates as ... %d \n",i);


}
else
{
wait(0);
printf("\n the value of i in parent \n %d", i);
}
}

#include<stdio.h>
void main()
{

int pid,*i;

*i = 10;

pid = fork();

if(pid == 0)
{

*i = *i + 10;
printf("\n the value in the child process %d \n ",*i);

}
else
{

wait(0);
printf(" \n value of i in the parent process %d \n",*i);

}
}

#include<stdio.h>
void main()
{

int pid;
int i = 10;

pid = fork();

if(pid == 0)
{

i = i + 10;

printf(" \n child terminates as ... %u \n",&i);


}
else
{
wait(0);
printf("\n the value of i in parent \n %u", &i);
}
}
#include<stdio.h>
#include<fcntl.h>

void main()
{

int fp;

char chr = 'a';


int pid;
pid = fork();
if(pid == 0)
{

fp = open("baby",O_WRONLY,0666);
chr = 'b';
write(fp,&chr,1);
printf("\n the child chr %c \n",chr);
close(fp);
}
else
{
wait(0);
fp = open("baby",O_RDONLY);
read(fp,&chr,1);
printf("\n the read chr %c \n ",chr);
close(fp);
}
}

You might also like