New User Special Price Expires in

Let's log you in.

Sign in with Facebook


Don't have a StudySoup account? Create one here!


Create a StudySoup account

Be part of our community, it's free to join!

Sign up with Facebook


Create your account
By creating an account you agree to StudySoup's terms and conditions and privacy policy

Already have a StudySoup account? Login here


by: Orrin Rutherford


Orrin Rutherford
GPA 3.91


Almost Ready


These notes were just uploaded, and will be ready to view shortly.

Purchase these notes here, or revisit this page.

Either way, we'll remind you when they're ready :)

Preview These Notes for FREE

Get a free preview of these Notes, just enter your email below.

Unlock Preview
Unlock Preview

Preview these materials now for free

Why put in your email? Get access to more of this material and other relevant free materials for your school

View Preview

About this Document

Class Notes
25 ?




Popular in Course

Popular in ComputerScienence

This 34 page Class Notes was uploaded by Orrin Rutherford on Wednesday September 2, 2015. The Class Notes belongs to CS 201 at Portland State University taught by Staff in Fall. Since its upload, it has received 15 views. For similar materials see /class/168300/cs-201-portland-state-university in ComputerScienence at Portland State University.

Similar to CS 201 at PSU

Popular in ComputerScienence




Report this Material


What is Karma?


Karma is the currency of StudySoup.

You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!

Date Created: 09/02/15
CS 201 Processes Gerson Robboy Portland State University Review Definition A process is an instance of a running program I One of the most fundamental concepts in computer science I Not the same as program or processor A program is a set of instructions and initialized data in a file usually found on a disk A process is an instance of that program while it is running along with the state of all the CPU registers and the values of data in memory A single program can correspond to many processes for example several users can be running a shell 2 15213 F O2 Processes The operating system provides each process with a virtual machine If a process were a thing that can have a point of view it would see itself having exclusive use of the computer I Running continuously on the CPU I In possession of the entire memory space CPU registers and lO devices I No other processes are visible I If it checked the wall time often the process might notice gaps in time it can not account for 3 15213 F 02 Logical Control Flows Each process has its own logical control flow Process A Process B Process C Time 4 15213 F 02 Context Switching Processes are managed by a shared chunk of OS code called the kernel I The kernel is not a separate process but rather runs as part of some user process Control flow passes from one process to another via a context switch Process A usercode kernel code context sw1tch usercode kernel code context switch usercode 15213 F O2 Private Address Spaces Each process has its own private address space Oxffffffff kernel virtual memory memory code data heap stack invisible to OXCOOOOOOO User COde user stack created at runtime A esp stack pomter memory mapped region for shared libraries 0x40000000 T k brk runtime heap managed by malloc readwrite segment 39data 39bSS loaded from the readonly segment executable file OX08O48OOO Init text rodata O unused 6 15213 F 02 How do processes get created The fork system call creates a new process Every process is created by another process I With one exception the very first process fork creates a duplicate of the process that called it 15213 F O2 fork Creating new processes int forkvoid I creates a new process child process that is identical to the calling process parent process I Fork is called once but returns in two separate processes I The processes are identical except for one detail 0 fork returns 0 to the child process 0 fork returns the child s pid to the parent process 8 15213 F 02 int In this code example what will you see on the screen 15213 F O2 Fork Key Points I Parent and child both run the same code 0 Distinguish parent from child by return value from fork I Both processes after fork have identical state 0 Including shared open file descriptors 0 Relative ordering of their print statements undefined o The two processes will go their separate ways without synchronizing I This is important Separate memory spaces 10 15213 F 02 Fork Example 1 What does this program do void fork1 int x 1 pidt pid fork if pid 0 printfquotChild has x dnquot x else printfquotParent has x dnquot x printfquotBye from process d with x dnquot getpid x 11 15213 F O2 Fork Example Both parent and child can continue forking quot ltf 001altti kza 03 9 P3 Bye 3 3 quot i V quot i Q 99 W 3 A y P2 Bye Bye P0 12 15213 F 02 Exercise What does this program print void doit fork fork printf hellonquot return int main doit printf hellonquot exit0 13 15213 F O2 Exercise What does this program print static int j 0 dqchildint i int pid if ilt 2 pid fork ifpid 1 exit0 else ifpid 0 dqchildi1 else j printfquotThis is process d jdnquot i j else j printfquotThis is process d jdnquot i j main dqchild0 14 15213 F O2 OK so now we know how to create processes Doesn t a computer do something besides run duplicate copies of what s already running How 15 15213 F O2 exec Running new programs A family of related functions execv execp execl int execlchar path char argO char argl m 0 I loads and runs executable at path with args argO argl o path is the complete path of an executable o argO becomes the name of the process 0 real arguments to the executable start with argl etc 0 list of args is terminated by a char 0 argument Here s what they all do I Overwrite the calling process with a new program 0 Does not create a new process 0 Runs a new program I returns 1 if error otherwise doesn t return 0 Why doesn t it return 16 15213 F 02 Example A program that creates a child process the child executes usrbin s and then the parent prints done main if fork 0 execlquotusrbinlsquot ls 0 wait0 This is the parent printf donen exit0 15213 F O2 17 void exitint status I exits a process 0 Normally return with status 0 I atexit registers functions to be executed upon exit 18 15213 F 02 wait Synchronizing with children int waitint childstatus I suspends current process until one of its children terminates I return value is the pid of the child process that terminated I If the child has already terminated then wait returns its pid immediately I If childstatus NULL then the object it points to will be set to a status indicating why the child process terminated 19 15213 F 02 wait Synchronizing with children void fork9 20 int childstatus if fork 0 printfquotHC hello from childnquot else printfquotHP hello from parentnquot waitampchildstatus printfquotCT child has terminatednquot printfquotByenquot HC Bye exit A HP CT Bye 15213 F 02 wait reaping children int waitint childstatus o If the child has already terminated then wait returns its pid immediately 0 What if many children have terminated I Will wait reliably return pids of all terminated children I Is it possible to lose some 0 Terminated child processes turn into zombies I Wait reaps the zombies 21 15213 F 02 Still more on wait What wait is really waiting for is a SIGCHLD signal Other signals can also cause wait to return When wait returns check to see if it really returns the pid of a child process I Otherwise it could have been some other signal 22 15213 F 02 Multitasking The System Runs Many Processes Concurrently State consists of memory image register values I general registers I system registers include program counter pointer to page tables The kernel continually switches from one process to another I Sometimes a process blocks waiting for lO I Sometimes the timer preempts a process To us observing from outside the computer it appears that all processes are running concurrently 23 15213 F 02 Unix Startup Step 1 1 Pushing reset button loads the PC with the address of a small bootstrap program 2 Bootstrap program loads the operating system kernel from the file system or maybe a secondary bootstrap program 3 Bootstrap program passes control to kernel 5 Kernel handcrafts process 0 0 Process 0 handcrafted kernel process Process 0 forks child process 1 Child process 1 execs sbininit 24 15213 F O2 Unix Startup Step 2 0 etcinittab gt 39 39 111113 1 init forks and execs daemons per etcinittab and forks Daemons ge tty fagrdt Sn tty program seg ftpd httpd 25 15213 F 02 Unix Startup Step 3 0 The getty process execs a login program 26 15213 F 02 Unix Startup Step 4 0 tcsh login reads login and passwd if OK it execs a shell if not OK it execs another getty 27 15213 F 02 Unix Process Hierarchy 0 initE Login shell Child w 28 15213 F O2 I I I I I II I o So the kernel never spontaneously creates a process except for process zero The kernel creates a process when some existing process calls fork 29 15213 F O2 Programmer s Model of Multitasking Basic Functions I fork spawns new process 0 Called once returns twice 0 Parent and child process both resume running where fork returns I exit terminates the process that calls it o Called once never returns I wait and waitpid wait for and reap terminated children I execl execv and friends 0 run a new program in an existing process 0 Called once normally never returns 30 15213 F 02 Example Shell Programs A shell is an application program that runs programs on behalf of the user I sh Original Unix Bourne Shell I csh BSD Unix C Shell tcsh Enhanced C Shell I bash BourneAgain Shell int main char cmdlineMAXLINE while 1 read printfquotgt fgetscmdline MAXLINE stdin if feofstdin exit0 evalcmdline Execution is a sequence of readevaluate steps 15213 F O2 Simple Shell eval Function void evalchar cmdline OL char argvMAXARGS argv for execve int bg pidt pid should the job run in bg or fg process id bg parselinecmdline argv if builtin commandargv if pid Fork 0 child runs user job if execveargv0 argv environ lt 0 printfquots Command not foundnquot argv0 exit0 if bg parent waits for fg job to terminate int status if waitpidpid ampstatus 0 lt 0 unixerrorquotwaitfg waitpid errorquot else otherwise don t wait for bg job printfquotd squot pid cmdline IULIUI UL Summarizing Processes I At any given time a system can have multiple active processes I Only one can execute at a time though 0 Per CPU that is I Each process from its own point of view appears to have total control of a virtual computer 0 A CPU including its registers o A virtual memory space 33 15213 F 02 Summarizing cont Spawning Processes I Call tO fork 0 One call two returns Terminating Processes I Call exit 0 One call no return Reaping Processes I Call wait or waitpid Replacing Program Executed by Process I Call execl or variant 0 One call normally no return 34 15213 F O2


Buy Material

Are you sure you want to buy this material for

25 Karma

Buy Material

BOOM! Enjoy Your Free Notes!

We've added these Notes to your profile, click here to view them now.


You're already Subscribed!

Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'

Why people love StudySoup

Jim McGreen Ohio University

"Knowing I can count on the Elite Notetaker in my class allows me to focus on what the professor is saying instead of just scribbling notes the whole time and falling behind."

Amaris Trozzo George Washington University

"I made $350 in just two days after posting my first study guide."

Bentley McCaw University of Florida

"I was shooting for a perfect 4.0 GPA this semester. Having StudySoup as a study aid was critical to helping me achieve my goal...and I nailed it!"

Parker Thompson 500 Startups

"It's a great way for students to improve their educational experience and it seemed like a product that everybody wants, so all the people participating are winning."

Become an Elite Notetaker and start selling your notes online!

Refund Policy


All subscriptions to StudySoup are paid in full at the time of subscribing. To change your credit card information or to cancel your subscription, go to "Edit Settings". All credit card information will be available there. If you should decide to cancel your subscription, it will continue to be valid until the next payment period, as all payments for the current period were made in advance. For special circumstances, please email


StudySoup has more than 1 million course-specific study resources to help students study smarter. If you’re having trouble finding what you’re looking for, our customer support team can help you find what you need! Feel free to contact them here:

Recurring Subscriptions: If you have canceled your recurring subscription on the day of renewal and have not downloaded any documents, you may request a refund by submitting an email to

Satisfaction Guarantee: If you’re not satisfied with your subscription, you can contact us for further help. Contact must be made within 3 business days of your subscription purchase and your refund request will be subject for review.

Please Note: Refunds can never be provided more than 30 days after the initial purchase date regardless of your activity on the site.