再次运行,我们可能得到如下结果: This is a pthread. This is the main process. This is a pthread. This is the main process. This is a pthread. This is the main process.
多线程的程序运行的时候,由于两个线程都在争夺CPU资源,线程调用的顺序是随机的,所以结果有可能会不同。 实例
1 #include
5 void *thrd_func1(void *arg); 6 void *thrd_func2(void *arg); 7
8 int main(){
9 pthread_t tid1,tid2; 10 void *tret;
11 // 创建线程tid1,线程函数thrd_func1
12 if (pthread_create(&tid1,NULL,thrd_func1,NULL)!=0) { 13 printf(\); 14 exit(1); 15 }
16 // 创建线程tid2,线程函数thrd_func2
17 if (pthread_create(&tid2,NULL,thrd_func2,NULL)!=0) { 18 printf(\); 19 exit(1); 20 }
21 // 等待线程tid1结束,线程函数返回值放在tret中 22 if (pthread_join(tid1,&tret)!=0){ 23 printf(\); 24 exit(1); 25 } 26
27 printf(\,(int *)tret); 28 // 等待tid2结束,线程函数返回值放在tret中 29 if (pthread_join(tid2,&tret)!=0){
34
30 printf(\); 31 exit(1); 32 } 33
34 printf(\,(int *)tret); 35
36 return 0; 37 } 38
39 void *thrd_func1(void *arg){
40 printf(\); 41 // sleep(3);
42 return ((void *)1); // 自动退出线程 43 } 44
45 void *thrd_func2(void *arg){ 46 printf(\);
47 pthread_exit((void *)2); // 线程主动退出,返回(void *)2 48 }
? 多线程程序实例2
/*test.c*/
#include
int retval_hello1= 2, retval_hello2 = 3;
void* hello1(void *arg){
char *hello_str = (char *)arg; sleep(1);
printf(\ pthread_exit(&retval_hello1); }
void* hello2(void *arg){
char *hello_str = (char *)arg; sleep(2);
35
printf(\ pthread_exit(&retval_hello2); }
int main(int argc, char *argv[]){ int i;
int ret_val;
int *retval_hello[2];
pthread_t pt[THREAD_NUMBER];
const char *arg[THREAD_NUMBER];
arg[0] = \ arg[1] = \ printf(\
ret_val = pthread_create(&pt[0], NULL, hello1, (void *)arg[0]); if (ret_val != 0 ) {
printf(\ exit(1);}
ret_val = pthread_create(&pt[1], NULL, hello2, (void *)arg[1]); if (ret_val != 0 ) {
printf(\ exit(1); }
printf(\ for(i = 0; i < THREAD_NUMBER; i++) {
ret_val = pthread_join(pt[i], (void **)&retval_hello[i]); if (ret_val != 0) {
printf(\ exit(1); } else {
printf(\ } }
printf(\ return 0; }
编译并运行:
[root@localhost ~]# gcc test.c -lpthread -o test [root@localhost ~]# ./test Begin to create threads... Begin to wait for threads... hello world from thread1
36
return value is 2 hello world from thread2 return value is 3
Now, the main thread returns.
实验六 基于Linux系统调用的文件编程
6.1 常用文件操作函数 创建
int creat(const char *filename, mode_t mode); int creat(const char *filename, mode_t mode);
参数mode指定新建文件的存取权限,它同umask一起决定文件的最终权限(mode&umask),其中umask代表了文件在创建时需要去掉的一些存取权限。umask可通过系统调用umask()来改变:
int umask(int newmask); 打开
int open(const char *pathname, int flags); int open(const char *pathname, int flags, mode_t mode); open函数有两个形式,其中pathname是要打开的文件名(包含路径名称,缺省是认为在当前路径下面),flags可以取下面的一个值或者是几个值的组合:
标志 含义 O_RDONLY 以只读的方式打开文件 O_WRONLY 以只写的方式打开文件 O_RDWR 以读写的方式打开文件 O_APPEND 以追加的方式打开文件 O_CREAT 创建一个文件 O_EXEC 如果使用了O_CREAT而且文件已经存在,就会发生一个错误 O_NOBLOCK 以非阻塞的方式打开一个文件 37
O_TRUNC 如果文件已经存在,则删除文件的内容 O_RDONLY、O_WRONLY、O_RDWR三个标志只能使用任意的一个
以O_CREAT为标志的open实际上实现了文件创建的功能,因此,下面的函数等同creat()函数:
int open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode); mode可以是以下情况的组合:
标志 S_IRUSR S_IWUSR S_IXUSR S_IRWXU S_IRGRP S_IWGRP S_IXGRP S_IRWXG S_IROTH S_IWOTH S_IXOTH S_IRWXO S_ISUID S_ISGID 含义 用户可以读 用户可以写 用户可以执行 用户可以读、写、执行 组可以读 组可以写 组可以执行 组可以读写执行 其他人可以读 其他人可以写 其他人可以执行 其他人可以读、写、执行 设置用户执行ID 设置组的执行ID 除了可以通过上述宏进行“或”逻辑产生标志以外,也可以自己用数字来表示,Linux总共用5个数字来表示文件的各种权限:第一位表示设置用户ID;第二位表示设置组ID;第三位表示用户自己的权限位;第四位表示组的权限;最后一位表示其他人的权限。每个数字可以取1(执行权限)、2(写权限)、4(读权限)、0(无)或者是这些值的和。例如,要创建一个用户可读、可写、可执行,但是组没有权限,其他人可以读、可以执行的文件,并设置用户ID位。那么,我
38