CS 410 Operating Systems
Lab Assignment #2
Using “fork()” (Creating a Process)
In multitasking OS, processes (running programs) need a way to create new processes to run
other programs. In Unix/Linux OS fork is an operation whereby a process creates a copy of
itself. It is usually a system call, implemented in the kernel. Fork is the primary method of
process creation on Unix-like operating systems.
For a process to start the execution of a different program, it first forks to create a copy of itself.
Then, the copy, called the “child process”, calls the exec system call to overlay itself with the
other program: it ceases execution of its former program in favor of the other.
The fork operation creates a separate address space for the child. The child process has an
exact copy of all the memory segments of the parent process.
The fork function is the primitive for creating a process. It is declared in the header file unistd.h
Function: pid_t fork (void)
http://www.gnu.org/software/libc/manual/html_node/Creating-a-Process.html
A program is an executable file residing in a disk file. An executing instance of a program is
called a process. In Unix/Linux a program is read into memory and executed by the kernel as a
result of one of the six exec functions.
Every Unix/Linux process is guaranteed to have a unique numeric identifier called the process
ID. The process ID (short: PID) is always a nonnegative integer.
Under Unix, the only way (except for some very special processes) to create a new processes
is when an existing process calls the fork function. You will find details about fork and wait in the
manual pages (Unix/Linux command: “man fork”).
Assignment:
Write a simple program that uses the “fork” function. The new process created by fork is called
the “child” process. The function is called once but returns twice. Both the child and the parent
continue executing with the instruction that follows the call to fork. Here is a typical example:
/* parent creates a process */
pid = fork();
/* the return value is a process ID, that ALWAYS needs to be tested */
switch (pid) {
case -1:
/* error: fork was unsuccessful */
break;
case 0:
/* this is the child process */
http://www.gnu.org/software/libc/manual/html_node/Creating-a-Process.html
/* no process ID */
/* … do something … */
break;
default:
/* this is the parent process */
/* pid=process ID of the child */
/* … */
}
/* both processes continue here */
You can list processes by using the ps command or the top command (to see the manual pages
use “man ps” or “man top”). Under Unix/Linux a process that has terminated, but whose
parent has not yet waited for it is called a zombie.
Adjust the sleep calls for parent and child and use the command ps to answer these questions
(add them as comments at the end of your program):
1. What status is being printed when the child terminates before the parent (in another terminal
use: ps ajxf | grep yourProgramName) ?
Note: run the command “man ps” for more options.
2. What is the parent process ID (PPID) of a child process whose parent terminates before
the child?
Note: to get the proper results from the “ps” command you need to use two terminals, in one
you will run the program, and in another terminal you will execute the “ps” command. This has to
be almost at the same time, the parent or the child (depending on the case) should still be
running when you execute the “ps” command, otherwise you won’t get any information from
those processes.
To make the parent wait for the child, replace the sleep() by
pid = waitpid(pid, &status, 0) in the code for the parent and read about the function
by executing “man waitpid”. The status is an integer.
Name your programs: YourLastName_YourFirstName_Lab02_1.c
and YourLastName_YourFirstName_Lab02_2.c
You must include a comment on the top of your programs like the following
/*
CS 410 Operating Systems
Lastname Firstname
Lab 02
*/
Submission
The submission of your program and report will be through the Western Online. The report
should include screenshots and a conclusion (properly labeled as that) assessing the
importance of the fork function and process replication. Include a header in your report and
name it: YourLastName_YourFirstName_Lab01
Check the Rubrics for more details about this assignment.
——///
CS 410 Operating Systems
Lab Assignment #2
Using “fork()” (Creating a Process)
In multitasking OS, processes (running programs) need a way to create new processes to run
other programs. In Unix/Linux OS fork is an operation whereby a process creates a copy of
itself. It is usually a system call, implemented in the kernel. Fork is the primary method of
process creation on Unix-like operating systems.
For a process to start the execution of a different program, it first forks to create a copy of itself.
Then, the copy, called the “child process”, calls the exec system call to overlay itself with the
other program: it ceases execution of its former program in favor of the other.
The fork operation creates a separate address space for the child. The child process has an
exact copy of all the memory segments of the parent process.
The fork function is the primitive for creating a process. It is declared in the header file unistd.h
Function: pid_t fork (void)
http://www.gnu.org/software/libc/manual/html_node/Creating-a-Process.html
A program is an executable file residing in a disk file. An executing instance of a program is
called a process. In Unix/Linux a program is read into memory and executed by the kernel as a
result of one of the six exec functions.
Every Unix/Linux process is guaranteed to have a unique numeric identifier called the process
ID. The process ID (short: PID) is always a nonnegative integer.
Under Unix, the only way (except for some very special processes) to create a new processes
is when an existing process calls the fork function. You will find details about fork and wait in the
manual pages (Unix/Linux command: “man fork”).
Assignment:
Write a simple program that uses the “fork” function. The new process created by fork is called
the “child” process. The function is called once but returns twice. Both the child and the parent
continue executing with the instruction that follows the call to fork. Here is a typical example:
/* parent creates a process */
pid = fork();
/* the return value is a process ID, that ALWAYS needs to be tested */
switch (pid) {
case -1:
/* error: fork was unsuccessful */
break;
case 0:
/* this is the child process */
http://www.gnu.org/software/libc/manual/html_node/Creating-a-Process.html
/* no process ID */
/* … do something … */
break;
default:
/* this is the parent process */
/* pid=process ID of the child */
/* … */
}
/* both processes continue here */
You can list processes by using the ps command or the top command (to see the manual pages
use “man ps” or “man top”). Under Unix/Linux a process that has terminated, but whose
parent has not yet waited for it is called a zombie.
Adjust the sleep calls for parent and child and use the command ps to answer these questions
(add them as comments at the end of your program):
1. What status is being printed when the child terminates before the parent (in another terminal
use: ps ajxf | grep yourProgramName) ?
Note: run the command “man ps” for more options.
2. What is the parent process ID (PPID) of a child process whose parent terminates before
the child?
Note: to get the proper results from the “ps” command you need to use two terminals, in one
you will run the program, and in another terminal you will execute the “ps” command. This has to
be almost at the same time, the parent or the child (depending on the case) should still be
running when you execute the “ps” command, otherwise you won’t get any information from
those processes.
To make the parent wait for the child, replace the sleep() by
pid = waitpid(pid, &status, 0) in the code for the parent and read about the function
by executing “man waitpid”. The status is an integer.
Name your programs: YourLastName_YourFirstName_Lab02_1.c
and YourLastName_YourFirstName_Lab02_2.c
You must include a comment on the top of your programs like the following
/*
CS 410 Operating Systems
Lastname Firstname
Lab 02
*/
Submission
The submission of your program and report will be through the Western Online. The report
should include screenshots and a conclusion (properly labeled as that) assessing the
importance of the fork function and process replication. Include a header in your report and
name it: YourLastName_YourFirstName_Lab01
Check the Rubrics for more details about this assignment.
——///
CS 410 Operating Systems
Lab Assignment #2
Using “fork()” (Creating a Process)
In multitasking OS, processes (running programs) need a way to create new processes to run
other programs. In Unix/Linux OS fork is an operation whereby a process creates a copy of
itself. It is usually a system call, implemented in the kernel. Fork is the primary method of
process creation on Unix-like operating systems.
For a process to start the execution of a different program, it first forks to create a copy of itself.
Then, the copy, called the “child process”, calls the exec system call to overlay itself with the
other program: it ceases execution of its former program in favor of the other.
The fork operation creates a separate address space for the child. The child process has an
exact copy of all the memory segments of the parent process.
The fork function is the primitive for creating a process. It is declared in the header file unistd.h
Function: pid_t fork (void)
http://www.gnu.org/software/libc/manual/html_node/Creating-a-Process.html
A program is an executable file residing in a disk file. An executing instance of a program is
called a process. In Unix/Linux a program is read into memory and executed by the kernel as a
result of one of the six exec functions.
Every Unix/Linux process is guaranteed to have a unique numeric identifier called the process
ID. The process ID (short: PID) is always a nonnegative integer.
Under Unix, the only way (except for some very special processes) to create a new processes
is when an existing process calls the fork function. You will find details about fork and wait in the
manual pages (Unix/Linux command: “man fork”).
Assignment:
Write a simple program that uses the “fork” function. The new process created by fork is called
the “child” process. The function is called once but returns twice. Both the child and the parent
continue executing with the instruction that follows the call to fork. Here is a typical example:
/* parent creates a process */
pid = fork();
/* the return value is a process ID, that ALWAYS needs to be tested */
switch (pid) {
case -1:
/* error: fork was unsuccessful */
break;
case 0:
/* this is the child process */
http://www.gnu.org/software/libc/manual/html_node/Creating-a-Process.html
/* no process ID */
/* … do something … */
break;
default:
/* this is the parent process */
/* pid=process ID of the child */
/* … */
}
/* both processes continue here */
You can list processes by using the ps command or the top command (to see the manual pages
use “man ps” or “man top”). Under Unix/Linux a process that has terminated, but whose
parent has not yet waited for it is called a zombie.
Adjust the sleep calls for parent and child and use the command ps to answer these questions
(add them as comments at the end of your program):
1. What status is being printed when the child terminates before the parent (in another terminal
use: ps ajxf | grep yourProgramName) ?
Note: run the command “man ps” for more options.
2. What is the parent process ID (PPID) of a child process whose parent terminates before
the child?
Note: to get the proper results from the “ps” command you need to use two terminals, in one
you will run the program, and in another terminal you will execute the “ps” command. This has to
be almost at the same time, the parent or the child (depending on the case) should still be
running when you execute the “ps” command, otherwise you won’t get any information from
those processes.
To make the parent wait for the child, replace the sleep() by
pid = waitpid(pid, &status, 0) in the code for the parent and read about the function
by executing “man waitpid”. The status is an integer.
Name your programs: YourLastName_YourFirstName_Lab02_1.c
and YourLastName_YourFirstName_Lab02_2.c
You must include a comment on the top of your programs like the following
/*
CS 410 Operating Systems
Lastname Firstname
Lab 02
*/
Submission
The submission of your program and report will be through the Western Online. The report
should include screenshots and a conclusion (properly labeled as that) assessing the
importance of the fork function and process replication. Include a header in your report and
name it: YourLastName_YourFirstName_Lab01
Check the Rubrics for more details about this assignment.
——///
CS 410 Operating Systems
Lab Assignment #2
Using “fork()” (Creating a Process)
In multitasking OS, processes (running programs) need a way to create new processes to run
other programs. In Unix/Linux OS fork is an operation whereby a process creates a copy of
itself. It is usually a system call, implemented in the kernel. Fork is the primary method of
process creation on Unix-like operating systems.
For a process to start the execution of a different program, it first forks to create a copy of itself.
Then, the copy, called the “child process”, calls the exec system call to overlay itself with the
other program: it ceases execution of its former program in favor of the other.
The fork operation creates a separate address space for the child. The child process has an
exact copy of all the memory segments of the parent process.
The fork function is the primitive for creating a process. It is declared in the header file unistd.h
Function: pid_t fork (void)
http://www.gnu.org/software/libc/manual/html_node/Creating-a-Process.html
A program is an executable file residing in a disk file. An executing instance of a program is
called a process. In Unix/Linux a program is read into memory and executed by the kernel as a
result of one of the six exec functions.
Every Unix/Linux process is guaranteed to have a unique numeric identifier called the process
ID. The process ID (short: PID) is always a nonnegative integer.
Under Unix, the only way (except for some very special processes) to create a new processes
is when an existing process calls the fork function. You will find details about fork and wait in the
manual pages (Unix/Linux command: “man fork”).
Assignment:
Write a simple program that uses the “fork” function. The new process created by fork is called
the “child” process. The function is called once but returns twice. Both the child and the parent
continue executing with the instruction that follows the call to fork. Here is a typical example:
/* parent creates a process */
pid = fork();
/* the return value is a process ID, that ALWAYS needs to be tested */
switch (pid) {
case -1:
/* error: fork was unsuccessful */
break;
case 0:
/* this is the child process */
http://www.gnu.org/software/libc/manual/html_node/Creating-a-Process.html
/* no process ID */
/* … do something … */
break;
default:
/* this is the parent process */
/* pid=process ID of the child */
/* … */
}
/* both processes continue here */
You can list processes by using the ps command or the top command (to see the manual pages
use “man ps” or “man top”). Under Unix/Linux a process that has terminated, but whose
parent has not yet waited for it is called a zombie.
Adjust the sleep calls for parent and child and use the command ps to answer these questions
(add them as comments at the end of your program):
1. What status is being printed when the child terminates before the parent (in another terminal
use: ps ajxf | grep yourProgramName) ?
Note: run the command “man ps” for more options.
2. What is the parent process ID (PPID) of a child process whose parent terminates before
the child?
Note: to get the proper results from the “ps” command you need to use two terminals, in one
you will run the program, and in another terminal you will execute the “ps” command. This has to
be almost at the same time, the parent or the child (depending on the case) should still be
running when you execute the “ps” command, otherwise you won’t get any information from
those processes.
To make the parent wait for the child, replace the sleep() by
pid = waitpid(pid, &status, 0) in the code for the parent and read about the function
by executing “man waitpid”. The status is an integer.
Name your programs: YourLastName_YourFirstName_Lab02_1.c
and YourLastName_YourFirstName_Lab02_2.c
You must include a comment on the top of your programs like the following
/*
CS 410 Operating Systems
Lastname Firstname
Lab 02
*/
Submission
The submission of your program and report will be through the Western Online. The report
should include screenshots and a conclusion (properly labeled as that) assessing the
importance of the fork function and process replication. Include a header in your report and
name it: YourLastName_YourFirstName_Lab01
Check the Rubrics for more details about this assignment.
——///
CS 410 Operating Systems
Lab Assignment #2
Using “fork()” (Creating a Process)
In multitasking OS, processes (running programs) need a way to create new processes to run
other programs. In Unix/Linux OS fork is an operation whereby a process creates a copy of
itself. It is usually a system call, implemented in the kernel. Fork is the primary method of
process creation on Unix-like operating systems.
For a process to start the execution of a different program, it first forks to create a copy of itself.
Then, the copy, called the “child process”, calls the exec system call to overlay itself with the
other program: it ceases execution of its former program in favor of the other.
The fork operation creates a separate address space for the child. The child process has an
exact copy of all the memory segments of the parent process.
The fork function is the primitive for creating a process. It is declared in the header file unistd.h
Function: pid_t fork (void)
http://www.gnu.org/software/libc/manual/html_node/Creating-a-Process.html
A program is an executable file residing in a disk file. An executing instance of a program is
called a process. In Unix/Linux a program is read into memory and executed by the kernel as a
result of one of the six exec functions.
Every Unix/Linux process is guaranteed to have a unique numeric identifier called the process
ID. The process ID (short: PID) is always a nonnegative integer.
Under Unix, the only way (except for some very special processes) to create a new processes
is when an existing process calls the fork function. You will find details about fork and wait in the
manual pages (Unix/Linux command: “man fork”).
Assignment:
Write a simple program that uses the “fork” function. The new process created by fork is called
the “child” process. The function is called once but returns twice. Both the child and the parent
continue executing with the instruction that follows the call to fork. Here is a typical example:
/* parent creates a process */
pid = fork();
/* the return value is a process ID, that ALWAYS needs to be tested */
switch (pid) {
case -1:
/* error: fork was unsuccessful */
break;
case 0:
/* this is the child process */
http://www.gnu.org/software/libc/manual/html_node/Creating-a-Process.html
/* no process ID */
/* … do something … */
break;
default:
/* this is the parent process */
/* pid=process ID of the child */
/* … */
}
/* both processes continue here */
You can list processes by using the ps command or the top command (to see the manual pages
use “man ps” or “man top”). Under Unix/Linux a process that has terminated, but whose
parent has not yet waited for it is called a zombie.
Adjust the sleep calls for parent and child and use the command ps to answer these questions
(add them as comments at the end of your program):
1. What status is being printed when the child terminates before the parent (in another terminal
use: ps ajxf | grep yourProgramName) ?
Note: run the command “man ps” for more options.
2. What is the parent process ID (PPID) of a child process whose parent terminates before
the child?
Note: to get the proper results from the “ps” command you need to use two terminals, in one
you will run the program, and in another terminal you will execute the “ps” command. This has to
be almost at the same time, the parent or the child (depending on the case) should still be
running when you execute the “ps” command, otherwise you won’t get any information from
those processes.
To make the parent wait for the child, replace the sleep() by
pid = waitpid(pid, &status, 0) in the code for the parent and read about the function
by executing “man waitpid”. The status is an integer.
Name your programs: YourLastName_YourFirstName_Lab02_1.c
and YourLastName_YourFirstName_Lab02_2.c
You must include a comment on the top of your programs like the following
/*
CS 410 Operating Systems
Lastname Firstname
Lab 02
*/
Submission
The submission of your program and report will be through the Western Online. The report
should include screenshots and a conclusion (properly labeled as that) assessing the
importance of the fork function and process replication. Include a header in your report and
name it: YourLastName_YourFirstName_Lab01
Check the Rubrics for more details about this assignment.
——///