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

CSCI-332 Week2 2

This document discusses processes and process system calls in an operating systems class. It covers topics like process states, creating processes using fork(), executing new programs using exec(), waiting for process termination using wait(), and how orphan and zombie processes are created depending on whether wait() is called. Examples are provided to demonstrate using fork(), getpid(), wait(), exec(), and how to intentionally create orphan and zombie processes. The next class will involve a lab session where students write code and run commands on their VM/Linux system to work with processes.

Uploaded by

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

CSCI-332 Week2 2

This document discusses processes and process system calls in an operating systems class. It covers topics like process states, creating processes using fork(), executing new programs using exec(), waiting for process termination using wait(), and how orphan and zombie processes are created depending on whether wait() is called. Examples are provided to demonstrate using fork(), getpid(), wait(), exec(), and how to intentionally create orphan and zombie processes. The next class will involve a lab session where students write code and run commands on their VM/Linux system to work with processes.

Uploaded by

Dokin Nikod
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

CSCI 332

Operating Systems

Process system calls

Prof. Dimitrios Zorbas


In the previous class

Processes
– Definition
– States
– PCB, Context switch
– System calls for creation, termination etc.
Diagram of Process States
Today’s class

Week 2: Processes
– Orphan and zombie processes
– Examples with fork(), exec(), wait(), and
exit() system calls
Process Termination

The parent process may wait for termination of a child
process by using the wait()system call. The call returns
status information and the pid of the terminated process
pid = wait(&status);

If no parent is waiting (did not invoke wait()), the child
process is a zombie

If the parent terminated without invoking wait(), the
child process is an orphan
fork() in UNIX

Fork is a system call used for creating a new process
– The new process is called child process,
– Children processes run concurrently with the parent process.
– After a new child process is created, both processes will execute the next instruction following the fork()
system call.
– A child process uses the same program counter, same CPU registers, same open files that are used in the
parent process.

It takes no parameters and returns an integer value. Below are different values returned by fork().
– Negative Value: creation of a child process was unsuccessful.
– Zero: Returned to the newly created child process.
– Positive value: Returned to parent or caller. The value contains the process ID of the newly created child
process.
Examples with fork()
#include <stdio.h> #include <stdio.h>
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>

int main()
int main()
{
{
fork();
fork();
fork();
fork();
printf("Hello world!\n"); printf("Hello world!\n");
return 0; return 0;
} }
What will be the output?
Hello from Child!
#include <stdio.h>

#include <sys/types.h>
#include <unistd.h> Hello from Parent!
void forkexample(){
if (fork() == 0)
printf("Hello from Child!\n");

Hello from Parent!
else Hello from Child!
printf("Hello from Parent!\n");
} ●
Any of the two
int main(){
forkexample();
return 0;
}
getpid() and getppid()
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main( void ) {


printf( "The pid of the parent process is %d\n", getpid() );

int p = fork();

if ( p == 0 ) {
printf( "After the fork, the pid of the child is %d\n", getpid() );
} else {
printf( "After the fork, the pid of the parent is %d\n", getpid() );
}

return 0;
}
wait()
#include<stdio.h>
#include<sys/wait.h>
#include<unistd.h>

int main(){
int p = fork();
if (p == 0) {
printf("Hello from child\n");
sleep(2);
} else {
printf("Hello from parent\n");
wait(NULL);
printf("Child has terminated\n");
}

printf("Bye\n");
return 0;
}
exec() and wait()
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

int main(){
char *argv[3] = {"Command", "-l", NULL};
int status = 1;

int p = fork();

if ( p == 0 ) {
execvp( "ls", argv );
}

wait( &status );

printf( "Finished executing the parent process\n");


return 0;
}
Change the following code to create a
zombie process
#include<stdio.h>
#include<sys/wait.h>
#include<unistd.h>

int main(){
int p = fork();
if (p == 0) {
printf("Hello from child\n");
sleep(2);
} else {
printf("Hello from parent\n");
wait(NULL);
printf("Child has terminated\n");
}

printf("Bye\n");
return 0;
}
Change the following code to create an
orphan process
#include<stdio.h>
#include<sys/wait.h>
#include<unistd.h>

int main(){
int p = fork();
if (p == 0) {
printf("Hello from child\n");
sleep(2);
} else {
printf("Hello from parent\n");
wait(NULL);
printf("Child has terminated\n");
}

printf("Bye\n");
return 0;
}
Any questions?
What to read

Re-run the examples and play with fork(),
exec(), wait() etc.

Read the documentation of those system calls
Next class

Friday: Lab about processes
– Have your VM or native Linux installation ready
– You will need to run some commands and develop
some code

You might also like