Class Notes (836,147)
Canada (509,656)
CSC209H1 (41)
Karen Reid (20)
Lecture

mar01c.docx

3 Pages
124 Views
Unlock Document

Department
Computer Science
Course
CSC209H1
Professor
Karen Reid
Semester
Winter

Description
Processes (from feb15c 23:15) o Ex.1 tryfork1.c 1. #include  Process State 2. #include o Only one process (instruction) can be running (executing) on a 3. #include // contains fork() uniprocessor at a time (this is a simplification form the real model)  The scheduler decides which of the ready processes to run 4. 5. int main() {  Multitasking can still be accomplished with a uniprocessor by 6. int pid = fork(); switching between processes quickly 7. if (pid < 0) {  Feels concurrent b/c every ~20 millisecond the processor 8. perror("fork()"); 9. } else if (pid > 0) { is switches to a different process 10. printf("parent\n"); o A process if ready if it could use the CPI immediately 11. } else { // pid == 0  For 1 process that's running, there's several processes ready and12. printf("child\n"); 13. } waiting to be ran 14. return 0; o A process is blocked if it is waiting for an event (I/O, signal, etc.15. }  Ex. waiting for user input or waiting for data to arrive from disk  output; note that print order not garenteed  Can explicitly put a process to sleep  b/c printf in the parent and child is ran concurrently  the operating system decides which gets execute first running  order is arbitrary btwn machines but usually the same order on the same machine (depend on scheduler) $ gcc -Wall -g -o tryfork tryfork.c $ ./tryfork parent blocked or child ready sleeping o fork() properties  Properties of parent that's inherited by child: o Other states: initial state and termination state  UID, GID  Controlling terminal o Creating new process in UNIX  create a copy of a currently running  CWD, root directory process with fork()  init is the initial process ran at system startup and lasts until  Signal mask, environment, resource limits  Shared memory segments the system is shut down, only process not called by fork()  Differences between parent and child  It is the direct or indirect parent of all other processes  PID, PPID, return value from fork()  Pending alarms cleared for child  fork() o The fork system call creates a duplicate of the currently running  Pending signals are cleared for child program; results in two completely different processes o Ex.2 tryfork2.c  After the fork () call the parent process blocks and control is 1. #include 2. #include passed to the operating system 3. #include // contains pid_t  Operating system then copys all the relevant data; restarts the 4. #include parent process and the child process 5. 6. int main(int argc, char **argv) { o The duplicate (child process) and the original (parent process) both proceed from the point of the fork 7. int i;  Both retain exactly the same data, and the same state expect for 8. pid_t pid; // actually an unsigned int 9. a couple of distinguishable details 10. i = 5; o Use the child process to run specific programs with exec 11. printf("%d\n", i); o The only difference is the return value from the fork call 12. pid = fork(); 13. 14. if (pid < 0) { fork() 15. perror("fork()"); // fork() error Process 16. } else if (pid > 0) { A 17. i = 6; // only parent gets here Process 18. } else { // pid == 0 A 19. i = 4; // only child gets here Process 20. } A1 21. printf("%d\n", i); 22. 23. return 0; int fork(void) 24. }  If fork() succeeds it returns the child's PID to the parents, and  Stepthrough the parent and child process returns 0 to child  Original process (parent)  If fork() fails, it returns -1 to the parent, no child is created 11. // this line & above runs normally and sets errno 12. pid = fork(); // creates child 13.  There is a limit to the maximum number of processes a 14. if (pid < 0) { user can create and the operating can keep track 15. perror("fork()");  Once the limit reached, subsequent calls to fork() 16. } else if (pid > 0) { // pid == 677 17. i = 6; return -1 18. } else { o PIDs (process id) and PPIDs (parent process id) 19. i = 4;  int getpid() – returns the PID of current process 20. } 21. printf("%d\n", i); // prints 6  int getppid() – returns the PID of parent process 22.  The PID of init is 1 23. return 0;  Then getppid() on an orphan child process returns 1 24. …  child process o A process that calls wait() can: 11. // this line and above executes normally 12. pid = fork(); // inherits parent's state  Block if all of its children are still running  usually wanted  Return immediately with the termination status of a child 13. // diverges from here on  If a child has terminated and is waiting for its termination 14. if (pid < 0) { 15. perror("fork()"); status to be fetched 16. } else if (pid > 0) {  Returns the PID of the terminated child 17. i = 6;  Return immediately with an error (-1) and changes errno 18. } else { // pid == 0 19. i = 4;  If doesn't have any child processes  status = useless 20. } o If more than 1 child wait() returns on termination of any children 21. printf("%d\n", i); // prints 4 22. o Useful wait status macros  status contains a lot of exit info, use macros to decipher it 23. return 0;  ex. WIFEXITED & WEXITSTATUS, WIFSTOPPED & 24. …  output WSTOPSIG, WIFSIGNALED & WTERMSIG $ gcc -Wall -g -o tryfork2 tryfork2.c o Some process don't need wait to be cleaned up $ ./tryfork2  ex. daemon processes = background processes 5 6 pid_t waitpid(pid_t pid, int *status, int option); 4 o Used to wait for a specific child PID  Process initializing in
More Less

Related notes for CSC209H1

Log In


OR

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


OR

By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.


Submit