Q: Write a program in C for linux fork system call. Also show zoombie and orphaned processes.
The use of fork system call is displayed using the following two programs
Before fork
PPID=1503 PID=1897
....................................
After fork
After fork
This is from the child with pid=1898
This is from the child with ppid=1897
Value returned by fork system call is0
The child process is now sleeping for 5seconds
This is from parent with pid=1897
This is from the parent with ppid=1503
Value returned by fork system call is1898
The parent process will now be terminated[student@localhost grpb]$
The value of child process ppid after termination of parent process is1
End of program
Fork2
Fork2 output
[student@localhost grpb]$ gcc fork2.c
[student@localhost grpb]$ ./a.out
Study of fork system call
Before fork
PPID=1503 PID=2459
....................................
After fork
This is from parent with pid=2459
This is from the parent with ppid=1503
After fork
This is from the child with pid=2460
This is from the child with ppid=2459
This is the state of the process while child process is yet to execute
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
0 S 1017 1503 1498 0 80 0 - 29014 wait pts/0 00:00:00 bash
0 T 1017 1734 1503 0 80 0 - 26716 signal pts/0 00:00:00 cat
0 T 1017 1844 1503 0 80 0 - 29705 signal pts/0 00:00:00 man
0 T 1017 1860 1844 0 80 0 - 27901 signal pts/0 00:00:00 less
0 S 1017 2459 1503 0 80 0 - 1028 hrtime pts/0 00:00:00 a.out
1 S 1017 2460 2459 0 80 0 - 1028 wait pts/0 00:00:00 a.out
0 R 1017 2461 2460 0 80 0 - 28409 - pts/0 00:00:00 ps
End of program
State of the process after the execution of the child is finished
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
0 S 1017 1503 1498 0 80 0 - 29014 wait pts/0 00:00:00 bash
0 T 1017 1734 1503 0 80 0 - 26716 signal pts/0 00:00:00 cat
0 T 1017 1844 1503 0 80 0 - 29705 signal pts/0 00:00:00 man
0 T 1017 1860 1844 0 80 0 - 27901 signal pts/0 00:00:00 less
0 S 1017 2459 1503 0 80 0 - 1028 wait pts/0 00:00:00 a.out
1 Z 1017 2460 2459 0 80 0 - 0 exit pts/0 00:00:00 a.out <defunct>
0 R 1017 2462 2459 0 80 0 - 28409 - pts/0 00:00:00 ps
End of program
[student@localhost grpb]$
The use of fork system call is displayed using the following two programs
Fork1
#include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <sys/types.h> int main(int argc,char *argv[]) { int valpid; printf("\nStudy of fork system call"); printf("\nBefore fork"); printf("\nPPID=%d\tPID=%d",getppid(),getpid()); printf("\n...................................."); fflush(NULL); //this prevents double printing of the earliear line valpid=fork(); printf("\nAfter fork"); if(valpid>0){ fflush(NULL); sleep(2); printf("\nThis is from parent with pid=%d",getpid()); printf("\nThis is from the parent with ppid=%d",getppid()); printf("\nValue returned by fork system call is%d",valpid); printf("\nThe parent process will now be terminated"); exit(1); } else if(valpid==0){ printf("\nThis is from the child with pid=%d",getpid()); printf("\nThis is from the child with ppid=%d",getppid()); printf("\nValue returned by fork system call is%d",valpid); printf("\nThe child process is now sleeping for 5seconds"); fflush(NULL); //to print the above printf statement before sleeping sleep(5); printf("\nThe value of child process ppid after termination of parent process is%d",getppid()); } else{ printf("\nError condition"); printf("\nFork Unsuccessful"); } printf("\nEnd of program\n"); return 0; }
Fork1 output
Study of fork system callBefore fork
PPID=1503 PID=1897
....................................
After fork
After fork
This is from the child with pid=1898
This is from the child with ppid=1897
Value returned by fork system call is0
The child process is now sleeping for 5seconds
This is from parent with pid=1897
This is from the parent with ppid=1503
Value returned by fork system call is1898
The parent process will now be terminated[student@localhost grpb]$
The value of child process ppid after termination of parent process is1
End of program
Fork2
#include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <sys/types.h> int main(int argc,char *argv[]) { int valpid; printf("\nStudy of fork system call"); printf("\nBefore fork"); printf("\nPPID=%d\tPID=%d",getppid(),getpid()); printf("\n...................................."); fflush(NULL); //this prevents double printing of the earliear line valpid=fork(); printf("\nAfter fork"); fflush(NULL); if(valpid>0){ printf("\nThis is from parent with pid=%d",getpid()); printf("\nThis is from the parent with ppid=%d",getppid()); fflush(NULL); sleep(5); printf("State of the process after the execution of the child is finished\n"); system("ps -l"); fflush(NULL); } else if(valpid==0){ printf("\nThis is from the child with pid=%d",getpid()); printf("\nThis is from the child with ppid=%d\n",getppid()); printf("This is the state of the process while child process is yet to execute\n"); system("ps -l"); fflush(NULL); } else{ printf("\nError condition"); printf("\nFork Unsuccessful"); } printf("\nEnd of program\n"); fflush(NULL); return 0; }
Fork2 output
[student@localhost grpb]$ gcc fork2.c
[student@localhost grpb]$ ./a.out
Study of fork system call
Before fork
PPID=1503 PID=2459
....................................
After fork
This is from parent with pid=2459
This is from the parent with ppid=1503
After fork
This is from the child with pid=2460
This is from the child with ppid=2459
This is the state of the process while child process is yet to execute
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
0 S 1017 1503 1498 0 80 0 - 29014 wait pts/0 00:00:00 bash
0 T 1017 1734 1503 0 80 0 - 26716 signal pts/0 00:00:00 cat
0 T 1017 1844 1503 0 80 0 - 29705 signal pts/0 00:00:00 man
0 T 1017 1860 1844 0 80 0 - 27901 signal pts/0 00:00:00 less
0 S 1017 2459 1503 0 80 0 - 1028 hrtime pts/0 00:00:00 a.out
1 S 1017 2460 2459 0 80 0 - 1028 wait pts/0 00:00:00 a.out
0 R 1017 2461 2460 0 80 0 - 28409 - pts/0 00:00:00 ps
End of program
State of the process after the execution of the child is finished
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
0 S 1017 1503 1498 0 80 0 - 29014 wait pts/0 00:00:00 bash
0 T 1017 1734 1503 0 80 0 - 26716 signal pts/0 00:00:00 cat
0 T 1017 1844 1503 0 80 0 - 29705 signal pts/0 00:00:00 man
0 T 1017 1860 1844 0 80 0 - 27901 signal pts/0 00:00:00 less
0 S 1017 2459 1503 0 80 0 - 1028 wait pts/0 00:00:00 a.out
1 Z 1017 2460 2459 0 80 0 - 0 exit pts/0 00:00:00 a.out <defunct>
0 R 1017 2462 2459 0 80 0 - 28409 - pts/0 00:00:00 ps
End of program
[student@localhost grpb]$
No comments:
Post a Comment