进程,线程

//=================================================================================================================================
[1] >> 进程?
<>-->><1>进程--运行中的程序--前台进程(./a.out)+后台进程(./a.out &)  #1:守护进程是后台进程,随linux系统启动而启动,关闭而关闭
<>-->><2>进程动画演示
[
					 [a.out] #程序
		./a.out  --> /     \  <--./a.out &   #后台进程不能接收前台的任何数据
				   ↙       ↘
			会话1		       会话2
		 |----------|		|----------| 
		 |	 前台	|		|	前台   |	 
		 |	 a.out	|		|		   |
		 |==========|		|==========|	 
		 |⇳		 fg⇧|		|⇳		fg⇧|
		 |⇩bg	   ⇳|		|⇩bg	  ⇳|	 
		 |==========|		|==========|
		 |			|		|  a.out & |	 
		 |	 后台	|		|	后台   |
		 |----------|		|----------| 
#a.out --> 终端1前台运行 --> 进程1
#a.out --> 终端2后台运行 --> 进程2
#a.out --> 多个终端同时运行 --> 多个进程
]
<>-->><3>进程的6种状态  
[
											[可中断的睡眠状态 = S]   [不可中断的睡眠状态 = D]
									 						↖				↗
												  		     [睡眠态=挂起态][停止态 = T =ctrl+z]
																	|[就绪态] --> [R = runing状态] 
																	↓		↘
																[X-退出状态 = ctrl+c]  [僵尸态 = Z]  
]
<>-->><4>PCB #process control block 
<>-->><5>PID #Process Identification  
<>-->><6>ps aux #查看前台进程 
<>-->><7>top
<>-->><8>进程优先级[-20最大,19最小] #数值越低<-->进程优先级越高,进程默认0,一般取大于0   
[
	<x>nice -n 2 ./a.out #优先级2运行a.out  
	<x>renice -n 2 6655  #修改6655进程优先级为2  
]
<>-->><9>jobs  #查看后台进程 
<>-->><10>fg  #后台进程 --> 前台  
<>[暂停进程-ctrl+z,由于后台不能接收前台数据,所以后台进程暂停需要先fg为前台进程,再bg为后台进程]
<>-->><11>bg  # 前台进程 --> 后台
<>-->><12>pid_t fork(void); #create a child process [return:-1-->error]
[
	the PID of the child process is returned in the parent,
    0 is returned in the child
	子进程号是父进程号加1
	进程号是pid_t 类型
]
<>-->><13>pid_t getpid(void); # returns the process ID of the calling process(calling-当前)
<>-->><14>pid_t getppid(void); #returns the process ID of the parent of the calling process
<>-->><15>孤儿进程 #父进程先结束死亡,子进程变成后台进程,由init进程管理 
<x>-->><16>僵尸进程 #子进程先结束死亡,而父进程又没有回收子进程 
<x>-->><17>void exit(int status); #结束进程资源,value of status & 0377 is returned to the parent
[
	注意exit(77);父进程接收到的不是77,而是&0377的数值,所以除非对这个值需要判断计算,其他时候不接收该值
]
<>-->><18>pid_t wait(int *stat_loc); #回收子进程;子进程exit返回的status,传递给父进程的stat_loc
[
	# on success, returns the process ID of the terminated child; on error, -1 is returned.   
	# 如果父进程下有多个子进程,就需要调用多次wait来回收,wait优先回收先结束的子进程
	<x>WIFEXITED(status) #w+if+exited-- returns true if the child terminated normally   
	<x>WEXITSTATUS(status) #w+exit+status--returns the exit status of the child   
	<x>WIFSIGNALED(status) #w+if+signaled --returns true if the child process was terminated by a signal.   
	<x>WTERMSIG(status) #w+term+sig --returns the number of the signal that caused the child process to terminate.  
}
<x>-->><19>pid_t waitpid(pid_t pid, int *status, int options); #recycle(回收) child process   [return:-1-->error;child process PID] 
[
	pid
		>0 回收指定ID的进程
       -1 回收任意进程(相当于wait)
        0 回收和当前调用waitpid一个组的所有进程
	
	status--子进程的结束状态值会由参数 status 返回
		<x>WIFEXITED(status) #w+if+exited-- returns true if the child terminated normally   
		<x>WEXITSTATUS(status) #w+exit+status--returns the exit status of the child   
		<x>WIFSIGNALED(status) #w+if+signaled --returns true if the child process was terminated by a signal.   
		<x>WTERMSIG(status) #w+term+sig --returns the number of the signal that caused the child process to terminate. 
		
	options--提供了一些额外的选项来控制waitpid
		0--不想使用它们
		WNOHANG--若pid指定的子进程没有结束,则waitpid()函数返回0,不予以等待
		WUNTRACED--若子进程进入暂停状态,则马上返回
		
	如果执行成功则返回子进程识别码(PID) ,如果有错误发生则返回返回值-1。失败原因存于 errno 中。

]
//=================================================================================================================================
[2] >> exec函数族?   
[
	功能----如果一个进程想执行另一个程序,那么它就可以调用fork函数新建一个进程,然后调用exec函数族中的任意一个函数,这样看起来就像通过执行应用程序而产生了一个新进程(这种情况非常普遍)。
	
	exec函数族共有6种不同形式的函数。这6个函数可以划分为两组:
	(1)execl、execle和execlp。
	(2)execv、execve和execvp。
	第一组是l.称为execl系列,l是list(列表)的意思,表示execl系列函数需要将每个命令行参数作为函数的参数进行传递;
	第二组是v,称为execv系列,v是vector(矢量)的意思,表示execv系列函数将所有函数包装到一个矢量数组中传递即可。
	exec函数的原型如下:
	int execl(const char * path,const char * arg,…)int execle(const char * path,const char * arg,char * const envp[])int execlp(const char * file,const char * arg,…)int execv(const char * path,char * const argv[])int execve(const char * path,char * const argv[]char * const envp[])int execvp(const char * file,char * const argv[]);

	参数说明:
	path:带路径名的文件名。可以是绝对路径或者是相对路径
	file:要执行的程序名称。系统将根据PATH环境变量指定的路径顺序搜索指定的文件。
	argv:命令行参数的矢量数组。
	envp:带有该参数的exec函数可以在调用时指定一个环境变量数组。	
	arg:程序的第0个参数,即程序名自身。相当于argv[O]。
	…:命令行参数列表,在所有命令行参数的最后应该增加一个空的参数项(NULL),表明命令行参数结束。
	返回值:一1表明调用exec失败,无返回表明调用成功。 
]
//=================================================================================================================================
[3] >> system函数
<>-->> int system(const char *command); #execute a shell command  [return:-1-->error]
//=================================================================================================================================
[4]会话与进程组,进程三者的关系
[
			    	  [会话1]终端1			        	 [会话2]终端2
			    ↙		⇣		↘					↙		⇣		↘	
			[进程组1][进程组2][进程组3]			[进程组1][进程组2][进程组3]
			[父进程1][父进程2][父进程3]			[父进程1][父进程2][父进程3]	
			[子进程1][子进程2][子进程3]			[子进程1][子进程2][子进程3]		
			[孙进程1][孙进程2][孙进程3]			[孙进程1][孙进程2][孙进程3]		
	#进程 < 进程组 < 会话
	#每个父进程被分配到一个进程组,父进程下的子进程也属于这个进程组 
	#终端 = 会话 = 多个进程组	
]
<x>2>>pid_t setsid(void); 
[
		On success, the (new) session ID of the calling  process  is  returned.On  error,  (pid_t) -1  is  returned, 
		setsid() creates a new session if the calling process is not a process group leader. The calling process is the leader of the new session
		#这个孤儿进程依然依附于该终端所打开的会话,需要在孤儿进程中自己setsid();创建一个会话
		#这样孤儿进程是该会话的第一个进程也就是会话组长
]
<x>3>>mode_t umask(mode_t mask); //设置守护进程的文件操作权限 
[
	umask() sets the calling process's file mode creation mask (umask) to mask & 0777 
]
<x>4>>int getdtablesize(void); //获取打开的文件描述符 
[
	 getdtablesize - get file descriptor 
	 getdtablesize() returns the maximum number of files a process can have open
]
//=================================================================================================================================
[5] >> 进程间通信方式--管道pipe/fifo
[
	无名管道和有名管道是Linux系统内核的特殊文件,用于进程之间的通信。
	无名管道相当于一个队列结构,fd[1]为写入端(入队),fd[0]为读出端(出队)。其中信息读出后即删除,再次读取时即为下一个信息。

	函数形式:int pipe(int fd[2])
	功能--创建无名管道文件。无名管道是一个特殊文件,不可由open函数创建。
	参数:fd[2]有两个成员 fd[0]和 fd[1],他们都是文件描述符。 管道有固定的读端 fd[0]和固定的写端 fd[1]。
	返回值:成功返回 0,出错返回-1。

	说明:
	1.管道是创建在内存中,进程结束空间释放,管道不复存在。
	2.无名管道和有名管道都是半双工通信,实现双向通信需要建立两个管道。
	3.无名管道只用于父子进程之间,有名管道可用于无亲缘关系的进程之间。
	
	int mkfifo(const char *pathname, mode_t mode);[return:-1-->error;0-->success] 
		mkfifo ()会依参数pathname建立特殊的FIFO文件,该文件必须不存在,而参数mode为该文件的权限
]
//=================================================================================================================================
[7] >> 进程间通信方式3--信号
注意:程序接收到信号后,程序会暂停执行,转去响应信号,然后再继续执行程序
<x>-->><1>kill -l 查看信号类型 #linux有64种信号 
[
	所有常见到的信号:
	
<x>	SIGNUP–中断关闭时触发–>进程终止 
<x>	SIGINT–ctrl+c触发该信号–>终止进程(为什么ctrl+c能关闭进程的原因) 
<x>	SIGQUIT–ctrl+\触发该信号–>终止进程(与ctrl+c效果一致) 
<x>	SIGILL–执行了非法指令时触发–>终止进程
<x>	SIGSEV–内存溢出,野指针时触发–>终止进程(经常看见段错误后,进程就强制退出的原因) 
<x>	SIGPIPE–写入管道错误触发–>终止进程 
<x>	SIGKILL–用来关闭进程(进程被关闭的原因) 
<x>	SIGSTOP–暂停进程(gdb调试能设置断点暂停进程的原因) 
<x>	SIGTSTP–ctrl+z–暂停进程 
<x>	SIGCONT–继续运行进程(GDB调试,能继续运行程序的原因)
<>	SIGALRM–定时器信号;定时器时间到了后会让进程结束 
<>	SIGUSR1/2–用户可以使用的两个信号
]
-->><2>信号处理方式
[
	缺省方式 (默认中断处理方式)
	忽略该信号
	捕捉信号 (用户中断处理函数)
]
<>-->><3>kill - <信号编号> <进程号>  #终端先进程发送信号指令  #例1:kill -19 6688 ,6688进程发送信号19 
<>-->><4>killall - <信号编号> <名称> #例1:kill -19  a.out ,向a.out所生成的所有进程发送信号19 
<>-->><5>int kill(pid_t pid, int sig); [return:-1-->error;0-->success]
<>-->><6>unsigned int alarm(unsigned int seconds); #set an alarm clock for delivery(交互) of a signal 
[
	Description : #时间到了就向该进程发送闹钟信号
	alarm() arranges for a SIGALRM signal to be delivered(递送) to the calling process in seconds seconds.
	
	Return Value :
	alarm() returns the number of seconds remaining(剩余) until any previously scheduled(分配) alarm was due(到期) 
	to be delivered(递送), or zero if there was no previously scheduled alarm.
]
<>-->><7>int pause(void);[return:-1-->error] 
[
	pause() causes the calling process (or thread) to sleep 
	until a signal is delivered that either terminates(结束) the process or causes the invocation of a signal-catching function.
]
<x>-->><8>sighandler_t signal(int signum, sighandler_t handler);
[
	注意:typedef void (*sighandler_t)(int);
	
	signal() sets the disposition(处理) of the signal signum to handler, which is either SIG_IGN(忽略方式), SIG_DFL(缺省方式), or the address of a programmer-defined function
	<>SIG_IGN = ignored(忽略)
	<>SIG_DFL = default(默认)
	The signals SIGKILL and SIGSTOP cannot be caught(捕捉) or ignored(忽略).
	signal() returns the previous value of the signal handler, or SIG_ERR on error.
]
//=================================================================================================================================
//=================================================================================================================================
[8] >> 线程thread ?
-->><><1>TID = Thread identify 
-->><x><2>int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);  
[

	 discription : The pthread_create() function starts a new thread in the calling process.
	1:The attr argument points to a pthread_attr_t structure whose contents are used at thread creation time 
	to determine(决定) attributes(属性) for the new thread; 
	2:The new thread starts execution(执行) by invoking(唤醒) start_routine(); 
	3:arg is passed as the sole(装有的) argument of start_routine().

	return:On success, pthread_create() returns 0; on error, it returns an error number
]

