【Linux c】线程入门

来源:互联网 发布:无法打开 windows.h 编辑:程序博客网 时间:2024/05/29 18:12
#include <stdio.h>#include <unistd.h>#include <pthread.h>#include <string.h>#include <sys/time.h>void print_msg1(void);void print_msg2(void);void thread_create(pthread_t thread[2]);int main(){pthread_t thread[2];thread_create(thread);sleep(1000);return 0;}void print_msg1(int fd){int retval;int id=pthread_self();printf("Thread1 ID: %x\n",id);while(1){printf("1111111fd=%d\n",fd);//sleep(1);}  //pthread_exit(&retval);}void print_msg2(int fd){//int retval;int id=pthread_self();printf("Thread2 ID: %x\n",id);while(1){  printf("222222222fd=%d\n",fd);//sleep(1);}//thread_exit(&retval);}void thread_create(pthread_t thread[2]){    int fd = 33;pthread_create(&thread[0],NULL, (void *)(&print_msg1), (void *)fd);pthread_create(&thread[1],NULL, (void *)(&print_msg2), (void *)fd);}

第二组:

#include <pthread.h>#include <stdio.h>#include <sys/time.h>#include <string.h>#define MAX 10 pthread_t thread[2];pthread_mutex_t mut;int number=0, i;  struct argument{int TS_port;int NB_port;};void *thread1(void *arg_thread){struct argument *arg; // 接收传递过来的参数结构体      arg = ( struct argument * )arg_thread;    printf ("thread1 : I'm thread 1\n");//for (i = 0; i < MAX; i++){//int *p;//p = k;//*p = 111;arg->TS_port = 101;printf("thread1, arg->TS_port = %d \n", arg->TS_port);//sleep(1);//}    printf("thread1 :主函数在等我完成任务吗?\n");     pthread_exit(NULL); } void *thread2(void *arg_thread){struct argument *arg; // 接收传递过来的参数结构体      arg = ( struct argument * )arg_thread;    printf("thread2 : I'm thread 2\n");//每隔1秒检查K的值,当k的值变化了,就继续执行,否则,等待while(arg->TS_port == 0){printf("thread2,我在等待K...\n");sleep(1);}printf("thread2, k = %d \n", arg->TS_port);    printf("thread2 :主函数在等我完成任务吗?\n");    pthread_exit(NULL);}   void thread_wait(void){    //等待线程结束    if(thread[0] !=0) { //comment4        pthread_join(thread[0],NULL);        printf("线程1已经结束\n");    }    if(thread[1] !=0) { //comment5        pthread_join(thread[1],NULL);        printf("线程2已经结束\n");    }} int main(){      struct argument arg;      arg.TS_port = 0;   arg.NB_port = 0;       printf("我是主函数哦,我正在创建线程,呵呵\n");    int temp;    memset(&thread, 0, sizeof(thread)); //comment1    //创建线程    if((temp = pthread_create(&thread[0], NULL, thread1, (void*)&arg)) != 0) //comment2        printf("线程1创建失败!\n");    else        printf("线程1被创建\n");    if((temp = pthread_create(&thread[1], NULL, thread2, (void*)&arg)) != 0) //comment3        printf("线程2创建失败");    else        printf("线程2被创建\n");    printf("我是主函数哦,我正在等待线程完成任务阿,呵呵\n");    thread_wait();printf("main, k = %d \n", arg.TS_port);    return 0;}
第三组:

