Skip to content

aaziz9/Using-the-process-API

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 

Repository files navigation

Using-the-process-API

Practicing with fork, exec, wait.

Overview

The goal is for you to practice using the unix process API, i.e., 'fork()', 'exec()', 'wait()', and 'exit()'. Some questions do not require a direct answer. All questions ask you to write a program. To write the programs, you will simply adapt the examples shown in Chapter 5. The actual source code for Chapter 5 is provided in the book's companion website (http://pages.cs.wisc.edu/~remzi/OSTEP/).

Questions

  1. Write a program that calls fork(). Before calling fork(), have the main process access a variable (e.g., x) and set its value to something (e.g., 100). What value is the variable in the child process? What happens to the variable when both the child and parent change the value of x?
// Add your code or answer here. You can also add screenshots showing your program's execution.  
  1. Write a program that opens a file (with the open() system call) and then calls fork() to create a new process. Can both the child and parent access the file descriptor returned by open()? What happens when they are writing to the file concurrently, i.e., at the same time?
// Add your code or answer here. You can also add screenshots showing your program's execution.  
  1. Write another program using fork().The child process should print “hello”; the parent process should print “goodbye”. You should try to ensure that the child process always prints first; can you do this without calling wait() in the parent?
// Add your code or answer here. You can also add screenshots showing your program's execution.  
  1. Write a program that calls fork() and then calls some form of exec() to run the program /bin/ls. See if you can try all of the variants of exec(), including (on Linux) execl(), execle(), execlp(), execv(), execvp(), and execvpe(). Why do you think there are so many variants of the same basic call?
// Add your code or answer here. You can also add screenshots showing your program's execution.  
  1. Now write a program that uses wait() to wait for the child process to finish in the parent. What does wait() return? What happens if you use wait() in the child?
// Add your code or answer here. You can also add screenshots showing your program's execution.  
  1. Write a slight modification of the previous program, this time using waitpid() instead of wait(). When would waitpid() be useful?
// Add your code or answer here. You can also add screenshots showing your program's execution.  
  1. Write a program that creates a child process, and then in the child closes standard output (STDOUT FILENO). What happens if the child calls printf() to print some output after closing the descriptor?
// Add your code or answer here. You can also add screenshots showing your program's execution.  

About

Practicing with fork, exec, wait.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published