You are on page 1of 7

21BCE3017 Lanka Nishanth George L55+56

Lab Assignment – 2
Faculty: Thamizharasan S
Slot: L55+56
Name: Lanka Nishanth George
Reg No: 21BCE3017
21BCE3017 Lanka Nishanth George L55+56

1)Execute a program to create the following process:


a) Orphan process
b) Zombie process
Display the outputs of corresponding process
a :Orphan Process:
Code:
#include<stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(){
int process_id;
process_id = fork();
if(process_id == 0){
printf("I am the child, my process ID is %d\n", getpid());
printf("My parent's process ID is %d\n", getppid());
sleep(6);
printf("\nAfter sleep\nI am the child, my process ID is %d\n",
getpid());
printf("My parent's process ID is %d\n", getppid());
}
else{
sleep(3);
printf("I am the parent, my process ID is %d\n", getpid());
printf("The parent's parent process ID is %d\n", getppid());
printf("Parent terminates\n");
}
return 0;
}
21BCE3017 Lanka Nishanth George L55+56

Output:

Output:
21BCE3017 Lanka Nishanth George L55+56

b:Zombie process:
Code:
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
int a, b, c;
pid_t pid = fork();
if (pid > 0)
{
printf("Parent process is going to sleep\n");
sleep(10);
printf("Parent process is resumed\n");
}
else if (pid == 0)
{
printf("Child process executing\n");
printf("Child terminates\n");
exit(0);
}
else
{
printf("Fork failed\n");
return 1;
}
return 0;
}
21BCE3017 Lanka Nishanth George L55+56

Output:

Output:
21BCE3017 Lanka Nishanth George L55+56

2)Write a C program to execute the following shell commands.


a) date
b) ls
Display the results of corresponding shell commands.

Code:
#include <stdio.h>
#include <stdlib.h>
int main() {
printf("Executing 'date' command:\n");
system("date");
printf("\nExecuting 'ls' command:\n");
system("ls");
return 0;
}
21BCE3017 Lanka Nishanth George L55+56

Output:

Output:

You might also like