//两个函数互斥访问一个全局变量#include <pthread.h>#include <stdio.h>#include <sys/time.h>#include <string.h>#define MAX 10 pthread_t thread[2];pthread_mutex_t mut;int number=0, i; void print(char a[], int num){int i;for(i = 0; i < num; i++){printf("%d\t", a[i]);}printf("\n");return ;}void *thread1(){    printf ("thread1 : I'm thread 1\n");char a [] = "abcde";print(a, 5);    for (i = 0; i < MAX; i++)        {            printf("thread1 : number = %d\n",number);            pthread_mutex_lock(&mut);            number++;            pthread_mutex_unlock(&mut);            sleep(2);        }    printf("thread1 :主函数在等我完成任务吗?\n");     pthread_exit(NULL); } void *thread2(){    printf("thread2 : I'm thread 2\n");char a [] = "abcde";print(a, 5);    for (i = 0; i < MAX; i++)        {            printf("thread2 : number = %d\n",number);            pthread_mutex_lock(&mut);            number++;            pthread_mutex_unlock(&mut);            sleep(3);        }    printf("thread2 :主函数在等我完成任务吗?\n");    pthread_exit(NULL);}  void thread_create(void){    int temp;    memset(&thread, 0, sizeof(thread)); //comment1    //创建线程    if((temp = pthread_create(&thread[0], NULL, thread1, NULL)) != 0) //comment2        printf("线程1创建失败!\n");    else        printf("线程1被创建\n");    if((temp = pthread_create(&thread[1], NULL, thread2, NULL)) != 0) //comment3        printf("线程2创建失败");    else        printf("线程2被创建\n");} void thread_wait(void){    //等待线程结束    if(thread[0] !=0) { //comment4        pthread_join(thread[0],NULL);        printf("线程1已经结束\n");    }    if(thread[1] !=0) { //comment5        pthread_join(thread[1],NULL);        printf("线程2已经结束\n");    }} int main(){    //用默认属性初始化互斥锁    pthread_mutex_init(&mut,NULL);    printf("我是主函数哦,我正在创建线程,呵呵\n");    thread_create();    printf("我是主函数哦,我正在等待线程完成任务阿,呵呵\n");    thread_wait();    return 0;}

第四组:

fan给的一个任务

