P ro ce ss e s The P ro ce ss M ode l M ultiprogr a mm i ng of four progr a m s Conce ptu a l mode l of 4 ind e p e nd e nt , s e qu e nt i a l pro ce ss e s O nl y one progr a m ac tive a t a ny i ns t a nt 2
3 P ro ce ss Ta b le Whe n a proces s move s fro m a runnin g state t o a read y o r blocke d stat e i t mus t store certai n informatio n s o tha t i t ca n restar t from th e s ame point w he n i t m ove s bac k t o a runni ng s t a te Whic h instructio n i t wa s abou t t o execute Whic h recor d i t wa s abou t t o rea d fro m it s input file Value s i n th e registers
P ro ce ss Con trol B l oc k F iel ds of a pro ce ss t a ble e nt ry 4
U n i x P ro ce ss S tate T r a ns iti ons 5
6 U n i x P ro ce ss S tate s Exec uti ng i n us e r mode . Exec uti ng i n ke rn e l m ode . N ot exec uti ng but r ea dy t o run w he n s che dul e d. S l ee pi ng (blo cke d) i n m a i n m e m ory. R ea dy, but sw appe d out t o di s c . S l ee pi ng, but sw appe d out t o di s c .
7 U n i x P ro ce ss S tate s (2) R e turni ng from the ke rn e l t o us e r mode , but th e n pr eempte d (s i m il a r t o s t a te 3). N e w l y c r ea t e d a nd i n a tr a ns iti on s t a t e . It e xi s t s but i s not r ea dy t o run nor i s it s l ee pi ng. H a s exec ut e d e xit () a nd i s a zombie . It no l onge r e xi s t s but l eave s a r ec ord of e xit code a nd t i m i ng s t a t s for it s pa r e nt .
M u c h M or e in / p r o c $ ca t /proc/pc i PC I device s found : Maste r Capable . N o bursts . Mi n Gnt=2 . Bu s , Clas s IR Q devic e 2 , functio n : 0300 : PC I devic e 8086:258 2 (re v 4) . 11 . Non-prefetchabl e 3 2 bi t memor y a t 0xdff0000 [0xdff7f f I/ O a t 0xe89 8 [0xe89f] . . . . Bu s , devic e , functio n : Clas s 0600 : PC I devic e 8086:258 (re v 4) . Bu s , devic e 1 , functio n : Clas s 0604 : PC I devic e 8086:258 1 (re v 4) . IR Q 11 .
Can a process create a process ? Yes! Unique identity of process is the “process ID” (or PID) Fork() system call creates a copy of current process with a new PID Return value from Fork(): integer When > 0: Running in (original) Parent process return value is pid of new child When = 0: Running in new Child process When < 0: Error! Must handle somehow Running in original process All state of original process duplicated in both Parent and Child! Memory, File Descriptors (next topic), etc…
UNIX Process Management UNIX fork – system call to create a copy of the current process, and start it running No arguments! UNIX exec – system call to change the program being run by the current process UNIX wait – system call to wait for a process to finish UNIX signal – system call to send a notification to another process UNIX man pages: fork (2), exec (3), wait (2), signal (3)
f ork2.c int status; … cpid = fork(); if ( cpid > 0) { /* Parent Process */ mypid = getpid (); printf ("[%d] parent of [%d]\n", mypid,cpid ); tcpid = wait(&status); printf ("[%d] bye %d(%d)\n", mypid , tcpid , status); } else if ( cpid == 0) { /* Child Process */ mypid = getpid (); printf ("[%d] child\n", mypid ); } …
389] parent of [390] [390] child [389] bye 390(0)
UNIX Process Management
Process Races: fork3.c Question: What does this program print? Does it change if you add in one of the sleep() statements? int i ; cpid = fork(); if ( cpid > 0) { mypid = getpid (); printf ("[%d] parent of [%d]\n", mypid , cpid ); for ( i =0; i <100; i ++) { printf ("[%d] parent: %d\n", mypid , i ); // sleep(1); } } else if ( cpid == 0) { mypid = getpid (); printf ("[%d] child\n", mypid ); for ( i =0; i >-100; i --) { printf ("[%d] child: %d\n", mypid , i ); // sleep(1); } }
19 P ro ce ss Te rm i n ati on Cond iti ons w hi c h te r mi nat e pro ce ss e s N or m a l e xit (volunt a ry) Error e xit (volunt a ry) F a t a l e rror (involunt a ry) K ill e d by a noth e r pro ce ss (involunt a ry)
3 types of Kernel Mode Transfer Syscall Process requests a system service, e.g., exit Like a function call, but “outside” the process Does not have the address of the system function to call Like a Remote Procedure C all (RPC) – for later Marshall the syscall ID and arguments in registers and execute syscall Interrupt External asynchronous event triggers context switch e g . Timer, I/O device Independent of user process Trap or Exception Internal synchronous event in process triggers context switch e .g., Protection violation (segmentation fault), Divide by zero, …
I nt e rrupt s Forcibly change normal flow of control Enters the kernel at a specific point; the kernel then figures out which interrupt handler should run Many different types of interrupts
T y p es o f I nt e rrupt s Sy nchronous v ersus asynchronous Asy nchronous F ro m e xte r na l sourc e , su ch as I/O d e v i ce N ot re l ate d to i nst r uct i o n be i n g e x ecute d Sy nchronous (a l so ca ll e d e xcept i on s ) Pro g r a mm i n g errors or requests f or k e r ne l i nte r v ent i o n F au l ts — correcta b l e; of f end i n g i nst r uct i o n i s ret r i e d T r aps — ofte n f or de b ugg i ng; i nst r uct i o n i sn ’t ret r i e d
/ p r o c/i nt e rrupt s $ ca t /proc/interrupt s CPU C olu m ns: IR Q , count, in ter r upt contr olle r , d e v i ces : 13006660 9 XT-PI C time r 2 : XT-PI C cascad e 3 : XT-PI C uhci_hc d 5 : XT-PI C uhci_hc d 8 : 43 6 XT-PI C rt c 9 : 243156 8 XT-PI C acpi , libata , uhci_hcd , et h 10 : XT-PI C ehci_hcd , uhci_hc d 14 : 117024 XT-PI C ide NMI : ERR :
Int e rrupt hand li ng ( a ) (b) S t e ps i n s t a rti ng a n I/ O d e vi c e a nd g e tti ng int e rrupt How the CPU i s int e rrupt e d 25
Int e rrupt hand li ng (2) S k e l e t on of w h a t l ow e s t l e v e l of OS do e s w h e n a n in te rrupt o cc urs 26
Interrupt Controller Interrupts invoked with interrupt lines from devices Interrupt controller chooses interrupt request to honor Mask enables/disables interrupts Priority encoder picks highest enabled interrupt Software Interrupt Set/Cleared by Software Interrupt identity specified with ID line CPU can disable all interrupts with internal flag Non- M askable I nterrupt line (NMI) can’t be disabled Network IntID Interrupt Interrupt Mask Control Software Interrupt NMI CPU Priority Encoder Timer Int Disable
Sys t em Calls Syste m ca ll s are th e w a y i n w h i ch user pro g r a ms request act i o ns fro m th e k e r ne l A l m ost a l w a y s , th e y represent contro ll e d access to p r i v il e ge d ope r at i ons If so m eth i n g ca n b e don e w i th reasona b l e effic i ency pure l y at user l e v e l , i t shou l d not b e a syste m ca l l Th e k e r ne l i s entere d v i a a soft w are i nter r upt
A d d i n g a Sys t em Call W r i te th e cod e Download\Extract the kernel source R out i nes are gene r a ll y na m e d sy s xxx To define a new system call sys_hello (), Create a directory hello in the kernel source directory( / usr / src /linux-3.16/) If i t ’ s i n a n e w fi l e , ad d th e fi l ena me to th e approp r i ate M a k e fi l e Add the new system call ( sys_hello () ) into the system call table (syscall_32.tbl file A d d th e sysca l l n u m ber to li n ux/sysca ll s .h W r i te th e C li nkag e rout i n e
A Rei mp le m e nt a t i o n o f getpid() #includ e <asm/unistd.h > asmlinkag e lon g sys_mypid(void ) { retur n current->tgid ; } “ asmlinkage ” is a key word used to indicate that all parameters of the function would be available on the stack. #include < linux / kernel.h > asmlinkage long sys_hello (void) { printk (“Hello world\n”); return 0; }
Si mp le User L i n k a g e #defin e NR_mypi d 29 4 syscall0(long , mypid ) in t main( ) { printf("%d\n" , mypid()) ; retur n ; } syscall i s f or a syste m c all w i th n o argu m ents; there are al so syscall 1 , syscall 2 , . . . , syscall 6 #include < stdio.h > #include < linux / kernel.h > #include <sys/ syscall.h > #include < unistd.h > int main() { long int amma = syscall (354); printf (“System call sys_hello returned %ld\n”, amma ); return 0; }
#include < stdio.h > # include < pthread.h > /* thread function definition*/ void * threadFunction ( void * args ) { while (1 ) { printf ("I am threadFunction .\n"); } } int main() { /* creating thread id*/ pthread_t id ; int ret ; /* creating thread*/ ret = pthread_create (& id ,NULL,& threadFunction ,NULL ); if (ret ==0){ printf ("Thread created successfully.\n"); } else { printf ("Thread not created.\n"); return 0; /*return from main*/ } while (1 ) { printf ("I am main function.\n"); } return 0; }
If file name is main.c while output file name is main To compile $ gcc main.c -o main - lpthread To run $ ./main
Thread created successfully. I am threadFunction . I am threadFunction . I am threadFunction . I am threadFunction . … … I am threadFunction . I am main function. I am main function. I am main function. I am main function. … … I am main function. I am threadFunction . … and so on.