3 - ELEC 2602 Object Oriented & Systems Programming Module...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
1 ELEC 2602 – Object Oriented & Systems Programming Module 3 – Process Control
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Process Control: Overview ¾ Process IDs default processes and how to obtain process IDs; ¾ Creation of new/child processes – fork() versus vfork(); ¾ Coordination between processes – file sharing versus race condition; ¾ Process termination – waitpid() ; ¾ Measuring Process Times – times() ; ¾ Executing programs – exec(); ¾ Sample program illustrating the combined uses of fork() and exec() !
Background image of page 2
3 Critical Processes & Their IDs on the Unix Environment ¾ Process ID 0 is the scheduler process, called the swapper z no program on disk corresponds to this process z it is part of the kernel and is known as a system process ¾ Process ID 1 is usually the init process z invoked by the kernel at the end of the bootstrap procedure z program file for this process is: /sbin/init z it reads the system-dependent initialization files (the /etc/rc* files) z runs perpetually ¾ Process ID 2 is usually the pagedaemon
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 To obtain Process IDs… #include <sys/types.h> #include <unistd.h> pid_t getpid(void); /* return process ID of calling process */ pid_t getppid(void); /* return parent process ID */ uid_t getuid(void); /* return real user ID of calling process */ uid_t geteuid(void); /* return effective user ID */ gid_t getgid(void); /* return real group ID of calling process */ gid_t getegid(void); /* return effective group ID */
Background image of page 4
5 Process Control: fork() #include <sys/types.h> #include <unistd.h> pid_t fork(void); ¾ The only way a new process is created by the UNIX kernel is when an existing process calls fork() (doesn’t apply to processes 0, 1, and 2). The new process created by fork() is called the child process; ¾ fork() is called once but returns twice—by both the parent and child z The only difference in the returns is that the return value in the child process is 0, while the return value in the parent is the process ID of the new child; both the child and parent continue execution with the instruction that follows the call to fork ¾ Logically, the child gets a copy of the parent’s data space, heap, and stack ¾ Physically, copy-on-write (COW) is implemented—the child and parent shares the memory space, except for those memory pages that either one of them tries to modify
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Process Control: vfork() ¾ vfork() is intended to create a new process when the purpose of the new process is to exec a new program ¾ vfork() creates the new process, just like fork() , without fully copying the address space ¾ While the child is running, until it calls either exec or exit, the child runs in the address space of the parent ¾ vfork() guarantees that the child runs first , until the child calls exec or exit, at which time the parent resumes z can lead to deadlock if the child depends on further actions of the parent before calling exec or exit
Background image of page 6
7 Sample Prog. # 1 –
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 07/05/2008 for the course EEE ELEC2602 taught by Professor Dr.vincenttam during the Winter '08 term at HKU.

Page1 / 23

3 - ELEC 2602 Object Oriented & Systems Programming Module...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online