fork(), getpid() and exit() in linux- adri jovin

1
#include<sys/types.h> #include<unistd.h> #include<stdio.h> int main() { int pid; printf("\n\n Fork Function Testing"); printf("\n\n My Process ID=%d",getpid()); printf("\n\n My Parent's ID=%d",getpid()); pid=fork(); if(pid==-1) { printf("\n\n Error occurred... \n Process cannot be created "); exit(0); } if(pid!=0) { printf("\n\n Parent : My ID=%d",getpid()); printf("\n\n My Parent ID=%d",getpid()); exit(0); } else { printf("\n\nChild : My ID=%d",getpid()); printf("\n\n My Parents's ID=%d",getpid()); exit(0); } }

Upload: adri-jovin

Post on 29-Jun-2015

1.836 views

Category:

Education


2 download

DESCRIPTION

Program to work with fork(), getpid() and exit()

TRANSCRIPT

Page 1: fork(), getpid() and exit() in Linux- Adri Jovin

#include<sys/types.h> #include<unistd.h> #include<stdio.h> int main() { int pid; printf("\n\n Fork Function Testing"); printf("\n\n My Process ID=%d",getpid()); printf("\n\n My Parent's ID=%d",getpid()); pid=fork(); if(pid==-1) { printf("\n\n Error occurred... \n Process cannot be created "); exit(0); } if(pid!=0) { printf("\n\n Parent : My ID=%d",getpid()); printf("\n\n My Parent ID=%d",getpid()); exit(0); } else { printf("\n\nChild : My ID=%d",getpid()); printf("\n\n My Parents's ID=%d",getpid()); exit(0); } }