0% found this document useful (0 votes)
16 views10 pages

163 OS Week4

This document outlines an assignment on operating systems, specifically focusing on the use of system calls such as fork(), execl(), and vfork(). It includes detailed descriptions and code examples for various processes, including child and parent processes, orphan processes, and zombie processes. The document also provides the expected output and instructions for observing process states.

Uploaded by

yeruvamokshitha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views10 pages

163 OS Week4

This document outlines an assignment on operating systems, specifically focusing on the use of system calls such as fork(), execl(), and vfork(). It includes detailed descriptions and code examples for various processes, including child and parent processes, orphan processes, and zombie processes. The document also provides the expected output and instructions for observing process states.

Uploaded by

yeruvamokshitha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Roll No: 160122733163 Exp.

No: 4 Date: 30-08-2024

OPERATING SYSTEMS

ASSIGNMENT-4

Aim: Write a program to demonstrate the use of fork() sytem call


Description:
Include Headers: We include the necessary headers for I/O functions and system calls (stdio.h,
stdlib.h, and unistd.h).
• Fork System Call: We call fork() to create a new process. The fork() function returns
a process ID (PID):
• Negative Value: Indicates an error occurred during the fork.
• Zero: Indicates the code is running in the child process.
• Positive Value: Indicates the code is running in the parent process, and the returned
value is the PID of the child process.
• Child Process:
• The child process prints its own PID and its parent’s PID.
• Parent Process:
• The parent process prints its own PID and the PID of its child.

CODE:
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
int main( ){
pid_t rf;
int a;
rf = fork (); // Create a new child process;
if (rf < 0) {
printf("fork failed");
return 1;
} else if (rf == 0) {
printf ("child process successfully created!\n");
printf ("child_PID = %d,parent_PID = %d\n",
getpid(), getppid( ) );
printf("inside rf=%d \n",rf);
} else {
// wait(NULL);
printf ("parent process successfully created!\n");
printf ("child_PID = %d, parent_PID = %d", getpid( ), getppid( ) );
printf("rf=%d \n",rf);
}
return 0;
}

Page no.…...52……. Signature of the Faculty………………………...


Roll No: 160122733163 Exp. No: 4 Date: 30-08-2024

OUTPUT:

Aim: Write a program to demonstrate the use of exe system calls Parent process --&gt;
Execute factorial of given number Child process --&gt; Execute prime number.

Description:
fork(): The main program creates a child process. Both the parent and child processes execute
different tasks.
• execl(): Replaces the current process image with a new process image.
• execl("./prime", "./prime", (char *)NULL); replaces the child process with the prime
program.
• execl("./factorial", "./factorial", (char *)NULL); replaces the parent process with the
factorial program.
• Parent process: Waits for the child to complete using wait(NULL) before replacing
its image with the factorial calculation.
• Child process: Immediately replaces its process image with the prime number
checking program.
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
// Function to calculate factorial
long long int factorial(long long int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
// Function to check if a number is prime
int is_prime(int n) {
if (n <= 1) return 0;
if (n <= 3) return 1;
if (n % 2 == 0 || n % 3 == 0) return 0;
for (int i = 5; i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0) return 0;

Page no.…...53……. Signature of the Faculty………………………...


Roll No: 160122733163 Exp. No: 4 Date: 30-08-2024

}
return 1;
}
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <number>\n", argv[0]);
exit(EXIT_FAILURE);
}
int num = atoi(argv[1]);
if (num < 0) {
fprintf(stderr, "Number must be non-negative.\n");
exit(EXIT_FAILURE);
}
pid_t pid = fork();
if (pid < 0) {
perror("fork failed");
exit(EXIT_FAILURE);
}
if (pid == 0) {
printf("Child Process:"); // Child process
if (is_prime(num)) {
printf("%d is a prime number.\n", num);
} else {
printf("%d is not a prime number.\n", num);
}
exit(EXIT_SUCCESS);
} else { // Parent process
wait(NULL); // Wait for the child process to complete
printf("Parent Process:\n");
printf("Factorial of %d is %lld \n", num, factorial(num));
exit(EXIT_SUCCESS);
}
return 0; }

Aim: Write a program to demonstrate use following exe functions


int execl(const char* path, const char* arg, …)
int execlp(const char* file, const char* arg, …)
int execle(const char* path, const char* arg, …, char* const envp[])
int execv(const char* path, const char* argv[])
int execvp(const char* file, const char* argv[])
int execvpe(const char* file, const char* argv[], char *const envp[])
Description :

execl(const char *path, const char *arg, …):

Page no.…...54……. Signature of the Faculty………………………...


Roll No: 160122733163 Exp. No: 4 Date: 30-08-2024

