知识一点一点的积累,如果有前辈看有错误的地方请指出,我好尽快改正
进程的四要素:
1,首先要有一段程序供进程运行,也就是说,就像一个剧本一样,而剧本可以有很多剧团一起用,一起来参考剧本演戏。
2。进程都有起码的“私有财产”,就是说每个进程都会有进程专用的系统堆栈空间。
3。每个进程都会有一个相应的数据结构,也就是PCB(进程控制块),其数据结构如下:
Linux/include/linux/sched.h 中
801 struct task_struct { 802 volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped 描述进程状态的*/ 803 struct thread_info *thread_info; 804 atomic_t usage; 805 unsigned long flags; /* 描述进程标志的per process flags, defined below */ 806 unsigned long ptrace; 807 808 int lock_depth; /* BKL lock depth */ 809 810 #ifdef CONFIG_SMP 811 #ifdef __ARCH_WANT_UNLOCKED_CTXSW 812 int oncpu; 813 #endif 814 #endif 815 int load_weight; /* for niceness load balancing purposes */ 816 int prio, static_prio, normal_prio; 817 struct list_head run_list; 818 struct prio_array *array; 819 820 unsigned short ioprio; 821 #ifdef CONFIG_BLK_DEV_IO_TRACE 822 unsigned int btrace_seq; 823 #endif 824 unsigned long sleep_avg; 825 unsigned long long timestamp, last_ran; 826 unsigned long long sched_time; /* sched_clock time spent running */ 827 enum sleep_type sleep_type; 828 829 unsigned long policy; 830 cpumask_t cpus_allowed; 831 unsigned int time_slice, first_time_slice; 832 833 #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT) 834 struct sched_info sched_info; 835 #endif 836 837 struct list_head tasks; 838 /* 839 * ptrace_list/ptrace_children forms the list of my children 840 * that were stolen by a ptracer. 841 */ 842 struct list_head ptrace_children; 843 struct list_head ptrace_list; 844 845 struct mm_struct *mm, *active_mm; 846 847 /* task state */ 848 struct linux_binfmt *binfmt; 849 long exit_state; 850 int exit_code, exit_signal; 851 int pdeath_signal; /* The signal sent when the parent dies */ 852 /* ??? */ 853 unsigned long personality; 854 unsigned did_exec:1; 855 pid_t pid;/*进程id*/ 856 pid_t tgid; 857 858 #ifdef CONFIG_CC_STACKPROTECTOR 859 /* Canary value for the -fstack-protector gcc feature */ 860 unsigned long stack_canary; 861 #endif 862 /* 863 * pointers to (original) parent process, youngest child, younger sibling, 864 * older sibling, respectively. (p->father can be replaced with 865 * p->parent->pid) 866 */ 867 struct task_struct *real_parent; /* real parent process (when being debugged) */ 868 struct task_struct *parent; /*父进程 parent process */ 869 /* 870 * children/sibling forms the list of my children plus the 871 * tasks I'm ptracing. 872 */ 873 struct list_head children; /* 子进程list of my children */ 874 struct list_head sibling; /* linkage in my parent's children list */ 875 struct task_struct *group_leader; /* threadgroup leader */ 876 877 /* PID/PID hash table linkage. */ 878 struct pid_link pids[PIDTYPE_MAX]; 879 struct list_head thread_group; 880 881 struct completion *vfork_done; /* for vfork() */ 882 int __user *set_child_tid; /* CLONE_CHILD_SETTID */ 883 int __user *clear_child_tid; /* CLONE_CHILD_CLEARTID */ 884 885 unsigned long rt_priority; 886 cputime_t utime, stime; 887 unsigned long nvcsw, nivcsw; /* context switch counts */ 888 struct timespec start_time; 889 /* mm fault and swap info: this can arguably be seen as either mm-specific or thread-specific */ 890 unsigned long min_flt, maj_flt; 891 892 cputime_t it_prof_expires, it_virt_expires; 893 unsigned long long it_sched_expires; 894 struct list_head cpu_timers[3]; 895 896 /* process credentials */ 897 uid_t uid,euid,suid,fsuid; 898 gid_t gid,egid,sgid,fsgid; 899 struct group_info *group_info; 900 kernel_cap_t cap_effective, cap_inheritable, cap_permitted; 901 unsigned keep_capabilities:1; 902 struct user_struct *user; 903 #ifdef CONFIG_KEYS 904 struct key *request_key_auth; /* assumed request_key authority */ 905 struct key *thread_keyring; /* keyring private to this thread */ 906 unsigned char jit_keyring; /* default keyring to attach requested keys to */ 907 #endif 908 /* 909 * fpu_counter contains the number of consecutive context switches 910 * that the FPU is used. If this is over a threshold, the lazy fpu 911 * saving becomes unlazy to save the trap. This is an unsigned char 912 * so that after 256 times the counter wraps and the behavior turns 913 * lazy again; this to deal with bursty apps that only use FPU for 914 * a short time 915 */ 916 unsigned char fpu_counter; 917 int oomkilladj; /* OOM kill score adjustment (bit shift). */ 918 char comm[TASK_COMM_LEN]; /* executable name excluding path 919 - access with [gs]et_task_comm (which lock 920 it with task_lock()) 921 - initialized normally by flush_old_exec */ 922 /* file system info */ 923 int link_count, total_link_count; 924 #ifdef CONFIG_SYSVIPC 925 /* ipc stuff */ 926 struct sysv_sem sysvsem; 927 #endif 928 /* CPU-specific state of this task */ 929 struct thread_struct thread; 930 /* filesystem information */ 931 struct fs_struct *fs; 932 /* open file information */ 933 struct files_struct *files; 934 /* namespaces */ 935 struct nsproxy *nsproxy; 936 /* signal handlers */ 937 struct signal_struct *signal; 938 struct sighand_struct *sighand; 939 940 sigset_t blocked, real_blocked; 941 sigset_t saved_sigmask; /* To be restored with TIF_RESTORE_SIGMASK */ 942 struct sigpending pending; 943 944 unsigned long sas_ss_sp; 945 size_t sas_ss_size; 946 int (*notifier)(void *priv); 947 void *notifier_data; 948 sigset_t *notifier_mask; 949 950 void *security; 951 struct audit_context *audit_context; 952 seccomp_t seccomp; 953 954 /* Thread group tracking */ 955 u32 parent_exec_id; 956 u32 self_exec_id; 957 /* Protection of (de-)allocation: mm, files, fs, tty, keyrings */ 958 spinlock_t alloc_lock; 959 960 /* Protection of the PI data structures: */ 961 spinlock_t pi_lock; 962 963 #ifdef CONFIG_RT_MUTEXES 964 /* PI waiters blocked on a rt_mutex held by this task */ 965 struct plist_head pi_waiters; 966 /* Deadlock detection and priority inheritance handling */ 967 struct rt_mutex_waiter *pi_blocked_on; 968 #endif 969 970 #ifdef CONFIG_DEBUG_MUTEXES 971 /* mutex deadlock detection */ 972 struct mutex_waiter *blocked_on; 973 #endif 974 #ifdef CONFIG_TRACE_IRQFLAGS 975 unsigned int irq_events; 976 int hardirqs_enabled; 977 unsigned long hardirq_enable_ip; 978 unsigned int hardirq_enable_event; 979 unsigned long hardirq_disable_ip; 980 unsigned int hardirq_disable_event; 981 int softirqs_enabled; 982 unsigned long softirq_disable_ip; 983 unsigned int softirq_disable_event; 984 unsigned long softirq_enable_ip; 985 unsigned int softirq_enable_event; 986 int hardirq_context; 987 int softirq_context; 988 #endif 989 #ifdef CONFIG_LOCKDEP 990 # define MAX_LOCK_DEPTH 30UL 991 u64 curr_chain_key; 992 int lockdep_depth; 993 struct held_lock held_locks[MAX_LOCK_DEPTH]; 994 unsigned int lockdep_recursion; 995 #endif 996 997 /* journalling filesystem info */ 998 void *journal_info; 999 1000 /* VM state */ 1001 struct reclaim_state *reclaim_state; 1002 1003 struct backing_dev_info *backing_dev_info; 1004 1005 struct io_context *io_context; 1006 1007 unsigned long ptrace_message; 1008 siginfo_t *last_siginfo; /* For ptrace use. */ 1009 /* 1010 * current io wait handle: wait queue entry to use for io waits 1011 * If this thread is processing aio, this points at the waitqueue 1012 * inside the currently handled kiocb. It may be NULL (i.e. default 1013 * to a stack based synchronous wait) if its doing sync IO. 1014 */ 1015 wait_queue_t *io_wait; 1016 /* i/o counters(bytes read/written, #syscalls */ 1017 u64 rchar, wchar, syscr, syscw; 1018 struct task_io_accounting ioac; 1019 #if defined(CONFIG_TASK_XACCT) 1020 u64 acct_rss_mem1; /* accumulated rss usage */ 1021 u64 acct_vm_mem1; /* accumulated virtual memory usage */ 1022 cputime_t acct_stimexpd;/* stime since last update */ 1023 #endif 1024 #ifdef CONFIG_NUMA 1025 struct mempolicy *mempolicy; 1026 short il_next; 1027 #endif 1028 #ifdef CONFIG_CPUSETS 1029 struct cpuset *cpuset; 1030 nodemask_t mems_allowed; 1031 int cpuset_mems_generation; 1032 int cpuset_mem_spread_rotor; 1033 #endif 1034 struct robust_list_head __user *robust_list; 1035 #ifdef CONFIG_COMPAT 1036 struct compat_robust_list_head __user *compat_robust_list; 1037 #endif 1038 struct list_head pi_state_list; 1039 struct futex_pi_state *pi_state_cache; 1040 1041 atomic_t fs_excl; /* holding fs exclusive resources */ 1042 struct rcu_head rcu; 1043 1044 /* 1045 * cache last used pipe for splice 1046 */ 1047 struct pipe_inode_info *splice_pipe; 1048 #ifdef CONFIG_TASK_DELAY_ACCT 1049 struct task_delay_info *delays; 1050 #endif 1051 #ifdef CONFIG_FAULT_INJECTION 1052 int make_it_fail; 1053 #endif 1054 };
|
上面的数据结构(也就是进程控制快)里包含了进程的各种信息。
4。有独立的存储空间,也就是进程的专有空间,除了前面说的系统空间堆栈空间还有用户空间堆栈。系统空间是不能独立的,任何进程都不能直接改变系统空间的内容(除了使用系统调用),除非是进程本身的系统空间堆栈以外。
如果缺少以上四个要素中的任何一个,那就不能叫进程了,如果缺了其中的第四个条件且具备其他三个要素的话,就应该称之为线程,线程分为内核线程和用户线程,内核线程指的是完全没有用户空间的,用户线程是指共享用户空间了。
阅读(1891) | 评论(2) | 转发(0) |