0% found this document useful (0 votes)
41 views6 pages

Lab#7 OS

This document contains code snippets and output from several tasks demonstrating processes and process IDs in Linux. Task 1 prints the process ID and parent process ID of the current process. Task 2 demonstrates fork() to create a child process, with each process printing its ID. Task 4 uses fork() and wait() to have the parent process wait for the child, which executes ls. Task 6 shows how the child would become a zombie process if the parent sleeps before the child exits.

Uploaded by

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

Lab#7 OS

This document contains code snippets and output from several tasks demonstrating processes and process IDs in Linux. Task 1 prints the process ID and parent process ID of the current process. Task 2 demonstrates fork() to create a child process, with each process printing its ID. Task 4 uses fork() and wait() to have the parent process wait for the child, which executes ls. Task 6 shows how the child would become a zombie process if the parent sleeps before the child exits.

Uploaded by

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

Operating Systems

Lab7
Task#1
Code:
#include<stdio.h>

#include<unistd.h>

int main()

printf("Process ID: %d\n", getpid() );

printf("Parent Process ID: %d\n", getppid() );

return 0;

Output:
Task#2
Code:
#include <stdio.h>
#include <stdlib.h>
int main() {
int pid = fork();
// child process because return value is zero
if (pid == 0) {
printf("This is the child process. My pid is %d and my parent's id is %d.\n", getpid(), getppid());
}
// Parent process because return value is non-zero
else {
printf("This is the parent process. My pid is %d and my parent's id is %d.\n", getpid(), pid);
}
return 0;
}
Output:

Task#4
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/wait.h>
int main() {
pid_t forkStatus;
forkStatus = fork();

/* Child... */
if (forkStatus == 0) {
printf("Child is running, processing.\n");
execl("/bin/ls", "ls", "-l", (char *) 0);
sleep(5);
printf("Child is done, exiting.\n");

/* Parent... */
} else if (forkStatus != -1) {
printf("Parent is waiting...\n");
wait(NULL);
printf("Parent is exiting...\n");
} else {
perror("Error while calling the fork function");
}
return 0;
}
Output:

Task#6
Code:
// Child becomes Zombie as parent is sleeping
// when child process exits.
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
// Fork returns process id
// in parent process
pid_t child_pid = fork();

// Parent process
if (child_pid > 0){
printf("Parent will sleep");
sleep(50);
}
// Child process
else
exit(0);
return 0;
}

Output:

You might also like