全部博文(92)
分类: 嵌入式
2009-08-24 11:22:20
Reference: "Linux Application Development "
Reference Site:
Process: It is in the implementation phase of the program, as well as the general term for the resources it contains.
Thread: It is in the process campaigns. Each thread has a separate program counter, process stack and a set of process registers. Scheduling is targeted at the kernel thread, not process.
Process descriptor structure: task_struct, defined in
thread_info structure in the file
struct thread_info (
struct task_struct * task;
struct exec_domain * exec_domain;
unsigned long flags;
unsigned long status;
__u32 cpu;
__s32 preempt_count;
mm_segment_t addr_limit;
struct restart_block restart_block;
unsigned long previous_esp;
__u8 supervisor_stack [0];
);
Process Status:
TASK_RUNNING (Run): no matter the process is being occupied by CPU or not, as long as the process has the operating conditions, is in the run state. Linux organize all the PCB in that state into a run queue run_queue, scheduler select process to run from this queue. In fact, Linux merges ready state and running state into one state.
TASK_INTERRUPTIBLE (interruptible blocking): Linux will be divided into blocking state TASK_INTERRUPTIBLE, TASK_UNINTERRUPTIBLE, TASK_STOPPED three different state. In TASK_INTERRUPTIBLE state resources to effectively process was awakened, can also signal or interrupt wake-up timer.
TASK_UNINTERRUPTIBLE (blocking can not be interrupted): Another kind of blocking state, in the process of the state only when the resources are effectively was awakened, can not signal or interrupt wake-up timer.
TASK_STOPPED (pause): state of the third block, the process is stopped, usually by receiving a signal (SIGSTOP, SIGTSTP, SIGTTIN, or SIGTTOU). The process is being debugged may be stagnating. The process in this state only be waked up through the other process signal.
TASK_ZOMBILE (zombie): process has been completed but not yet dead, has released most of its resources, PCB has not yet been released, the data in the task to retain the structure task_struct. Once the parent process called wait4 (), the process descriptor will be released.
Set the current process status:
set_task_state (task, state);
Get process descriptor:
e.g: get the parent process, process descriptor:
struct task_struct * my_parent = current-> parent;
e.g: turn to visit the child process:
struct task_struct * task;
struct list_head * list;
list_for_each (list, & current-> children) (
task = list_entry (list, struct task_struct, sibling);
)
e.g: access list in the next process:
list_entry (task-> tasks.next, struct task_struct, tasks)
Access list in the previous process:
list_entry (task-> tasks.prev, struct task_struct, tasks)
Macro for_each_process (task), providing in turn the ability to access the entire task queue,
e.g:
struct task_struct * task;
for_each_process (task) (
printk ( "% s [% d] \ n", task-> comm, task-> pid);
)
Process creation:
# include
# include
pid_t fork (void);
fork function call time I return twice, in the parent process to return the child process ID, in the child process returned 0.
For example:
# include
# include
main () (
pid_t pid;
pid = fork ();
if (! pid)
....
else if (pid> 0)
...
else
printf ( "fork fail");
)
The process of implementation:
# include
# include
int execl (const char * path, const char * arg0 ,...);
For example:
execl ( "/ bin / ls", "ls", "-l", NULL)
# include
# include
pid_t wait (int * status);
pid_t waitpid (pid_t pid, int * status, int options);
wait () : as long as the calling process is the child process, they sleep until one of them terminated.
unsigned int sleep (unsigned int seconds)
sleep () make the task hang for some time, until the designated time runs out or subjected to the signal.
# include
# include
int system (string)
The process of dying:
# include
void exit (int status);
exit () is used to end the current process of implementation, and return the status parameters to the parent process.
Process-related system calls:
1.setuid and setgid system calls
# include
int setuid (uid_t uid);
int setgid (gid_t gid);
setuid () set a real and effective user ID for current process.
setgid () allow the process set a real and effective ID for another process as gid.
2.setpgrp and setpgid system calls
int setpgrp (void);
int setpgid (pid_t pid, pid_t pgid);
setpgrp() used to set the same process group ID with the process pid for the calling process
setpgid() used to set the process group ID for the process whose process ID is pgid