• Replaces the current process with a new process running the program specified by
path.
• Arguments are passed as individual parameters (end with NULL).
execlp(const char *file, const char *arg, …):
• Similar to execl, but file is searched in the directories listed in PATH.
• Arguments are passed as individual parameters (end with NULL).
execle(const char *path, const char *arg, …, char *const envp[]):
• Replaces the current process with a new process running the program specified by
path.
• Arguments are passed as individual parameters (end with NULL).
• You can specify a custom environment using envp.
execv(const char *path, char *const argv[]):
• Replaces the current process with a new process running the program specified by
path.
• Arguments are passed as an array (argv), ending with NULL.
execvp(const char *file, char *const argv[]):
• Similar to execv, but file is searched in the directories listed in PATH.
• Arguments are passed as an array (argv), ending with NULL.
execvpe(const char *file, char *const argv[], char *const envp[]):
• Similar to execvp, but you can specify a custom environment using envp.
• file is searched in PATH, and arguments are passed as an array (argv), ending with
NULL.
CODE:
Exec.c
#include<stdio.h>
#include<unistd.h>
int main()
{
int i;
printf("I am Demo file
called by execvp() ");
printf("\n");
return 0;
}

Execv.c
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
char
*args[]={"./exec",NULL};
execv(args[0],args);
printf("Ending-----");
return 0;

Page no.…...55……. Signature of the Faculty………………………...


Roll No: 160122733163 Exp. No: 4 Date: 30-08-2024

}
Execvp.c
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
char
*args[]={"./3_6_demo",NULL
};
execvp(args[0],args);
printf("Ending-----");
return 0;
}
Aim: Write a program to demonstrate the use of vfork()
Description:
vfork():
• Creates a new process (child) but does not fully copy the parent's address space.
Instead, the child process shares the address space with the parent until it calls exec()
or _exit().
• This makes vfork() more efficient than fork() when the child process is immediately
going to execute a new program or exit.
Child Process:
• After vfork(), the child process prints its PID and the parent PID. It then uses execlp()
to replace its process image with the ls command.
• If execlp() fails, it uses _exit() to terminate immediately. Note that _exit() is used
instead of exit() to avoid flushing standard I/O buffers, which might cause issues due
to shared memory.
Parent Process:
• The parent process waits for the child to complete using wait(NULL), ensuring it
synchronizes with the child process before continuing.
CODE:

#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include <stdlib.h>
int main()
{
printf(" Before fork");
int
fd1=open("kk.txt",O_RDONL
Y,742); //kk.txt is a shared
resource to parent and child
process

Page no.…...56……. Signature of the Faculty………………………...


Roll No: 160122733163 Exp. No: 4 Date: 30-08-2024

int a,b; // a is shared


variable to parent and child
process
printf(" Process Id of
parent %d \n",getpid());
int rf=vfork();
if(rf==0) // Upon
successful execution fork() -->
Child process will execute
below code in if block
{
printf("Inside child
process \n");
printf(" Process Id of
child pid= %d rf=%d \n
",getpid(),rf);
scanf("%d",&b);
printf(" Inside child b
value is b=%d",b);
}
else if (rf> 0) // Upon
successful execution fork()
system call --> Parent process
will exceute below code in
elseif block
{
printf("Inside parent
process");
printf(" Process Id of
parent pid=%d rf=%d
\n",getpid(),rf);
scanf("%d",&a);
printf(" Inside parent a
value is a=%d",a);
}
else{ // Upon
uncusccessful execution
fork() system call Parent
process will exceute below
code in else block
printf(" Fork
unsucessful \n Indide parent
and Process Id of parent pid=
%d rf=%d \n ",getpid(),rf);
}
printf("End of process
%d\n", getpid());
return 0;
}

Page no.…...57……. Signature of the Faculty………………………...


Roll No: 160122733163 Exp. No: 4 Date: 30-08-2024

OUTPUT:

Aim: Write a program to demonstrate Orphan process


Description:
Parent Process:
Creates a child process using fork().
Immediately prints a message and terminates.
Child Process:
Sleeps for a short period (5 seconds) to ensure the parent process has terminated.
After the sleep, it prints a message showing its PID and its new parent PID. The new parent
PID should be 1, which is the init process (or equivalent).
CODE:
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
pid_t pid;
char *message;
int n;
int exit_code;
printf("fork Program
starting\n");
pid = fork();
switch(pid)
{
case -1:
perror("fork failed");
exit(1);
case 0:
message = "This is the
child";
n = 5;
exit_code = 37;

Page no.…...58……. Signature of the Faculty………………………...


Roll No: 160122733163 Exp. No: 4 Date: 30-08-2024

break;
default:
message = "This is the
parent";
n = 3;
exit_code = 0;
break;
}
for(; n > 0; n--) {
puts(message);
sleep(1);
}
}
OUTPUT:

Aim: Write a program to demonstrate Zombie process


Description:
Parent Process:
• Creates a child process using fork().
• Sleeps for 10 seconds to allow the child process to terminate and become a zombie.
• Prints a message indicating the state of the child process (which is a zombie during
the sleep period).
Child Process:
• Prints a message and then terminates using _exit(0).
Zombie Process:
• During the sleep period, the child process will be in a zombie state because the parent
has not yet called wait() to read the exit status.
Cleanup:
• The parent process does not call wait() in this example, so the child remains a zombie
until the parent exits.
• Once the parent process exits, the init process (or equivalent) will clean up the zombie
process. You can uncomment the wait(NULL); line in the parent process to clean up
the zombie manually before the parent exits.

Page no.…...59……. Signature of the Faculty………………………...


Roll No: 160122733163 Exp. No: 4 Date: 30-08-2024

CODE:

#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
pid_t pid;
char *message;
int n;
int exit_code;
printf("fork Program
starting\n");
pid = fork();
switch(pid)
{
case -1:
perror("fork failed");
exit(1);
case 0:
message = "This is the
child";
n = 3;
exit_code = 37;
break;
default:
message = "This is the
parent";
n = 5;
exit_code = 0;
break;
}
for(; n > 0; n--) {
puts(message);
sleep(1);
}
}
Observing the Zombie Process:
To observe the zombie process:
1. Run the program and let it execute.
2. Open another terminal and use the ps -ax command to check the process status
Look for processes with the status Z (zombie).
3. Return to the original terminal and let the parent process exit. After the parent exits,
the zombie process should be cleaned up automatically.

Page no.…...60……. Signature of the Faculty………………………...


Roll No: 160122733163 Exp. No: 4 Date: 30-08-2024

OUTPUT:

Page no.…...61……. Signature of the Faculty………………………...

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy