Processes and Process Control PowerPoint PPT Presentation

presentation player overlay
1 / 14
About This Presentation
Transcript and Presenter's Notes

Title: Processes and Process Control


1
Processes and Process Control
  • Anand Sivasubramaniam
  • Dept. of Computer Science Eng.
  • Pennsylvania State University

2
Process States
Creation
Stopped
Signal
Signal
Preempt
Ready
Zombie
Executing
Terminate
Schedule
I/O
I/O Done
Suspended
3
Task Structure (PCB)
  • Task_struct // in linux/sched.h
  • Volatile long state
  • Long counter
  • Long priority
  • Unsigned long signals // pending sigs
  • Unsigned long blocked //masked sigs
  • Int pid, pgrp, uid, euid, gid, egid
  • Struct linux_binfmt
  • Struct task_struct p_opptr // ptr to original
    parent
  • Struct task_struct p_pptr // ptr to
    immediate parent
  • Struct task_struct p_cptr // ptr to most
    recent child
  • Struct task_struct p_ysptr // ptr to
    following sibling
  • Struct task_struct p_osptr // ptr to previous
    sibling
  • Struct task_struct next_task // in process
    list
  • Struct task_struct prev_task // in process
    list
  • Struct task_struct next_run // in ready queue
  • Struct task_struct prev_run //in ready queue

4
  • Struct mm_struct mm1
  • Unsigned long kernel_stack_page
  • Unsigned long saved_kernel_stack
  • Struct fs_struct fs1
  • Long utime, stime, cutime, cstime, start_time
  • Struct sem_queue semsleeping
  • Struct wait_queue wait_chldexit
  • Struct sigaction sigaction32
  • Struct rlimit rlimRLIM_NLIMITS
  • Struct thread_struct tss // includes saved
    registers
  • Unsigned long policy // SCHED_FIFO,SCHED_RR,SCHE
    D_OTHER
  • Unsigned long rt_priority
  • // for SMPs
  • Int processor, last processor
  • Int lock_depth

5
  • Struct task_struct init_task // Task structure
    for init
  • The list of tasks is a doubly-linked circular
    list.
  • Struct task_struct current // current task
  • In SMPs, there is task_struct current_setNR_CPUS
  • Earlier versions used a simple task table, and
    task0 is INIT_TASK

6
Queues and Semaphores
  • Circular list
  • Struct wait_queue
  • Struct task_struct task
  • Struct wait_queue nest
  • Access only using
  • Void add_wait_queue(struct wait_queue , struct
    wait_queue )
  • Void remove_wait_queue(struct wait_queue ,
    struct wait_queue )

7
  • Sleep and wakeup implementation using wait queues
  • void sleep_on(struct wait_queue queue)
  • add_wait_queue(queue, current)
  • schedule()
  • remove_wait_queue(queue, current)
  • void wake_up(struct wait_queue queue)
  • struct wait_queue p
  • do
  • P-gttask-gtstate TASK_RUNNING
  • P p-gt next
  • while (p!queue)
  • Exercise Implement Semaphores

8
Process Creation Using fork()
  • Clone flags CLONE_VM, CLONE_FS, CLONE_FILES,
    CLONE_SIGHAND, CLONE_PID
  • do_fork(clone_flags, unsigned long usp, struct
    pt_regs regs)
  • unsigned long new_stack
  • struct task_struct p
  • p (struct task_struct ) kmalloc(sizeof(p),
    GFP_KERNEL)
  • new_stack alloc_kernel_stack()
  • nr find_empty_process()
  • tasknr p
  • p current // inherit parents task struct

9
  • P-gtdid_exec 0
  • P-gtswappable 0
  • P-gtpid get_pid(clone_flags)
  • P-gtnext_run p-gtprev_run NULL
  • P-gtstart_time jiffies
  • P-gtswappable 1
  • P-gtexit_signal clone_flags CSIGNAL
  • P-gtcounter current-gtcounter gtgt 1
  • Init_waitqueue(p-gtwait_chldexit)
  • Init_timer(p-gtreal_timer)
  • Copy_files(clone_flags,p)
  • Copy_fs(clone_flags,p)
  • Copy_sighand(clone_flags,p)
  • Copy_mm(clone_flags,p)
  • Copy_thread(nr,clone_flags,usp,p,regs) // sets
    regs, and
  • p-gttss.eip is set to ret_from_sys_call for
    return address
  • Wake_up_process(p) // puts in ready queue
  • Return p-gtpid

10
execve()
  • Static int do_execve(char filename, char argv,
    char envp, struct pt_regs regs)
  • .
  • for (fmt formats fmt fmt-gt next)
  • fmt-gtload_binary(.)
  • // find out if this is the loader else go to the
    next
  • Load_binary_format()
  • check magic number, format etc.
  • flush_old_exec(bprm) // releases old
    memory

11
  • current-gtmm-gtstart_code N_TXT_ADDR(ex)
  • current-gtmm-gtend_code current-gtmm-gtstart_code
    ex.a_text
  • current-gtmm-gtend_data current-gtmm-gtend_code
    ex.a_data
  • current-gtmm-gtstart_brk current-gtmm-gtend_data
  • current-gtmm-gtbrk ex.a_bss current-gtmm-gtstart_b
    rk
  • current-gtsuid bprm-gte_uid
  • current-gteuid bprm-gte_uid
  • fd open(bprm-gtinode,O_RDONLY)
  • file current-gtfiles-gtfdfd
  • do_mmap(file,N_TEXTADDR(ex),ex.a_text,PROT_READPR
    OT_EXEC, MAP_FIXED MAP_PRIVATE MAP_DENYWRITE
    MAP_EXECUTABLE, fd_offset)
  • do_mmap(file,N_TXTADDR(ex) ex.a_text,
    ex.a_data, PROT_READ PROT_WRITE PROT_EXEC,
    MAP_FIXED MAP_PRIVATE MAP_DENYWRITE
    MAP_EXECUTABLE, fd_offset ex.a_text)
  • sys_close(fd)
  • set_brk(current-gtmm-gtstart_brk,current-gtmm-gtbrk)
  • current-gtmm-gtstart_stack ..
  • start_thread(regs, ex.a_entry, p)

12
Linux Scheduling
  • 3 scheduling classes set by sched_setscheduler()
  • SCHED_FIFO and SCHED_RR are real-time classes
  • SCHED_OTHER is for the rest
  • When a process with higher real-time priority
    (rt_priority) wishes to run, all other processes
    with lower real-time priority are thrust aside.
  • In SCHED_FIFO, a process runs unil I reliquishes
    control or another with higher real-time priority
    wishes to run.
  • SCHED_RR process, in addition to this, is also
    interrupted when its time slice explires or there
    are processes of same real-time priority (RR
    between processes of this class)
  • Schedule() in kernel/sched.c implements the
    scheduler.
  • It is called when
  • Blocking call (sleep)
  • After every ret_from_sys_call if need_resched is
    on
  • After every slow interrupt if need_resched is on

13
  • schedule(void)
  • struct task_struct p, prev, next
  • prev current
  • next init_task
  • if (bh_active bh_mask)
  • intr_count 1
  • do_bottom_half()
  • intr_count 0
  • run_task_queue(tq_scheduler)
  • if (prev-gtstate ! TASK_RUNNING)
    del_from_runqueue(prev)
  • else if (prev-gtpolicy SCHED_RR
    prev-gtcounter 0)
  • Prev-gtcounter prev-gtpriority
  • Move_last_runqueue(prev)

14
  • Restart_reschedule // goodness calculation
  • next init_task
  • next_p 1000
  • for (pinit_task-gtnext_run p!init_task
    pp-gtnext_run)
  • if (p-gtpolicy ! SCHED_OTHER)
  • weight 1000 p-gtrt_priority
  • else weight p-gtcounter
  • if (weight gt next_p)
  • next_p weight next p
  • if (next_p 0)
  • for_each_task(p) // not just in Ready Queue
    !!!
  • p-gtcounter (p-gtcounter/2) p-gtpriority
  • goto restart_reschedule
  • if (prev ! next) switch_to(prev,next)
Write a Comment
User Comments (0)
About PowerShow.com