Create Process Thread Linux
2017-09-09 22:03
155 查看
建立child process
*fork() <unistd.h>
*system()
*exec execve()等共有六个functions <unistd.h>
等待 child process
*wait() & waitpid() <sys/types.h> <sys/wait.h>
等待其他的process
建议popen+ps+grep
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
*unix linux以fork 产生process exec执行process
*呼叫fork() 的为Parent,产生出的为Child 运行顺序无法保证 用vfork() 可以保证child一定先运行 child一定先运行(直到exit 或exec)
*child将完美复制整个parent 但是太浪费内存 现有系统不再完整复制 只需要复制变更的部分 copy-on-write
*process 结束后 会产生结束状态(代码, exit code, cpu, 使用时间)并且有系统保管,直到parent领回才真正消灭
**如果parent先结束->把child托孤给init
**parent用wait()等待child->parent先转成idle, 直到child结束, parent领回child的结束状态后继续运行
**parent活着并且不处理child的结束状态->child变成zombie并且一直存在(占用process list
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
有时候parent正在忙 没空处理child的结束状态
利用两次fork() 第一个child扮演原本的parent 第二个child扮演的是真正的child
原本的parent结束 两个child都托孤给init->不会有zombie
if(fork()==0)
第一个child
else{
if(fork()==0)
第二个child
else
parent结束
}
-----------------------
利用两次fork() 第一个child扮演原本的第二个child的parent 在托孤给init
if(fork()==0){
第一个child
if(fork()==0)
第二个child
else
第一个child直接结束
}
else{
parent领回第一个child的结束状态
在继续做parent该忙碌的事情
}
--------------------------------------------------------------------------------------------------------------------------------
process ID User ID <unitsd.h> <sys/types.h>
可以取得各种ID 返回值结尾pid_t
若对于unix-like system calls
pid_t getpid(void) 取得process ID
pid_t getppid(void) 取得parent process ID
pid_t getuid(void) 取得 real user ID
pid_t geteuid(void) 取得effective user ID
pid_t geteuid(void) 取得real group ID
pid_t getegid(void) 取得effective group ID euid的egid
----------------------------------------------------------------------------------------------------------------------------------
int execl(const char*path, const char*arg0,...)
int execv(const char *path, const char *arg[]);
int execle(const char*path, const char *arg0, ..., const char*env[]);
int execve(const char*path, const char*arg[], const char*evn[]);
int execlp(const char *file, const char *arg0,...);
int execvp(const char *file, const char *arg[]);
假设A使用exec叫起B process B 会完全取代原本的process A 且从B的main function开始运行(pid不变)
#include<unistd.h>
失败了返回-1
其他五个后面都是用execve()
-----------------------------------------------------------------------------------------------------------------------------------------
int pthread_create( //建立一个新的thread
pthread_t *tid, //指针指向thread ID
pthread_attr_t *attr, //thread 属性设定
void *func(void *), //function name
void *arg); // parameter for function
成功 返回0
失败 返回EAGAIN EINVAL EPERM
int pthread_join( //等待某个thread结束 //win WaitForSingleObject
pthread_t tid, //thread ID
void **retval); //pthread_exit() 返回值
成功 返回0
失败 返回EDEADLK EINVAL ESRCH
------------------------------------------------------
pthread 补充
1.没有特别设定 一个thread 可以被pthread_join等待
2.不想被等待的话 有两种方式
*1.建立前设定attr
pthread_attr_init() 做init
pthread_attr_setdetachstate()设定detach
pthread_create()传入attr
pthread_attr_destroy()用完后要记得free resource
*2.建立完了才用pthread_detach(thread_id)
3.#include<pthread.h>
4.gcc的时候 假设 -lpthread
-------------------------------------------------------------
void main(void){
int x=100;
pthread_t tid=0;
if((pthread_create(&tid, NULL, aa, (void*)(&x)))!=0){
printf("create thread failed");
return;
}
pthread_join(tid, NULL);
printf("x=%d\n",x);
}
void *aa(void *arg){
int num=0;
num=*((int*)arg);
num=num+10;
printf("num=%d\n",num);
sleep(5);
}
如果要传过去的不止一个parameter 要么用struct 要么存放内存连续的
----------------------------------------------------------------------------
*fork() <unistd.h>
*system()
*exec execve()等共有六个functions <unistd.h>
等待 child process
*wait() & waitpid() <sys/types.h> <sys/wait.h>
等待其他的process
建议popen+ps+grep
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
*unix linux以fork 产生process exec执行process
*呼叫fork() 的为Parent,产生出的为Child 运行顺序无法保证 用vfork() 可以保证child一定先运行 child一定先运行(直到exit 或exec)
*child将完美复制整个parent 但是太浪费内存 现有系统不再完整复制 只需要复制变更的部分 copy-on-write
*process 结束后 会产生结束状态(代码, exit code, cpu, 使用时间)并且有系统保管,直到parent领回才真正消灭
**如果parent先结束->把child托孤给init
**parent用wait()等待child->parent先转成idle, 直到child结束, parent领回child的结束状态后继续运行
**parent活着并且不处理child的结束状态->child变成zombie并且一直存在(占用process list
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
有时候parent正在忙 没空处理child的结束状态
利用两次fork() 第一个child扮演原本的parent 第二个child扮演的是真正的child
原本的parent结束 两个child都托孤给init->不会有zombie
if(fork()==0)
第一个child
else{
if(fork()==0)
第二个child
else
parent结束
}
-----------------------
利用两次fork() 第一个child扮演原本的第二个child的parent 在托孤给init
if(fork()==0){
第一个child
if(fork()==0)
第二个child
else
第一个child直接结束
}
else{
parent领回第一个child的结束状态
在继续做parent该忙碌的事情
}
--------------------------------------------------------------------------------------------------------------------------------
process ID User ID <unitsd.h> <sys/types.h>
可以取得各种ID 返回值结尾pid_t
若对于unix-like system calls
pid_t getpid(void) 取得process ID
pid_t getppid(void) 取得parent process ID
pid_t getuid(void) 取得 real user ID
pid_t geteuid(void) 取得effective user ID
pid_t geteuid(void) 取得real group ID
pid_t getegid(void) 取得effective group ID euid的egid
----------------------------------------------------------------------------------------------------------------------------------
int execl(const char*path, const char*arg0,...)
int execv(const char *path, const char *arg[]);
int execle(const char*path, const char *arg0, ..., const char*env[]);
int execve(const char*path, const char*arg[], const char*evn[]);
int execlp(const char *file, const char *arg0,...);
int execvp(const char *file, const char *arg[]);
假设A使用exec叫起B process B 会完全取代原本的process A 且从B的main function开始运行(pid不变)
#include<unistd.h>
失败了返回-1
其他五个后面都是用execve()
-----------------------------------------------------------------------------------------------------------------------------------------
int pthread_create( //建立一个新的thread
pthread_t *tid, //指针指向thread ID
pthread_attr_t *attr, //thread 属性设定
void *func(void *), //function name
void *arg); // parameter for function
成功 返回0
失败 返回EAGAIN EINVAL EPERM
int pthread_join( //等待某个thread结束 //win WaitForSingleObject
pthread_t tid, //thread ID
void **retval); //pthread_exit() 返回值
成功 返回0
失败 返回EDEADLK EINVAL ESRCH
------------------------------------------------------
pthread 补充
1.没有特别设定 一个thread 可以被pthread_join等待
2.不想被等待的话 有两种方式
*1.建立前设定attr
pthread_attr_init() 做init
pthread_attr_setdetachstate()设定detach
pthread_create()传入attr
pthread_attr_destroy()用完后要记得free resource
*2.建立完了才用pthread_detach(thread_id)
3.#include<pthread.h>
4.gcc的时候 假设 -lpthread
-------------------------------------------------------------
void main(void){
int x=100;
pthread_t tid=0;
if((pthread_create(&tid, NULL, aa, (void*)(&x)))!=0){
printf("create thread failed");
return;
}
pthread_join(tid, NULL);
printf("x=%d\n",x);
}
void *aa(void *arg){
int num=0;
num=*((int*)arg);
num=num+10;
printf("num=%d\n",num);
sleep(5);
}
如果要传过去的不止一个parameter 要么用struct 要么存放内存连续的
----------------------------------------------------------------------------
相关文章推荐
- Linux中的task,process, thread 简介
- PspCreateProcessNotifyRoutine,PspCreateThreadNotifyRoutine,PspLoadImageNotifyRoutine表全部清空
- linux tomcat java.lang.OutOfMemoryError: unable to create new native thread
- CreateRemoteThread和WriteProcessMemory技术
- CreateRemoteThread和WriteProcessMemory技术
- CreateThread CreateProcess
- Linux当中Process与Thread的概念
- Remote Thread Execution in System Process using NtCreateThreadEx for Vista & Windows7
- Exception in thread "main" java.io.IOException: Cannot run program "XX": CreateProcess error
- Linux process/thread in practice
- 《Windows API巡礼》--CreateRemoteThread和WriteProcessMemory
- Linux 资源暂不可用或者unable to create new native thread
- Linux Process VS Thread VS LWP
- Analysis of Process Multi-Thread Manangement Based on a simpilified Linux Kernel--myKernel
- VirtualAllocEx;WriteProcessMemory;CreateRemoteThread
- linux中process, kernel thread, user thread的区别
- 赞!VC++编程创建远程线程式调用CALL_VirtualAllocEx_WriteProcessMemory_CreateRemoteThread_WaitForSingleObject
- 剥下“java.lang.OutOfMemoryError: unable to create new native thread”的外衣 创建线程数公式(MaxProcessMemory - JVMMemory – ReservedOsMemory)
- CreateRemoteThreadWriteProcessMemory技术 简要的
- Migrating Win32 C/C++ applications to Linux on POWER, Part 1: Process, thread