-->><x><3>void pthread_exit(void *retval); 
#The pthread_exit() function terminates the calling thread and returns a value via(通过) retval 

-->><x><4> int pthread_join(pthread_t thread, void **retval);
[
 #discription : 
 The pthread_join() function waits for the thread specified by thread to terminate.  If that thread has already terminated, then pthread_join()  returns immediately.
 #node:
 the value that the target thread supplied to pthread_exit(3)) into the location pointed to by *retval.
 On success, pthread_join() returns 0; on error, it returns an error number.
]
-->><5>synchronization(同步) mass thread
[
	1>>多线程采用共享资源采用全局变量(对共享数据进行同步机制,可以保证进程有序使用共享资源)
	2>>synchronization:多个任务按照先后次序操作线程间的公共资源,信号量就是一种同步机制,决定线程是运行还是等待
	
	-->>信号量的使用步骤
	[
		<x>1>>int sem_init(sem_t *sem, int pshared, unsigned int value);[return:-1-->error;0-->success] #同步信号量在进程创建之前初始化 
		[
			discription:
			sem_init() initializes the unnamed semaphore(信号标) at the address pointed to by sem.  The value argument specifies the initial value for the semaphore.
			pshared:
			If pshared has the value 0, then the semaphore is shared between the threads of a process
			If pshared is nonzero, then the semaphore is shared between processes
		}
		<>2>>int sem_wait(sem_t *sem);[return:0-->success;-1-->error]  
		[
			discirption:
			sem_wait() decrements() the semaphore pointed to by sem.  If
		    the semaphore's value is greater than zero, then the decrement
		    proceeds, and the function returns, immediately.  If the semaphore
		    currently has the value zero, then the call blocks until either it
		    becomes possible to perform(执行) the decrement 
		]
		<>3>>  int sem_post(sem_t *sem);[return:0-->success;-1-->error] 
		[
			 discirption:
			 sem_post() increments () the semaphore pointed to by sem.  If
		     the semaphore's value consequently becomes greater than zero, then
		     another process or thread blocked in a sem_wait(3) call will be woken
		     up and proceed to lock the semaphore.
		]
	]
]
-->><6>mutex(互斥) in mass thread
[
	1>>互斥机制使用在共享资源只能同时一个进程使用,防止其他进程修改,但是不能达到信号量同步机制,保证有序使用的效果
	2>>互斥锁在进程创建之前初始化

	-->>互斥锁的使用步骤
	[
		<x>1>>int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr);[return : 0 -->success ;error number] 
		[
			discirption:
		    The pthread_mutex_init() function initializes the specified mutex. 
		    If attr is non-NULL, the attributes specified are used to initialize the mutex. 
		    If attr is NULL, the mutex is initialized with default attributes,
		]
		<>2>>int pthread_mutex_lock(pthread_mutex_t *mutex);[return : 0 -->success ;error number] 
		[
			discirption:
		    The mutex object referenced by mutex shall(将会) be locked by calling pthread_mutex_lock(). 
		    If the mutex is already locked, the calling thread shall block until the mutex becomes available. 
		]
		<>3>>int pthread_mutex_unlock(pthread_mutex_t *mutex); [return : 0 -->success ; error number] 
		#The pthread_mutex_unlock() function shall release the mutex object referenced by mutex.
	]
]
//=================================================================================================================================

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值