#include <stdio.h>#include <string.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <stdlib.h>//add#include <signal.h>#include <unistd.h>#include <sys/time.h>#include <pthread.h>#include <errno.h>//only writevoid test1(){unsigned char * data = NULL;int time = 0;int size = 0;int fd = -1;int i = 0;int ret = 0;printf("How many times do you want to send:");scanf("%d", &time);printf("packet size(<2000):");scanf("%d", &size);printf("time = [%d], size = [%d]\n", time, size);data = (unsigned char *)malloc(size - 4);for(i=0; i< size-4; i++)data[i] = i;fd = open("/dev/umts_ipc0", O_RDWR);if(fd != -1){for (i=0; i<time; i++) {ret = write(fd, data, size-4);if (ret != size-4)printf("ret = [%d]\n", ret);}close(fd);}else{printf("Device open failure\n");}free(data);}//write and read(read after write),check datavoid test2(){unsigned char * data_in = NULL;unsigned char * data_out = NULL;int time = 0;int count = 0;int size = 0;int fd = -1;int i = 0;int ret = 0;int j = 0;printf("How many times do you want to send:");scanf("%d", &time);printf("How many packets in one time do you want to send:");scanf("%d", &count);size = 1500;printf("time = [%d], count = [%d] size = [%d]\n", time, count, size);data_in = (unsigned char *)malloc(size - 4);data_out = (unsigned char *)malloc(size);for(i=0; i< size-4; i++)data_in[i] = i;memset(data_out, 0, size);fd = open("/dev/umts_ipc0", O_RDWR);if(fd != -1){for (i=0; i<time; i++){for (j=0; j<count; j++){ret = write(fd, data_in, size-4);if (ret != size-4)printf("ret = [%d]\n", ret);}sleep(1);for (j=0; j<count; j++){ret = read(fd, data_out, size);if (memcmp(data_in, data_out+4, size-4) == 0)printf("wrtie and read is match\n");elseprintf("wrtie and read is not match!!!\n");}}close(fd);}else{printf("Device open failure\n");}free(data_in);free(data_out);}//write and read(read after write),check datavoid test3(){unsigned char * data_in = NULL;unsigned char * data_out = NULL;int time = 0;int count = 0;int size = 0;int fd = -1;int i = 0;int ret = 0;int j = 0;printf("How many times do you want to send:");scanf("%d", &time);printf("How many packets in one time do you want to send:");scanf("%d", &count);size = 1500;printf("time = [%d], count = [%d] size = [%d]\n", time, count, size);data_in = (unsigned char *)malloc(size - 4);data_out = (unsigned char *)malloc(size);for(i=0; i< size-4; i++)data_in[i] = i;memset(data_out, 0, size);fd = open("/dev/umts_ipc0", O_RDWR);if(fd != -1){for (i=0; i<time; i++){for (j=0; j<count; j++){ret = write(fd, data_in, size-4);if (ret != size-4)printf("ret = [%d]\n", ret);}sleep(1);for (j=0; j<count; j++){ret = read(fd, data_out, size);if (memcmp(data_in, data_out+4, size-4) == 0)printf("wrtie and read is match\n");else {printf("wrtie and read is not match!!! exit!\n");close(fd);free(data_in);free(data_out);return;}}}close(fd);}else{printf("Device open failure\n");}free(data_in);free(data_out);}int stop = 0;struct argument{int fd;unsigned int time;};void alarmhandle(int sig){    stop = 1;}void set_alarm(unsigned int time){int ret ;    struct itimerval tick;    tick.it_value.tv_sec = time;      tick.it_value.tv_usec = 0;    tick.it_interval.tv_sec  = 0;     tick.it_interval.tv_usec = 0;signal(SIGALRM, alarmhandle);    ret = setitimer(ITIMER_REAL, &tick, NULL);    if(ret != 0){        printf("Set timer error. %s \n", strerror(errno) );        return ;    }    printf("Wait!\n");}void write_thread(void *arg_thread){int ret = 0;int count = 0;int id = 0;int i = 0;unsigned char data[1024] = {0};struct argument *arg; // 接收传递过来的参数结构体      arg = ( struct argument * )arg_thread;id = pthread_self();printf("Thread write ID: %x\n",id);stop = 0;for(i=0; i< 1024; i++){data[i] = i;}if(arg->fd != -1){while(1){if (stop == 1) break;ret = write(arg->fd, data, sizeof(data)-1);count ++;if (ret > 1)printf(" write ret = [%d]\n", ret);ret = 0;}}else{printf("Device open failure\n");}//calculatedouble bandwidth ;bandwidth = (count*1024*8)/arg->time;printf("Bandwidth to write = %lf bps\n",bandwidth);}void read_thread(void *arg_thread){int ret = 0;int count = 0;int id = 0;unsigned char data[1024] = {0};struct argument *arg; // 接收传递过来的参数结构体      arg = ( struct argument * )arg_thread;id = pthread_self();printf("Thread read ID: %x\n",id);stop = 0;if(arg->fd != -1){while(1){if (stop == 1) break;ret = read(arg->fd, data, sizeof(data)-1);count ++;if (ret > 1)printf("read ret = [%d]\n", ret);stpcpy(data,"");ret = 0;}}else{printf("Device open failure\n");}//calculatedouble bandwidth ;bandwidth = (count*1024*8)/arg->time;printf("Bandwidth to read = %lf bps\n",bandwidth);}//throughput test on firmware-loopbackvoid test4(){int fd = -1;unsigned char result[1024] = {0};unsigned int time = 0;struct argument arg;pthread_t thread[2];printf("How many times do you want to send:");scanf("%d", &time);printf("time = [%d], size = [1024]\n", time);    fd= open("/dev/umts_ipc0", O_RDWR);arg.fd = fd;arg.time = time;set_alarm(time);pthread_create(&thread[0],NULL, (void *)(&read_thread),  (void*)&arg);pthread_create(&thread[1],NULL, (void *)(&write_thread), (void*)&arg);sleep(1000);close(fd);}void main(){int choice = 0;printf("test app version: 0.0.3.20160527_beat\n");printf("choose the test case:\n");printf("  1-only write\n");printf("  2-write and read,check data-if not match-give warning\n");printf("  3-write and read,check data-if not match-break\n");printf("  4-throughput test on firmware-loopback\n");scanf("%d", &choice);switch(choice){case 1:test1();break;case 2:test2();break;case 3:test3();break;case 4:test4();break;default:printf("input error\n");break;}}




                                             
1 0