一、县城和进程之间的关系,与进程相比线程有哪些优点?

1、线程与进程之间的关系:

(1)线程是进程的一个执行流,是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位。(2) 一个进程由几个线程组成,线程与同属一个进程的其它线程共享进程所拥有的全部资源。(3)进程--资源分配的最小单位,线程--程序执行的最小单位

(4)进程有独立的地址空间,一个进程崩溃之后,在保护模式下下不会对其它进程产生影响,而线程只是一个进程中的不同执行路径。

(5)线程有自己的堆栈和局部变量,单线程没有单独的地址空间,一个线程死掉就等于整个进程死掉,所以多进程的程序要比多线程的程序健壮,但在进程切换时,耗费资源较大,效率差一点。

2、与进程相比有哪些优点:

(1)是一种非常节俭的多任务操作方式,在linux下,启动一个新的进程必须分配给他独立的地址空间,建立众多的数据表来维护它的代码段,堆栈段,数据段,这是一种昂贵的多任务工作方式。

(2)线程间方便地通信机制。

(3)使多CPU系统更加的高效,操作系统会保证当前线程数不大于cpu数目时,不同的线程运行于不同的cpu上。

(4)改善程序结构,一个即长又复杂的进程可以考虑分成多个线程,成为几个独立或半独立的运行部分,这样的程序会利于理解和修改。

二、现成的创建,等待,退出,的api函数详解。

1、线程创建:pthread_create()

int pthread_create( pthread_t * tidp, const pthread_attr_t * attr,(void *)(*start_rtn)(void*),void* arg);

参数:tidp :为指向线程标识符的指针。

attr:用来设置线程属性。

arg:传入线程执行函数的参数

成功返回 0 ,失败 返回 错误码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h> 
#include <pthread.h> 
 
void *myThread1(void
    int i; 
    for (i=0; i<3; i++) 
    
        printf("This is the 1st pthread,created by zieckey.\n"); 
        sleep(1);//Let this thread to sleep 1 second,and then continue to run 
    
 
void *myThread2(void
    int i; 
    for (i=0; i<3; i++) 
    
        printf("This is the 2st pthread,created by zieckey.\n"); 
        sleep(1); 
    
 
int main() 
    int i=0, ret=0
    pthread_t id1,id2; 
 
    /*创建线程1*/
    ret = pthread_create(&id1, NULL, (void*)myThread1, NULL); 
    if (ret) 
    
        printf("Create pthread error!\n"); 
        return 1
    
 
    /*创建线程2*/
    ret = pthread_create(&id2, NULL, (void*)myThread2, NULL); 
    if (ret) 
    
        printf("Create pthread error!\n"); 
        return 1
    
 
    pthread_join(id1, NULL); 
    pthread_join(id2, NULL); 
 
    return 0
}

2、线程等待退出:pthread_join()

int pthread_join(pthread_t thread ,void ** retval);

参数:thread线程标识符,就是线程id,唯一的标识线程。

retval:用户定义的指针,用来存储被等待的线程的返回值。

返回值:0 代表成功,失败 返回的是 错误号

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <pthread.h> 
#include <unistd.h> 
#include <stdio.h> 
 
void *thread(void *str) 
    int i; 
    for (i = 0; i < 10; ++i) 
    
        sleep(2); 
        printf( "This in the thread : %d\n" , i ); 
    
    return NULL; 
 
int main() 
    pthread_t pth; 
    int i; 
    int ret = pthread_create(&pth, NULL, thread, (void *)(i)); 
 
    pthread_join(pth, NULL); 
 
    printf("123\n"); 
    for (i = 0; i < 10; ++i) 
    
        sleep(1); 
        printf( "This in the main : %d\n" , i ); 
    
 
    return 0
}

3、线程退出:pthread_exit()

void pthread_exit(void *retval)

返回值: 调用现成的返回值,可用其它函数pthread_join 来获取

查看线程的返回值:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h> 
#include <pthread.h> 
#include <unistd.h> 
 
void *create(void *arg) 
    printf("new thread is created ... \n"); 
    return (void *)8
 
int main(int argc,char *argv[]) 
    pthread_t tid; 
    int error; 
    void *temp; 
 
    error = pthread_create(&tid, NULL, create, NULL); 
    printf("main thread!\n"); 
    if( error ) 
    
        printf("thread is not created ... \n"); 
        return -1
    
    error = pthread_join(tid, &temp); 
    if( error ) 
    
        printf("thread is not exit ... \n"); 
        return -2
    
 
    printf("thread is exit code %d \n", (int)temp); 
    return 0
}

三、在创建线程时参数如何传递:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h> 
#include <pthread.h> 
#include <unistd.h> 
#include <stdlib.h> 
 
struct menber 
    int a; 
    char *s; 
}; 
 
void *create(void *arg) 
    struct menber *temp; 
    temp=(struct menber *)arg; 
    printf("menber->a = %d  \n",temp->a); 
    printf("menber->s = %s  \n",temp->s); 
 
    return (void *)0
 
 
int main(int argc,char *argv[]) 
    pthread_t tidp; 
    int error; 
    struct menber *b; 
 
    b=(struct menber *)malloc( sizeof(struct menber) ); 
    b->a = 4
    b->s = "zieckey"
 
    error = pthread_create(&tidp, NULL, create, (void *)b); 
 
    if( error ) 
    
        printf("phread is not created...\n"); 
        return -1
    
    sleep(1); 
    printf("pthread is created...\n"); 
    return 0
}