1.进程是一个具有一定独立功能的程序关于某个数据集合的一次运行活动。它是操作系统动态执行的基本单元,在传统的操作系统中,进程既是基本的分配单元,也是基本的执行单元
2.进程相关API说明
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);
参数:其中pthread_t *thread是线程唯一标识符的指针。
void *(*start_routine) (void *)是目标函数所放地方。
函数定义: int pthread_join(pthread_t thread, void **retval);
描述 :pthread_join()函数,以阻塞的方式等待thread指定的线程结束。当函数返回时,被等待线程的资源被收回。如果线程已经结束,那么该函数会立即返回。并且thread指定的线程必须是joinable的。
参数 :thread: 线程标识符,即线程ID,标识唯一线程。retval: 用户定义的指针,用来存储被等待线程的返回值。
返回值 : 0代表成功。 失败,返回的则是错误号。
3.进程的最基本创建
void* socket_pthread(void *data)
{
printf("这是线程");
}
int main()
{
pthread_t socketpthread;
pthread_create(&socketpthread,NULL,socket_pthread,NULL);
}
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
void *fun(void *arg)
{
printf("I'm thread, Thread ID = %lu\n", pthread_self());
return NULL;
}
int main(void)
{
pthread_t tid;
pthread_create(&tid, NULL, fun, NULL);
sleep(1); // 在多线程环境中,父线程终止,全部子线程被迫终止
printf("I am main, my pid = %d\n", getpid());
return 0;