Presentation is loading. Please wait.

Presentation is loading. Please wait.

Processes Creation and Threads

Similar presentations


Presentation on theme: "Processes Creation and Threads"— Presentation transcript:

1 Processes Creation and Threads

2 Creation of Processes Creation is very specific to an operating system. OS needs to: Reserve memory for process Add entry in process table Load the application code (optional?) All systems start from a single process.

3 Process Creation Calc.exe MyApp.exe CreateProcess(“calc”)

4 Linux Process Based on a system call named fork(). Syntax:
pid_t fork(); Duplicates the process calling this function. The new process is called the child, the original process is called the parent.

5 Linux Processes The child process will see 0 returned from the fork() call, the parent will see the process id of the new process. Often the new process will want to execute other command rather than just being a duplicate. Uses exec() function.

6 Example Unix call Main() { do_something(); if (fork() == 0) { // this is the child process execl(“xcalculator”); } // the child process will never make it here! do_morestuff();

7 What Happens Here? main() { while (1) fork(); }

8 Timeline xcalculator MyApp exec() MyApp MyApp fork()

9 Terminating a Process Memory needs to be released
Entry needs to be removed from process table. Usually the process asks to terminate itself! Program calls ExitProcess() or exit(). If your program doesn’t call this, the library (Java, C run time) will call it.

10 Terminating Other Processes
Can one process ask another to stop? If the first process has enough permission then yes. OS will not deliver the message otherwise. The process sends a special signal to the other process. TerminateProcess (Windows) kill (Linux)

11 Other Techniques The OS will remove any process that has caused a fault. “This application has performed an illegal operating and will be shut down.”

12 Design of an E-mail Client
We design an client like Outlook. Problem: Our client will notify the user when new mail comes in (even if they are creating a new message).

13 Attempt #1 Forever() { wait for a key add key to the new message if (new mail) { play notification }

14 Attempt #2 Forever() { wait for a key but no longer than 1 second if (key pressed) { add character to message } if (new mail) { play notification

15 Divide The Problem! // checking for new mail Forever() { sleep for an amount of time (or better yet block) if (new mail) play notification } // Handling keyboard wait for key (yes, block waiting for the key) add character to mail message

16 Two separate programs? If we could run both of these loops at the same time, our problem would be easier! Two processes are possible but sharing information between them is not convenient.

17 Thread A thread is a sequence of instructions.
Each process contains at least 1 thread. Many OS allow for multiple threads within a single process. Often called a lightweight process. Provides multi-tasking within a single process.

18 Threads Threads have access to all variables that are within the same program. If you have a global, you can read it or change it. No security between threads of a single process Easy to share information Easy to make mistakes!

19 Reasons for Threads Can simplify problems
On multi-core processors two threads can run simultaneously. Programs need to take advantage of this


Download ppt "Processes Creation and Threads"

Similar presentations


Ads by Google