TUTORIAL 2 Parallel Processing
Q=1. Program to create n number of child processes
from parent process and print process id of them.
int process_Fork(int n)
{
int i;
for(i=1;i<n;i++)
{
if(fork()==0)
{
return i;
}
}
return 0;
}
int main()
{
int id;
id =
process_Fork(3);
printf("Hello = %d\n ",id);
}
OUTPUT:-
[08ce55@linux ~]$ ./a.out
Hello = 1
Hello = 2
Hello = 0
Q=2. Program to join the processes. N number of
processes are forked out from parent process. Parent waits untill child
processes finishes their task and terminates a process_join().
#include<stdio.h>
int process_Fork(int n)
{
int i;
for(i=1;i<n;i++)
{
if(fork()==0)
{
return i;
}
}
return 0;
}
int process_Join(int id, int n)
{
int i;
if(id==0)
{
for(i=1;i<n;i++)
{
wait(0);
}
}
else
{
exit(0);
}
}
int main()
{
int id;
id =
process_Fork(3);
printf("Hello = %d\n ",id);
process_Join(id,3);
}
OUTPUT:-
[08ce55@linux ~]$ ./a.out
Hello = 1
Hello = 2
Hello = 0