chapter - 5 - usp
TRANSCRIPT
![Page 1: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/1.jpg)
PROCESS CONTROL
![Page 2: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/2.jpg)
Topics covered
Introduction
Process identifiers
Fork function
vfork function
Exit function
wait and waitpid function
![Page 3: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/3.jpg)
Wait3 and wait4 functions
Race conditions
Exec conditions
Changing user ID’s and Group ID’s
Interpreter files
System function
User identification
Process times
Process acounting
![Page 4: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/4.jpg)
Introduction
Process control – concerned – creation of new processes , program execution and process termination
![Page 5: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/5.jpg)
Process Identifiers
1. Process ID (pid)
- unique process ID
- Function to retrieve – getpid
prototype
#include<unistd.h>
pid_t getpid(void);
Returns : process ID of calling process
![Page 6: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/6.jpg)
2. Parent Process ID ( ppid) pid retrieved – getppid prototype pid_t getppid(void); Returns : parent process ID of the calling process
3. Real user ID (ruid) is user created – parent process prototype uid_t getuid(void); Returns : real user ID of the calling process
![Page 7: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/7.jpg)
4. Effective user ID (euid) - same as real user id , except file executed – create process – set – uid
turned on prototype : uid_t geteuid(void) Returns : effective user ID of calling process 5. Real group ID (gid) : - id - user – parent process prototype : gid_t getgid(void); Returns : real group ID of calling proces
![Page 8: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/8.jpg)
6. Effective Group ID (egid) :
same as real group id , except file executed – create process – set – gid flag
turned on
function ptototype :
gid_t getegid(void);
Returns : effective ID of the calling process
![Page 9: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/9.jpg)
Fork function
- process created – existing process – calls fork function
#include <unistd.h>
pid_t fork(void);
Returns : 0 in child , process ID of child in parent , -1 on error
- new process created – fork – child process
![Page 10: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/10.jpg)
Program to demonstrate fork function
#include <sys/types.h>
#include <unistd.h>
int main( )
{
fork( );
printf(“\n hello USP”);
}
![Page 11: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/11.jpg)
Properties of parent inherited by child
• Real user ID , real group ID , effective user ID , effective group ID
• Supplementary group ID’s
• Process group ID
• Session ID
• Controlling terminal
• Set -user-id and set-group-id
• Current working directory
• Root directory
![Page 12: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/12.jpg)
• File mode creation mask
• Signal masks and dispositions
• Close – on – exec flag for any open file descriptors
• Environment
• Attached shared memory segments
• Memory mappings
• Resource limits
![Page 13: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/13.jpg)
Differences between parent and child are
• Return value from fork
• Process IDs are different
• The 2 processes have different parent process IDs : parent process ID of the
child is the parent ; the parent process ID of the parent doesn’t change
• The child’s tms_utime , tms_stime , tms_cutime , and tms_cstime values
are set to 0
• File locks set by the parent are not inherited by the child
• The set of pending signals for the child is set to the empty set
![Page 14: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/14.jpg)
2 reasons for calling fork system call are :
• Process – duplicates itself so that parent and child can each execute
different sections of code at the same time
• Common for network servers
• Process – wants to execute different program
• Common for shells
![Page 15: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/15.jpg)
vfork function
- same calling sequence and same return values as that of fork
- intended – create – new process
Prototype of vfork is
#include<unistd.h>
#include<sys/types.h>
pid_t vfork(void);
Returns : 0 in child , process ID of child in parent , -1 on error
![Page 16: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/16.jpg)
Example of vfork#include<unistd.h>#include<sys/types.h>int glob = 6;int main(void){ int var; pid_t pid; var = 88; printf(“before vfork\n”); if((pid=vfork( ))<0){
![Page 17: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/17.jpg)
err_sys(“vfork error”);}else if (pid= = 0){glob++;var++;_exit(0);}
![Page 18: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/18.jpg)
Exit functions
Process can be terminated in 5 ways
• Executing return from main function
• Calling exit function
• Calling the _exit or _Exit
• Executing a return from the start routine of the last thread in the process
• Calling the pthread_exit function from the last thread in the process
![Page 19: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/19.jpg)
3 forms of abnormal termination
• Calling abort
• When the process receives certain signals
• The last thread responds to a cancellation request
Wait and waitpid functions
Parent process – use – wait – waitpid – wait – child process – terminate and
retrieve the child exit status
prototype
#include<sys/wait.h>
pid_t wait(int *statloc);
![Page 20: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/20.jpg)
#include<sys/wait.h>
pid_t wait(int *statloc);
pid_t waitpid(pid_t pid , int *statloc , int options);
both return : process ID if ok and -1 on error
Differences between wait and waitpid
Wait – block – caller , waitpid – option – prevents – blocking
Waitpid – doesn’t wait – child – terminates
Wait – child terminated
![Page 21: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/21.jpg)
Parent can check the exit status code with the following macro’s defined in <sys/wait.h>
• WIFEXITED
• WEXITSTATUS
• WIFSIGNALLED
• WTERMSIG
• WIFSTOPPED
• WSTOPSIG
![Page 22: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/22.jpg)
Wait3 and wait4 functions - provide additional arguments – allows – kernel – return – summary –
resources – used – terminated process prototype #include < sys/types.h> #include <sys/wait.h> #include <sys/time.h> #include<sys/resource.h> pid_t wait3(int *statloc , int options , struct rusage *rusage); pid_t wait4(pid_t pid , int *statloc , int options , struct rusage *rusage); both return : process ID if ok , -1 on error
![Page 23: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/23.jpg)
Race conditions• Occurs – multiple processes – trying – something – shared data – final
outcome – depends – order – processes runs• To avoid race condition and avoid poling – signaling required – multiple
processes• Program that contains a race condition : #include<stdlib.h> #include<sys/types.h> static void charatatime(char *); int main (void) { pid_t pid;
![Page 24: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/24.jpg)
if((pid=fork( ))<0){ printf(“fork error”);}else if(pid = = 0){ charatatime(“output from child\n”);}else{ charatatime(“output from parent\n”); }
exit(0);}
![Page 25: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/25.jpg)
static void charatatime(char *str){ char *ptr; int c; stdbuf(stdout , NULL); for (ptr = str; (c = *ptr++) ! = 0) putc(c, stdout);}• To avoid race condition TELL and WAIT functions can be used in above
program
![Page 26: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/26.jpg)
Exec functions• Causes – calling process – change – context – execute – different program• 6 versions of exec system calls• Same function – differ – argument lists• Process ID does not change – new process – not createdPrototype of these functions#include <unistd.h>int execl(const char *pathname , const char *arg0 , …/* (char *) 0 */ );int execv(const char *pathname , char *const argv [ ]);int execle(const char *pathname , const char *arg0,…/*(char *)0, char *constenvp */ );
![Page 27: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/27.jpg)
int execve(const char *pathname , char *const argv[ ] ,char *const envp[ ]);
int execlp(const char *filename, const char *ag0,…/* (char *)0 */ );
int execvp(const char *filename , char *const argv [ ] );
Return : -1 on error , no return on success
- 1st difference – 1st 4 – pathname – argument , last 2 – filename argument
- Filename - / - pathname
- Executable file – searched – directories specified – PATH environment
variable
![Page 28: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/28.jpg)
Additional properties inherited from calling process
• Process ID and parent process ID
• Real user ID and real group ID
• Supplementary group ID’s
• Process group ID
• Session ID
• Controlling terminal
• Time left until alarm clock
![Page 29: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/29.jpg)
• Current working directory
• Root directory
• File mode creation mask
• File locks
• Process signal mask
• Pending signals
• Resource limits
![Page 30: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/30.jpg)
![Page 31: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/31.jpg)
Changing user ID’s and group ID’s
1.Setuid and setgid functions
Real group ID and effective group ID - set – setgid
Prototype :
#include<unistd.h>
int setuid(uid_t uid);
int setgid(gid_t gid);
Return : 0 if ok , 1 on error
![Page 32: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/32.jpg)
Rules to change these ID’s
• Process – superuser privileges – setuid function – sets – real user ID ,
effective user ID , saved user ID – uid
• Process – no – superuser privileges – uid – equals – either – real user ID –
saved set-user-ID
• Errno – EPERM – 1 RETURNED
![Page 33: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/33.jpg)
• Superuser process – change – real user ID• Effective user ID – set – exec functions – set user ID set – program file• Saved set user ID – copied – effecctive user ID – exec
Setreuid and setregid functions
• Swapping – real user ID , effective user ID – setreuid function
#include <unistd.h>
int setreuid(uid_t ruid , uid_t euid);
int setregid(gid_t rgid , gid_t egid);
Return : 0 if ok , -1 on error
![Page 34: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/34.jpg)
Seteuid and setegid functions
- Similar – setuid and setgid , effective user ID and effcetive group ID
changed
#include <unistd.h>
int seteuid(uid_t uid);
int setegid(gid_t gid);
Return : 0 if ok , 1 on error
![Page 35: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/35.jpg)
Interpreter files
- Text files – begin – line of form
# ! pathname [ optional – argument]
- Space – exclamation – pathname- optional
- Most common
# !/bin/sh
Program
#include < sys/types.h>
#include <sys/wait.h>
int main (void)
{
pid_t pid;
![Page 36: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/36.jpg)
if ((pid = fork( )) < 0 ){ printf(“fork error”);}else if( pid = = 0){ /* child*/if (execl(“/home/vtu/testinterp” , “testinterp” , “myarg1 “ ,”MY ARG2” , (char * )
0) < 0 )printf(“execl error”);}if(waitpid(pid , NULL , 0) < 0 ) /*parent*/printf(“waitpid error”);exit(0);}
![Page 37: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/37.jpg)
System function
- Used – execute – command string – program
- Can be called as
system(“date > file “);
Prototype :
#include <stdlib.h>
int system(const char *cmdstring);
- System function – implemented – calling fork , exec and waitpid
- Calls – fork – create – child process
- Inturn – calls – execlp – execute – bourne shell with –c option
![Page 38: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/38.jpg)
3 types of return values
• either – fork – fails – waitpid – returns – error – system returns 1 – errno –
set – indicate – error
• Exec – fails – return value – shell – executed – exit
• All 3 succeed – return value – termination status
![Page 39: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/39.jpg)
User identification
System – keeps – track – name – logged in
getlogin – way – fetch – login name
Prototype :
#include <unistd.h>
char *getlogin(void);
Return : pointer to string giving login name if ok , NULL on error
![Page 40: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/40.jpg)
Process times
3 values of a process maintained
• Wall clock time
• System CPU time
• User CPU time
Prototype :
#include <sys/times.h>
clock_t times(struct tms *buf);
Returns : elapsed wall clock time in clock ticks if ok , -1 on error
![Page 41: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/41.jpg)
struct tms
{
clock_t tms_utime;
clock_t tms_stime;
clock_t tms_cutime;
clock_t tms_cstime;
};
![Page 42: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/42.jpg)
Process accounting
- When enabled – kernel – writes – acounting record – process terminates
- Accounting records – small – amount – binary data – command name , amount
of CPU time used , user ID , group ID
- Superuser – executes – accton – pathname argument – enable accounting
- Structure – accounting records – defined - <sys/acct.h>
struct acct
{
![Page 43: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/43.jpg)
char ac_flag;char ac_stat;uid_t ac_uid;gid_t ac_gid;dev_t ac_tty;time_t ac_btime;comp_t ac_utime;comp_t ac_stime;comp_t ac_etime;comp_t ac_mem;comp_t ac_io;comp_t ac_rw;};
![Page 44: Chapter - 5 - Usp](https://reader033.vdocuments.us/reader033/viewer/2022061122/54713d90b4af9fec728b4577/html5/thumbnails/44.jpg)
ac_flag description
AFORK process is the result of fork , but never called exec
ASU process used superuser privileges
ACOMPAT process used compatibility mode
ACORE process dumped core
AXSIG process was killed by a signal
AEXPND expanded accounting entry