24.01.2014 Views

Codice

Codice

Codice

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

274 volume VI os16<br />

Script e sorgenti del kernel 275<br />

«<br />

1750019 | proc_table[pid].wakeup_timer--;<br />

1750020 | if (proc_table[pid].wakeup_timer == 0)<br />

1750021 | {<br />

1750022 | proc_table[pid].status = PROC_READY;<br />

1750023 | }<br />

1750024 | }<br />

1750025 | }<br />

1750026 | }<br />

1750027 | previous_time = current_time;<br />

1750028 |}<br />

104.9.11 kernel/proc/proc_scheduler.c<br />

Si veda la sezione 103.8.11.<br />

1760001 |#include <br />

1760002 |#include <br />

1760003 |#include <br />

1760004 |//----------------------------------------------------------------------<br />

1760005 |extern uint16_t _ksp;<br />

1760006 |//----------------------------------------------------------------------<br />

1760007 |void<br />

1760008 |proc_scheduler (uint16_t *sp, segment_t *segment_d)<br />

1760009 |{<br />

1760010 | //<br />

1760011 | // The process is identified from the data and stack segment.<br />

1760012 | //<br />

1760013 | pid_t prev;<br />

1760014 | pid_t next;<br />

1760015 | //<br />

1760016 | static unsigned long int previous_clock;<br />

1760017 | unsigned long int current_clock;<br />

1760018 | //<br />

1760019 | // Check if current data segments are right.<br />

1760020 | //<br />

1760021 | if (es () != ds () || ss () != ds ())<br />

1760022 | {<br />

1760023 | k_printf ("\n");<br />

1760024 | k_printf ("Kernel panic: ES, DS, SS are different!\n");<br />

1760025 | k_exit (0);<br />

1760026 | }<br />

1760027 | //<br />

1760028 | // Search the data segment inside the process table.<br />

1760029 | // Must be done here, because the subsequent call to<br />

1760030 | // proc_sch_signals() will remove the segment numbers<br />

1760031 | // from a zombie process.<br />

1760032 | //<br />

1760033 | prev = proc_find (*segment_d);<br />

1760034 | //<br />

1760035 | // Take care of sleeping processes: wake up if sleeping time<br />

1760036 | // elapsed.<br />

1760037 | //<br />

1760038 | proc_sch_timers ();<br />

1760039 | //<br />

1760040 | // Take care of pending signals.<br />

1760041 | //<br />

1760042 | proc_sch_signals ();<br />

1760043 | //<br />

1760044 | // Take care input from terminals.<br />

1760045 | //<br />

1760046 | proc_sch_terminals ();<br />

1760047 | //<br />

1760048 | // Update the CPU time usage.<br />

1760049 | //<br />

1760050 | current_clock = k_clock ();<br />

1760051 | proc_table[prev].usage += current_clock - previous_clock;<br />

1760052 | previous_clock = current_clock;<br />

1760053 | //<br />

1760054 | // Scan for a next process.<br />

1760055 | //<br />

1760056 | for (next = prev+1; next != prev; next++)<br />

1760057 | {<br />

1760058 | if (next >= PROCESS_MAX)<br />

1760059 | {<br />

1760060 | next = -1; // At the next loop, ‘next’ will be zero.<br />

1760061 | continue;<br />

1760062 | }<br />

1760063 | if (proc_table[next].status == PROC_EMPTY)<br />

1760064 | {<br />

1760065 | continue;<br />

1760066 | }<br />

1760067 | else if (proc_table[next].status == PROC_CREATED)<br />

1760068 | {<br />

1760069 | continue;<br />

1760070 | }<br />

1760071 | else if (proc_table[next].status == PROC_READY)<br />

1760072 | {<br />

1760073 | if (proc_table[prev].status == PROC_RUNNING)<br />

1760074 | {<br />

1760075 | proc_table[prev].status = PROC_READY;<br />

1760076 | }<br />

1760077 | proc_table[prev].sp = *sp;<br />

1760078 | proc_table[next].status = PROC_RUNNING;<br />

1760079 | proc_table[next].ret = 0;<br />

1760080 | *segment_d = proc_table[next].segment_d;<br />

1760081 | *sp = proc_table[next].sp;<br />

1760082 | break;<br />

1760083 | }<br />

1760084 | else if (proc_table[next].status == PROC_RUNNING)<br />

1760085 | {<br />

1760086 | if (proc_table[prev].status == PROC_RUNNING)<br />

1760087 | {<br />

1760088 | k_printf ("Kernel alert: process %i ", prev);<br />

1760089 | k_printf ("and %i \"running\"!\r", next);<br />

1760090 | proc_table[prev].status = PROC_READY;<br />

1760091 | }<br />

1760092 | proc_table[prev].sp = *sp;<br />

1760093 | proc_table[next].status = PROC_RUNNING;<br />

1760094 | proc_table[next].ret = 0;<br />

1760095 | *segment_d = proc_table[next].segment_d;<br />

1760096 | *sp = proc_table[next].sp;<br />

1760097 | break;<br />

1760098 | }<br />

1760099 | else if (proc_table[next].status == PROC_SLEEPING)<br />

1760100 | {<br />

1760101 | continue;<br />

1760102 | }<br />

1760103 | else if (proc_table[next].status == PROC_ZOMBIE)<br />

1760104 | {<br />

1760105 | continue;<br />

1760106 | }<br />

1760107 | }<br />

1760108 | //<br />

1760109 | // Check again if the next process is set to running, otherwise set<br />

1760110 | // the kernel to such value!<br />

1760111 | //<br />

1760112 | next = proc_find (*segment_d);<br />

1760113 | if (proc_table[next].status != PROC_RUNNING)<br />

1760114 | {<br />

1760115 | proc_table[0].status = PROC_RUNNING;<br />

1760116 | *segment_d = proc_table[0].segment_d;<br />

1760117 | *sp = proc_table[0].sp;<br />

1760118 | }<br />

1760119 | //<br />

1760120 | // Save kernel stack pointer.<br />

1760121 | //<br />

1760122 | _ksp = proc_table[0].sp;<br />

1760123 | //<br />

1760124 | // At the end, must inform the PIC 1, with message «EOI».<br />

1760125 | //<br />

1760126 | out_8 (0x20, 0x20);<br />

1760127 |}<br />

104.9.12 kernel/proc/proc_sig_chld.c<br />

Si veda la sezione 103.8.12.<br />

1770001 |#include <br />

1770002 |//----------------------------------------------------------------------<br />

1770003 |void<br />

1770004 |proc_sig_chld (pid_t parent, int sig)<br />

1770005 |{<br />

1770006 | pid_t child;<br />

1770007 | //<br />

1770008 | // Please note that ‘sig’ should be SIGCHLD and nothing else.<br />

1770009 | // So, the following test, means to verify if the parent process<br />

1770010 | // has received a SIGCHLD already.<br />

1770011 | //<br />

1770012 | if (proc_sig_status (parent, sig))<br />

1770013 | {<br />

1770014 | if ( (!proc_sig_ignore (parent, sig))<br />

1770015 | && (proc_table[parent].status == PROC_SLEEPING)<br />

1770016 | && (proc_table[parent].wakeup_events & WAKEUP_EVENT_SIGNAL)<br />

1770017 | && (proc_table[parent].wakeup_signal == sig))<br />

1770018 | {<br />

1770019 | //<br />

1770020 | // The signal is not ignored from the parent process;<br />

1770021 | // the parent process is sleeping;<br />

1770022 | // the parent process is waiting for a signal;<br />

1770023 | // the parent process is waiting for current signal.<br />

1770024 | // So, just wake it up.<br />

1770025 | //<br />

1770026 | proc_table[parent].status = PROC_READY;<br />

1770027 | proc_table[parent].wakeup_events ^= WAKEUP_EVENT_SIGNAL;<br />

1770028 | proc_table[parent].wakeup_signal = 0;<br />

1770029 | }<br />

1770030 | else<br />

1770031 | {<br />

1770032 | //<br />

1770033 | // All other cases, means to remove all dead children.<br />

1770034 | //<br />

1770035 | for (child = 1; child < PROCESS_MAX; child++)<br />

1770036 | {<br />

1770037 | if ( proc_table[child].ppid == parent<br />

1770038 | && proc_table[child].status == PROC_ZOMBIE)<br />

1770039 | {<br />

1770040 | proc_available (child);<br />

1770041 | }<br />

1770042 | }<br />

1770043 | }<br />

1770044 | proc_sig_off (parent, sig);<br />

1770045 | }<br />

1770046 |}<br />

104.9.13 kernel/proc/proc_sig_cont.c<br />

Si veda la sezione 103.8.13.<br />

1780001 |#include <br />

1780002 |//----------------------------------------------------------------------<br />

1780003 |void<br />

1780004 |proc_sig_cont (pid_t pid, int sig)<br />

1780005 |{<br />

1780006 | //<br />

1780007 | // The value for argument ‘sig’ should be SIGCONT.<br />

1780008 | //<br />

1780009 | if (proc_sig_status (pid, sig))<br />

1780010 | {<br />

1780011 | if (proc_sig_ignore (pid, sig))<br />

1780012 | {<br />

1780013 | proc_sig_off (pid, sig);<br />

1780014 | }<br />

1780015 | else<br />

1780016 | {<br />

1780017 | proc_table[pid].status = PROC_READY;<br />

1780018 | proc_sig_off (pid, sig);<br />

1780019 | }<br />

1780020 | }<br />

1780021 |}<br />

104.9.14 kernel/proc/proc_sig_core.c<br />

Si veda la sezione 103.8.14.<br />

1790001 |#include <br />

1790002 |//----------------------------------------------------------------------<br />

1790003 |void<br />

1790004 |proc_sig_core (pid_t pid, int sig)<br />

1790005 |{<br />

«<br />

«<br />

«

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!