OS ASSIGNMENT
D
D.1 Process creation:
man pages
fork
waitpid
It's used generally to wait until a specific process finishes (or
otherwise changes state if you're using special flags), based
on its process ID (otherwise known as a pid).
It can also be used to wait for any of a group of child
processes, either one from a specific process group or any
child of the current process.
b. Discuss the following line of code:
w = waitpid(cpid, &status, WUNTRACED | WCONTINUED)
WUNTRACED
also return if a child has stopped (but not traced
via ptrace(2)). Status for traced children which have stopped
is provided even if this option is not specified.
WCONTINUED (since Linux 2.6.10)
also return if a stopped child has been resumed by delivery
of SIGCONT.
(For Linux-only options, see below.)
If status is not NULL, wait() and waitpid() store status
information in the int to which it points. This integer can be
inspected with the following macros (which take the integer
itself as an argument, not a pointer to it, as is done in wait()
and waitpid()!):
D.3.3 FIFO:
Reference: man−pages of fifo, mkfifo, mknod
Discuss what’s the difference between a zombie process
and a orphan process?
What are the Zombie Processes?
On Unix and Linux systems,
the zombie (or defunct) processes are dead processes
that still apear in the process table, usually because of bugs
and coding errors. A zombie process remains in the operating
system and does nothing until the parent process determines
that the exit status is no longer needed.
When does a process turn into a zombie?
Normally, when a process finishes execution, it reports the
execution status to its parent process. Until the parent
process decides that the child processes exit status is not
needed anymore, the child process turns into a defunct or
zombie process. It does not use resources and it cannot be
schuduled for execution. Sometimes the parent process
keeps the child in the zombie stateto ensure that the future
children processes will not receive the same PID.
What are the Orphan Processes?
An Orphan Process is a process whose parent is dead
(terminated). A process with dead parents is adopted by the
init process.
When does a process become an orphan process?
Sometimes, when a process crashes, it leaves the children
processes alive, transforming them into orphan processes. A
user can also create a orphan process, by detaching it from
the terminal.
How to find orphaned processes:
This command will not display only the orphaned processes,
but all the processes having the PPID 1 (having the init
process as it’s parent).
difference between exit and return
return is an instruction of the language that returns from a
function call.
•exit is a system call (not a language statement) that
terminates the current process.
What is the difference between exit(0) and exit(1) in C
language?
exit(0) indicates successful program termination & it is fully
portable, While
exit(1) (usually) indicates unsucessful termination. However,
it's usage is non-portable.
Note that the C standard
defines EXIT_SUCCESS and EXIT_FAILURE to return
termination status from a C program.
0 and EXIT_SUCCESS are the values specified by the standard
to indicate successful termination, however,
only EXIT_FAILURE is the standard value for returning
unsucessful termination. 1 is used for the same in many
implementations though.
Read Beginning Linux Programming, Chapter 11, page 503 to
learn how to avoid zombies with waitpid() system call. And
correct the above program
refer Beej's Guide to Unix IPC for D section....
man fork
man exec()
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
int
main(int argc, char *argv[])
{
pid_t cpid, w;
int status;
cpid = fork();
if (cpid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (cpid == 0) { /* Code executed by child */
printf("Child PID is %ld\n", (long) getpid());
if (argc == 1)
pause(); /* Wait for signals */
_exit(atoi(argv[1]));
} else { /* Code executed by parent */
do {
w = waitpid(cpid, &status, WUNTRACED |
WCONTINUED);
if (w == -1) {
perror("waitpid");
exit(EXIT_FAILURE);
}
if (WIFEXITED(status)) {
printf("exited, status=%d\n",
WEXITSTATUS(status));
} else if (WIFSIGNALED(status)) {
printf("killed by signal %d\n",
WTERMSIG(status));
} else if (WIFSTOPPED(status)) {
printf("stopped by signal %d\n",
WSTOPSIG(status));
} else if (WIFCONTINUED(status)) {
printf("continued\n");
}
} while (!WIFEXITED(status) && !
WIFSIGNALED(status));
exit(EXIT_SUCCESS);
}
}
program 1
#include <stdlib.h>
#include <stdio.h>
int main(){
printf("Running ps with system\n");
system("ps −ax &");
printf("Done.\n");
exit(0);
}
program 2
#include <unistd.h>
#include <stdio.h>
int main(){
printf("Running ps with execlp\n");
execlp("ps", "ps", "−ax", 0);
printf("Done.\n");
exit(0);
}
program 3
#include <stdio.h>
#include <unistd.h>
int main(){
pid_t child_p;
printf("Running ps with fork\n");
child_p = fork();
execlp("ps", "ps", "−ax", 0);
return 0;
}
program 4
#include <unistd.h>
#include <stdio.h>
int main(){
pid_t pid;
printf("Running ps again with fork\n");
pid = fork();
if ( pid == 0 ) { // in the child, do exec
execlp("ps", "ps", "−ax", 0);
}
else if (pid < 0){ // failed to fork
printf("fork failed.\n");
exit(1);
}
else {// parent
wait(NULL);
}
exit(0);
}
man fifo
man mkfifo
man mknod
man pipe
man write
man read
D
D.1 Process creation:
man pages
fork
waitpid
It's used generally to wait until a specific process finishes (or
otherwise changes state if you're using special flags), based
on its process ID (otherwise known as a pid).
It can also be used to wait for any of a group of child
processes, either one from a specific process group or any
child of the current process.
b. Discuss the following line of code:
w = waitpid(cpid, &status, WUNTRACED | WCONTINUED)
WUNTRACED
also return if a child has stopped (but not traced
via ptrace(2)). Status for traced children which have stopped
is provided even if this option is not specified.
WCONTINUED (since Linux 2.6.10)
also return if a stopped child has been resumed by delivery
of SIGCONT.
(For Linux-only options, see below.)
If status is not NULL, wait() and waitpid() store status
information in the int to which it points. This integer can be
inspected with the following macros (which take the integer
itself as an argument, not a pointer to it, as is done in wait()
and waitpid()!):
D.3.3 FIFO:
Reference: man−pages of fifo, mkfifo, mknod
Discuss what’s the difference between a zombie process
and a orphan process?
What are the Zombie Processes?
On Unix and Linux systems,
the zombie (or defunct) processes are dead processes
that still apear in the process table, usually because of bugs
and coding errors. A zombie process remains in the operating
system and does nothing until the parent process determines
that the exit status is no longer needed.
When does a process turn into a zombie?
Normally, when a process finishes execution, it reports the
execution status to its parent process. Until the parent
process decides that the child processes exit status is not
needed anymore, the child process turns into a defunct or
zombie process. It does not use resources and it cannot be
schuduled for execution. Sometimes the parent process
keeps the child in the zombie stateto ensure that the future
children processes will not receive the same PID.
What are the Orphan Processes?
An Orphan Process is a process whose parent is dead
(terminated). A process with dead parents is adopted by the
init process.
When does a process become an orphan process?
Sometimes, when a process crashes, it leaves the children
processes alive, transforming them into orphan processes. A
user can also create a orphan process, by detaching it from
the terminal.
How to find orphaned processes:
This command will not display only the orphaned processes,
but all the processes having the PPID 1 (having the init
process as it’s parent).
difference between exit and return
return is an instruction of the language that returns from a
function call.
•exit is a system call (not a language statement) that
terminates the current process.
What is the difference between exit(0) and exit(1) in C
language?
exit(0) indicates successful program termination & it is fully
portable, While
exit(1) (usually) indicates unsucessful termination. However,
it's usage is non-portable.
Note that the C standard
defines EXIT_SUCCESS and EXIT_FAILURE to return
termination status from a C program.
0 and EXIT_SUCCESS are the values specified by the standard
to indicate successful termination, however,
only EXIT_FAILURE is the standard value for returning
unsucessful termination. 1 is used for the same in many
implementations though.
Read Beginning Linux Programming, Chapter 11, page 503 to
learn how to avoid zombies with waitpid() system call. And
correct the above program
refer Beej's Guide to Unix IPC for D section....
man fork
man exec()
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
int
main(int argc, char *argv[])
{
pid_t cpid, w;
int status;
cpid = fork();
if (cpid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (cpid == 0) { /* Code executed by child */
printf("Child PID is %ld\n", (long) getpid());
if (argc == 1)
pause(); /* Wait for signals */
_exit(atoi(argv[1]));
} else { /* Code executed by parent */
do {
w = waitpid(cpid, &status, WUNTRACED |
WCONTINUED);
if (w == -1) {
perror("waitpid");
exit(EXIT_FAILURE);
}
if (WIFEXITED(status)) {
printf("exited, status=%d\n",
WEXITSTATUS(status));
} else if (WIFSIGNALED(status)) {
printf("killed by signal %d\n",
WTERMSIG(status));
} else if (WIFSTOPPED(status)) {
printf("stopped by signal %d\n",
WSTOPSIG(status));
} else if (WIFCONTINUED(status)) {
printf("continued\n");
}
} while (!WIFEXITED(status) && !
WIFSIGNALED(status));
exit(EXIT_SUCCESS);
}
}
program 1
#include <stdlib.h>
#include <stdio.h>
int main(){
printf("Running ps with system\n");
system("ps −ax &");
printf("Done.\n");
exit(0);
}
program 2
#include <unistd.h>
#include <stdio.h>
int main(){
printf("Running ps with execlp\n");
execlp("ps", "ps", "−ax", 0);
printf("Done.\n");
exit(0);
}
program 3
#include <stdio.h>
#include <unistd.h>
int main(){
pid_t child_p;
printf("Running ps with fork\n");
child_p = fork();
execlp("ps", "ps", "−ax", 0);
return 0;
}
program 4
#include <unistd.h>
#include <stdio.h>
int main(){
pid_t pid;
printf("Running ps again with fork\n");
pid = fork();
if ( pid == 0 ) { // in the child, do exec
execlp("ps", "ps", "−ax", 0);
}
else if (pid < 0){ // failed to fork
printf("fork failed.\n");
exit(1);
}
else {// parent
wait(NULL);
}
exit(0);
}
man fifo
man mkfifo
man mknod
man pipe
man write
man read
No comments:
Post a Comment