0% found this document useful (0 votes)
5 views

Assignment 6

The document is a lab manual for an Operating System course at Asansol Engineering College, focusing on process creation and types using the fork system call. It includes C programs demonstrating the creation of both a child process and an orphan process, along with their expected outputs. Key concepts such as process hierarchy and the behavior of orphan processes are also discussed.

Uploaded by

Amitayu das
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Assignment 6

The document is a lab manual for an Operating System course at Asansol Engineering College, focusing on process creation and types using the fork system call. It includes C programs demonstrating the creation of both a child process and an orphan process, along with their expected outputs. Key concepts such as process hierarchy and the behavior of orphan processes are also discussed.

Uploaded by

Amitayu das
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

ASANSOL ENGINEERING COLLEGE

Department of Computer Science and Engineering

Course Name: Operating Experiment No. 6


System Lab

Course Code : PCC-CS592 Branch: CSE Semester: 5

AIM: Knowledge on process creation and types.

Processes

A collection of logical control flows called processes (task or jobs).In other word a process is a program
in execution.
Each process is an instance of a running program.

Linux process hierarchy

Fork():

int fork(void)
• creates a new process (child process) that is identical to the calling process (parent
process)
• returns 0 to the child process
• returns child‘s pid to the parent process
• Returns negetive value if t fails to create process.

Operating System Lab Manual PCC-CS 592 Page 1


ASANSOL ENGINEERING COLLEGE
Department of Computer Science and Engineering
Problem: Write a c program to create a process using fork system call.
Program:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main() {
int pid;
// Create a new process
pid = fork();
if (pid < 0) {
// Error occurred
fprintf(stderr, "fork() failed\n");
exit(1);
}
// Child process
else if (pid == 0) {
printf("Child process ID: %d\n", getpid());
printf("Parent process ID: %d\n", getppid());
printf("This is the child process\n");
sleep(2);
printf("Child process exiting\n");
exit(0);
}
// Parent process
else {
printf("Parent process ID: %d\n", getpid());
printf("Child process ID: %d\n", pid);
printf("This is the parent process\n");
wait(NULL);
printf("Parent process exiting\n");
exit(0);
}
return 0;
}

Output:

Child process ID: 479


Parent process ID: 478
This is the child process
Child process exiting
Parent process ID: 478
Child process ID: 479
This is the parent process
Parent process exiting

Operating System Lab Manual PCC-CS 592 Page 2


ASANSOL ENGINEERING COLLEGE
Department of Computer Science and Engineering
Orphan Processes
Orphan processes are those processes that are still running even though their parent process has
terminated or finished. A process can be orphaned intentionally or unintentionally.

Problem: Write a c program to create an orphan process.


Program:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
pid_t pid;
// Create a new process
pid = fork();
if (pid < 0) {
// Error occurred
fprintf(stderr, "fork() failed\n");
exit(1);
}
// Child process (orphan process)
else if (pid == 0) {
printf("Child process ID: %d\n", getpid());
printf("Parent process ID: %d\n", getppid());
sleep(5); // Sleep for 5 seconds
printf("Orphan process exiting\n");
exit(0);
}
// Parent process
else {
printf("Parent process ID: %d\n", getpid());
printf("Child process ID: %d\n", pid);
printf("Parent process exiting immediately\n");
exit(0); // Parent exits immediately
}
return 0;
}

Output:

Parent process ID: 1234


Child process ID: 1235
Parent process exiting immediately
Child process ID: 1235
Parent process ID: 1 (init process)
Orphan process exiting

Operating System Lab Manual PCC-CS 592 Page 3

You might also like