unix system programming unix system programming
play

UNIX System Programming UNIX System Programming 1. What is a - PDF document

UNIX System Programming UNIX System Programming 1. What is a Process? 1. What is a Process? Processes 2. fork() 2. fork() 3. Example: 3. Example: talkto.c talkto.c 4. exec() 4. exec() 5. wait() 5. wait() 6. Process Data 6.


  1. UNIX System Programming UNIX System Programming 1. What is a Process? 1. What is a Process? Processes 2. fork() 2. fork() 3. Example: 3. Example: talkto.c talkto.c 4. exec() 4. exec() 5. wait() 5. wait() 6. Process Data 6. Process Data � Objectives Objectives � 7. Special Exit Cases 7. Special Exit Cases – look at how to program UNIX processes – look at how to program UNIX processes 8. Process IDs 8. Process IDs – fork() and exec() – fork() and exec() 1 2 1730 UNIX System Programming: Processes Maria Hybinette 1730 UNIX System Programming: Processes Maria Hybinette Overview Overview 1. What is a Process? 1. What is a Process? 1. What is a Process? � A process is an executing program. A process is an executing program. � 2. fork() 3. Example: talkto.c � A process: A process: � 4. exec() $ cat file1 file2 & 5. wait() � Two processes: Two processes: 6. Process Data � $ ls | wc - l $ 7. File Descriptors across Processes 8. Special Exit Cases � Each user can run many processes at once Each user can run many processes at once � 9. IO Redirection (e.g. using & (e.g. using & ) ) 10. User/Group ID real and effective continued 3 4 1730 UNIX System Programming: Processes Maria Hybinette 1730 UNIX System Programming: Processes Maria Hybinette A More Precise Definition What makes up a Process? A More Precise Definition What makes up a Process? � program code program code � � A process is the A process is the context context (the (the � information/data) maintained for an information/data) maintained for an � data variables data variables � executing program. executing program. � open files (file descriptors) open files (file descriptors) � � an Environment (environment variables; an Environment (environment variables; � credentials for security) credentials for security) 5 6 1730 UNIX System Programming: Processes Maria Hybinette 1730 UNIX System Programming: Processes Maria Hybinette 1 UNIX: processes Maria Hybinette

  2. Some of the Context Some of the Context Information Information – Process ID ( Process ID ( pid ) unique integer – pid ) unique integer – – Pointer to program code Pointer to program code – Parent process ID ( – Parent process ID ( ppid ppid ) ) – Pointer to data – Pointer to data Memory for global vars Memory for global vars – Real User ID – Real User ID ID of user/process which ID of user/process which – – Pointer to stack Pointer to stack Memory for local Memory for local vars vars started this process started this process – Pointer to heap Pointer to heap Malloc’d memory memory – Malloc’d – Effective User ID – Effective User ID ID of user who wrote ID of user who wrote the process’ program the process’ program – Execution priority Execution priority – – Current directory – Current directory – – Signal information Signal information – File descriptor table – File descriptor table – Environment – Environment VAR=VALUE pairs pairs VAR=VALUE continued 7 8 1730 UNIX System Programming: Processes Maria Hybinette 1730 UNIX System Programming: Processes Maria Hybinette Important System Unix Start Up Processes Diagram Important System Unix Start Up Processes Diagram Processes Processes OS kernel Process 0 � init init – – Mother of all processes. init is started Mother of all processes. init is started � (sched) at boot time and is responsible for starting at boot time and is responsible for starting Process 1 other processes. other processes. (init) – init uses file – init uses file inittab inittab & directories: /etc/ & directories: /etc/rc?.d rc?.d � getty getty – – login process that manages login login process that manages login � getty getty getty sessions. sessions. login login csh bash 9 10 1730 UNIX System Programming: Processes Maria Hybinette 1730 UNIX System Programming: Processes Maria Hybinette Pid and Parentage and Parentage 2. fork() Pid 2. fork() � A process ID or A process ID or pid pid is a positive integer that uniquely is a positive integer that uniquely � � #include <sys/types.h> identifies a running process, and is stored in a variable of identifies a running process, and is stored in a variable of #include <unistd.h> type type pid_t pid_t . . pid_t fork( void ); � You can get the You can get the process process pid pid or or parent’s parent’s pid pid � � Creates a child process by making a copy of Creates a child process by making a copy of � #include <sys/types> the parent process. the parent process. main() { pid_t pid, ppid; � Both the child Both the child and and the parent continue the parent continue printf( "My PID is:%d\n\n",(pid = getpid()) ); � printf( "Par PID is:%d\n\n",(ppid = getppid()) ); running. running. } 11 12 1730 UNIX System Programming: Processes Maria Hybinette 1730 UNIX System Programming: Processes Maria Hybinette 2 UNIX: processes Maria Hybinette

  3. fork() as a diagram fork() as a diagram Process IDs (pids Process IDs ( pids revisited) revisited) Parent � pid pid = fork(); = fork(); � pid = fork() Child � In the child: In the child: pid == 0 ; ; � pid == 0 In the parent: pid In the parent: == the process ID of the the process ID of the Returns a new PID: pid == pid == 0 e.g. pid == 5 child. child. Shared Program Data Data Copied � A program can use this A program can use this pid pid difference to do difference to do � different things in the parent and child. different things in the parent and child. 13 14 1730 UNIX System Programming: Processes Maria Hybinette 1730 UNIX System Programming: Processes Maria Hybinette fork() Example (parchld.c parchld.c) ) fork() Example ( #include <stdio.h> #include <sys/types.h> #include <unistd.h> int main() else { { pid_t pid; /* could be int */ /* child */ int i; for( i=0; I < 1000; i++ ) pid = fork(); printf( “CHILD %d\n”, i ); if( pid > 0 ) } { return 0; /* parent */ } for( i=0; i < 1000; i++ ) printf(“\t\t\tPARENT %d\n”, i); } 15 16 1730 UNIX System Programming: Processes Maria Hybinette 1730 UNIX System Programming: Processes Maria Hybinette Possible Output Things to Note Things to Note Possible Output i is copied between parent and child. is copied between parent and child. � i � CHILD 0 CHILD 1 CHILD 2 � The switching between the parent and child The switching between the parent and child PARENT 0 � PARENT 1 depends on many factors: depends on many factors: PARENT 2 – – machine load, system process scheduling machine load, system process scheduling PARENT 3 CHILD 3 CHILD 4 � I/O buffering effects amount of output shown. I/O buffering effects amount of output shown. � PARENT 4 : � Output interleaving is Output interleaving is nondeterministic nondeterministic � – cannot determine output by looking at code – cannot determine output by looking at code 17 18 1730 UNIX System Programming: Processes Maria Hybinette 1730 UNIX System Programming: Processes Maria Hybinette 3 UNIX: processes Maria Hybinette

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend