fork is expensive. Memory is copied from the parent to the child, all descriptors are duplicated in the child, and so on. Current implementations use a technique called copy-on-write, which avoids a copy of the parent's data space to the child until the child needs its own copy. But, regardless of this optimization,fork is expensive.
IPC is required to pass information between the parent and child after the fork. Passing information from the parent to the child before the fork is easy, since the child starts with a copy of the parent's data space and with a copy of all the parent's descriptors. But, returning information from the child to the parent takes more work.
Threads help with both problems. Threads are sometimes called lightweight processes since a thread is "lighter weight" than a process. That is, thread creation can be 10–100 times faster than process creation.
All threads within a process share the same global memory. This makes the sharing of information easy between the threads, but along with this simplicity comes the problem of synchronization.
=============================
3、有关线程操作的函数
#include <pthread.h>
int pthread_create(pthread_t *tid, const pthread_attr_t *attr, void *(*func) (void *), void *arg); int pthread_join (pthread_t tid, void ** status); pthread_t pthread_self (void); int pthread_detach (pthread_t tid); void pthread_exit (void *status);
/** 1)有一int型全局变量g_Flag初始值为0;* 2)在主线称中起动线程1,打印“this is thread1”,并将g_Flag设置为1* 3)在主线称中启动线程2,打印“this is thread2”,并将g_Flag设置为2*/
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<errno.h>
#include<unistd.h>
int g_Flag=0;
void* thread1(void*);
void* thread2(void*);
/** when program is started, a single thread is created, called the initial thread or main thread.* Additional threads are created by pthread_create.* So we just need to create two thread in main().*/
int main(int argc, char** argv)
{printf("enter main\n");pthread_t tid1, tid2;int rc1=0, rc2=0;rc2 = pthread_create(&tid2, NULL, thread2, NULL);if(rc2 != 0)printf("%s: %d\n",__func__, strerror(rc2));
thread1() will be execute by thread1, after pthread_create()
it will set g_Flag = 1; / void thread1(void* arg) { printf(“enter thread1\n”); printf(“this is thread1, g_Flag: %d, thread id is %u\n”,g_Flag, (unsigned int)pthread_self()); g_Flag = 1; printf(“this is thread1, g_Flag: %d, thread id is %u\n”,g_Flag, (unsigned int)pthread_self()); printf(“leave thread1\n”); pthread_exit(0); }
/*
thread2() will be execute by thread2, after pthread_create()
it will set g_Flag = 2; / void thread2(void* arg) { printf(“enter thread2\n”); printf(“this is thread2, g_Flag: %d, thread id is %u\n”,g_Flag, (unsigned int)pthread_self()); g_Flag = 2; printf(“this is thread1, g_Flag: %d, thread id is %u\n”,g_Flag, (unsigned int)pthread_self()); printf(“leave thread2\n”); pthread_exit(0); }
如果程序中使用到了pthread库中的函数,除了要#include<pthread.h>,在编译的时候还有加上-lpthread 选项。 netsky@ubuntu:~/workspace/pthead_test$ ./a.out enter main enter thread2 this is thread2, g_Flag: 0, thread id is 3079588720 this is thread1, g_Flag: 2, thread id is 3079588720 leave thread2 leave main enter thread1 this is thread1, g_Flag: 2, thread id is 3071196016 this is thread1, g_Flag: 1, thread id is 3071196016 leave thread1 但是运行结果不一定是上面的,还有可能是:
netsky@ubuntu:~/workspace/pthead_test$ ./a.out enter main leave main enter thread1 this is thread1, g_Flag: 0, thread id is 3069176688 this is thread1, g_Flag: 1, thread id is 3069176688 leave thread1
或者是:
netsky@ubuntu:~/workspace/pthead_test$ ./a.out enter main leave main 等等。这也很好理解因为,这取决于主线程main函数何时终止,线程thread1、thread2是否能够来得急执行它们的函数。这也是多线程编程时要注意的问题,因为有可能一个线程会影响到整个进程中的所有其它线程!如果我们在main函数退出前,sleep()一段时间,就可以保证thread1、thread2来得及执行。
int pthread_cond_wait(pthread_cond_t *cptr, pthread_mutex_t *mptr); int pthread_cond_signal(pthread_cond_t *cptr); //Both return: 0 if OK, positive Exxx value on error
/*是否熟悉POSIX多线程编程技术?如熟悉,编写程序完成如下功能:1)有一int型全局变量g_Flag初始值为0;2)在主线称中起动线程1,打印“this is thread1”,并将g_Flag设置为13)在主线称中启动线程2,打印“this is thread2”,并将g_Flag设置为24)线程序1需要在线程2退出后才能退出5)主线程在检测到g_Flag从1变为2,或者从2变为1的时候退出*/
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<errno.h>
#include<unistd.h>