U of I CS 241 - Systems Programming (25 pages)
Previewing pages 1, 2, 24, 25 of 25 page document View the full content.Systems Programming
Previewing pages 1, 2, 24, 25 of actual document.
View the full content.View Full Document
Systems Programming
0
0
128 views
- Pages:
- 25
- School:
- University of Illinois
- Course:
- Cs 241 - Intermediate Programming in C++
Intermediate Programming in C++ Documents
-
21 pages
-
25 pages
-
8 pages
-
24 pages
-
28 pages
-
System Programming Review Session
12 pages
-
39 pages
-
37 pages
-
30 pages
-
System Programming File System Implementation (V)
22 pages
-
39 pages
-
36 pages
-
52 pages
-
Interprocess Communication (VII)
31 pages
-
62 pages
-
Introduction to Memory Management
43 pages
-
23 pages
-
27 pages
-
17 pages
-
31 pages
-
42 pages
-
System Programming CPU Scheduling (2)
25 pages
-
48 pages
-
System Programming Protection Mechanisms
15 pages
-
16 pages
-
7 pages
-
Access Control, Special Files, etc
12 pages
-
69 pages
-
21 pages
-
37 pages
-
43 pages
-
23 pages
-
36 pages
-
42 pages
-
23 pages
-
94 pages
-
23 pages
-
33 pages
-
47 pages
-
50 pages
-
31 pages
-
28 pages
-
14 pages
-
33 pages
-
9 pages
-
System Programming File System I
29 pages
-
12 pages
-
26 pages
-
23 pages
-
47 pages
-
28 pages
-
37 pages
-
24 pages
-
Traveling into the Future via the Internet
32 pages
-
36 pages
-
30 pages
-
Synchronization Primitive - Semaphores
21 pages
-
32 pages
-
Operating Systems Deadlock Issues
19 pages
-
55 pages
-
35 pages
-
27 pages
-
Programming Languages and Compilers
32 pages
-
24 pages
-
Scheduling Policies and Introduction to Signals
30 pages
-
25 pages
-
Introduction to Communication (I)
20 pages
-
12 pages
-
27 pages
-
16 pages
-
27 pages
-
40 pages
-
20 pages
-
22 pages
-
26 pages
-
34 pages
-
System Programming Virtual Memory
16 pages
-
45 pages
-
34 pages
-
14 pages
-
29 pages
-
22 pages
-
33 pages
-
Synchronization Primitive - Semaphores
21 pages
-
28 pages
-
24 pages
-
27 pages
-
HW1 and Synchronization & Queuing
15 pages
-
16 pages
-
25 pages
-
19 pages
-
23 pages
-
85 pages
-
22 pages
-
15 pages
-
35 pages
-
12 pages
-
47 pages
-
Processes Wait, Exec. and Threads
32 pages
-
22 pages
-
21 pages
-
Access Control, Special Files, etc
23 pages
-
15 pages
-
7 pages
-
7 pages
-
25 pages
-
9 pages
-
13 pages
-
17 pages
-
27 pages
-
25 pages
-
28 pages
-
23 pages
-
17 pages
-
57 pages
-
45 pages
-
43 pages
-
16 pages
-
16 pages
-
30 pages
-
36 pages
-
System Programming Virtual Memory
16 pages
-
System Programming Memory Management (III)
14 pages
-
45 pages
-
55 pages
-
19 pages
-
System Programming Process Synchronization
28 pages
-
23 pages
-
Conditional Variables and Mutex
30 pages
-
26 pages
-
29 pages
-
50 pages
-
37 pages
-
System Programming Memory Management
26 pages
-
System Programming File System IV
18 pages
-
30 pages
-
Shared Memory and Message Queues (VI)
26 pages
-
12 pages
-
27 pages
-
25 pages
-
Midterm Review - System Programming
24 pages
-
37 pages
-
System Programming Deadlock Issues
20 pages
-
36 pages
-
15 pages
-
System Programming UDP Communication
22 pages
-
System Programming File System III
26 pages
-
28 pages
-
27 pages
-
25 pages
-
28 pages
-
33 pages
-
27 pages
-
18 pages
-
28 pages
-
42 pages
-
37 pages
-
15 pages
-
37 pages
-
4 pages
-
32 pages
-
22 pages
-
37 pages
-
24 pages
-
32 pages
-
8 pages
-
Homework 2 – System Programming
2 pages
-
System Programming Timers & Signals
23 pages
-
25 pages
-
38 pages
-
46 pages
-
System Programming File System I
29 pages
-
6 pages
-
24 pages
-
Classic Synchronization Problems
18 pages
-
12 pages
-
37 pages
-
31 pages
-
6 pages
-
25 pages
-
34 pages
-
Final Examination - System Programming
13 pages
-
Protocol Architecture The Common Language
28 pages
-
30 pages
-
14 pages
-
28 pages
-
20 pages
-
13 pages
-
Operating Systems Deadlock Issues
19 pages
-
Introduction to Synchronization
37 pages
-
26 pages
-
System Programming Memory Management
17 pages
-
22 pages
-
System Programming UDP Communication (IV)
22 pages
-
16 pages
-
System Programming File System III
26 pages
-
28 pages
-
Classic Synchronization Problems
18 pages
Sign up for free to view:
- This document and 3 million+ documents and flashcards
- High quality study guides, lecture notes, practice exams
- Course Packets handpicked by editors offering a comprehensive review of your courses
- Better Grades Guaranteed
CS241 Systems Programming Discussion Section Week 2 Slides by Stephen Kloder Outline Processes fork wait exec Processes A process is an instance of a running program A process contains Instructions i e the program Resources variables buffers links etc State identity ready running locked etc Processes can create other processes Process Creation with fork fork creates a new process env stack free heap static code Parent fork creates env stack free heap static code Child The new child process is identical to the old parent process except Differences between parent and child Process ID getpid Parent ID getppid Return value of fork In parent fork returns child pid In child fork returns 0 fork Example 1 What does this do fprintf stdout d n fork Try it fork example 2 include stdio h include sys types h include unistd h int main int argc char argv pid t child pid fork if child pid 0 error code perror Fork Failed return 1 fprintf stdout I am process d n getpid if child pid 0 child code fprintf stdout I m the child process n else parent code fprintf stdout I m the parent of child process d n child pid return 0 Example 2 cont d This exits too quickly let s slow it down if child pid 0 child code sleep 15 fprintf stdout I m the child process n else parent code sleep 20 fprintf stdout I m the parent of child process d n child pid Example 2 cont d In a second window run ps a and look for the pids from the program output Periodically run ps a again as the program in the first window executes What happens when the program runs What happens when the child finishes What happens when the parent finishes What happens when you switch the parent and child sleep statements Orphans and Zombies When a process finishes it becomes a zombie until its parent cleans up after it If its parent finishes first the process becomes an orphan and the init process id 1 adopts it How can a parent know when its children are done Solution wait wait allows a parent to wait for its child process and save its return value pid wait status options pid waitpid pid status options wait waits for any child waitpid waits for a specific child wait cont d wait blocks until child finishes wait does not block if the option WNOHANG is included When would we want to use this The child s return value is stored in status wait macros WIFEXITED status is true iff the process terminated normally WEXITSTATUS status gives the last 8 bits of the process s return value assuming normal exit Example 3 wait include sys wait h add this to parent code if waitpid child pid result 0 1 perror Wait failed return 1 if WIFEXITED result fprintf stdout child d returned d n child pid WEXITSTATUS result Problem 4 Recall Example 2 How can we use wait to clean up the zombie process exec exec exec replaces the current process image code variables etc with that of a new program Before After env stack free heap static code env exec New Program The program may choose to change the environment exec variations There are 6 different ways of calling exec Which one to use depends on three conditions 1 How arguments are passed 2 How the path is specified 3 Whether a new environment is used exec variations passing parameters exec can have parameters passed to it two different ways List of parameters execl usr bin ls ls l NULL Argument Vector like argv execv argv 1 argv 1 Q When would you use execl When would you use execv exec variations command path Adding a p to an exec call tells the system to look for the command in the environment s path Compare execl usr bin ls ls l NULL execlp ls ls l NULL The difference is similar for execv and execvp exec variations cont d By default the new program inherits the old program s environment Adding an e to the exec call allows the new program to run with a new environment environ execve and execle allow the user to specify the environment The others inherit the old environment fork exec If exec succeeds it does not return as it overwrites the program No checking return values executing multiple commands monitoring results etc Solution fork a new process and have the child run exec Example 5 exec include unistd h include stdio h int main int argc char argv child pid fork if child pid 0 perror Fork failed return 1 else if child pid 0 if execvp argv 1 argv 1 0 fprintf stderr Failed to execute s n argv 1 perror child exec return 1 What belongs here return 0 Problem 6 p6 exec c Compile and run this program It seems to use fork and exec to run a list of argument free commands ls cal etc from a file commands What s wrong with this program What happens when you run it How can you fix this
View Full Document