这里的12/34/67 是字符串
book@100ask:~/C_coding/0122$ cat main_thread.c
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
struct student{
int age;
char name[20];
char id[4];
};
void *thread_fun(void *stu)
{
printf("student age is %d\n,name is %s\n,id is %s\n",((struct student *)stu)->age,((struct student *)stu)->name,((struct student *)stu)->id);
return (void *)0;
}
int main(int argc,char *argv[])
{
pthread_t tid;
int err;
struct student stu;
stu.age = 20;
memcpy(stu.name,"zhangsan",20);
memcpy(stu.id,"0007",5);
err = pthread_create(&tid,NULL,thread_fun,(void *)(&stu));
if(err != 0)
{
printf("create new thread fialed\n");
return 0;
}
int i;
printf("main thread have %d args\n",argc);
for(i=0; i<argc; i++)
{
printf("main thread args is %s\n",argv[i]);
}
sleep(1);
return 0;
}
book@100ask:~/C_coding/0122$ sudo gcc -lpthread -pthread -o main_thread main_thread.c
book@100ask:~/C_coding/0122$ ./main_thread
student age is 20
,name is zhangsan
,id is 0007
main thread have 1 args
main thread args is ./main_thread
上例,主线程结束,sleep 1秒后就return
book@100ask:~/C_coding/0122$ cat main_thread.c
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
struct student{
int age;
char name[20];
char id[4];
};
void *thread_fun(void *stu)
{
sleep(1);
printf("student age is %d\n,name is %s\n,id is %s\n",((struct student *)stu)->age,((struct student *)stu)->name,((struct student *)stu)->id);
return (void *)0;
}
int main(int argc,char *argv[])
{
pthread_t tid;
int err;
struct student stu;
stu.age = 20;
memcpy(stu.name,"zhangsan",20);
memcpy(stu.id,"0007",5);
err = pthread_create(&tid,NULL,thread_fun,(void *)(&stu));
if(err != 0)
{
printf("create new thread fialed\n");
return 0;
}
int i;
printf("main thread have %d args\n",argc);
for(i=0; i<argc; i++)
{
printf("main thread args is %s\n",argv[i]);
}
// sleep(2);
return 0;
}
book@100ask:~/C_coding/0122$ sudo gcc -lpthread -pthread -o main_thread main_thread.c
book@100ask:~/C_coding/0122$ ./main_thread 12 34 56
main thread have 4 args
main thread args is ./main_thread
main thread args is 12
main thread args is 34
main thread args is 56
如果main函数先退出,进程内的其它线程也结束。
book@100ask:~/C_coding/0122$ cat main_thread.c
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
struct student{
int age;
char name[20];
char id[4];
};
void *thread_fun(void *stu)
{
sleep(1);
printf("student age is %d\n,name is %s\n,id is %s\n",((struct student *)stu)->age,((struct student *)stu)->name,((struct student *)stu)->id);
return (void *)0;
}
int main(int argc,char *argv[])
{
pthread_t tid;
int err;
struct student stu;
stu.age = 20;
memcpy(stu.name,"zhangsan",20);
memcpy(stu.id,"0007",5);
err = pthread_create(&tid,NULL,thread_fun,(void *)(&stu));
if(err != 0)
{
printf("create new thread fialed\n");
return 0;
}
int i;
printf("main thread have %d args\n",argc);
for(i=0; i<argc; i++)
{
printf("main thread args is %s\n",argv[i]);
}
sleep(2);
return 0;
}
book@100ask:~/C_coding/0122$ sudo gcc -lpthread -pthread -o main_thread main_thread.c
book@100ask:~/C_coding/0122$ ./main_thread 12 34 56
main thread have 4 args
main thread args is ./main_thread
main thread args is 12
main thread args is 34
main thread args is 56
student age is 20
,name is zhangsan
,id is 0007
main函数sleep多一点,子进程就会被执行。
book@100ask:~/C_coding/0122$ cat main_thread.c
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
struct student{
int age;
char name[20];
char id[4];
};
void *thread_fun(void *stu)
{
sleep(1);
printf("student age is %d\n,name is %s\n,id is %s\n",((struct student *)stu)->age,((struct student *)stu)->name,((struct student *)stu)->id);
return (void *)0;
}
int main(int argc,char *argv[])
{
pthread_t tid;
int err;
int *rval;
struct student stu;
stu.age = 20;
memcpy(stu.name,"zhangsan",20);
memcpy(stu.id,"0007",5);
err = pthread_create(&tid,NULL,thread_fun,(void *)(&stu));
if(err != 0)
{
printf("create new thread fialed\n");
return 0;
}
int i;
printf("main thread have %d args\n",argc);
for(i=0; i<argc; i++)
{
printf("main thread args is %s\n",argv[i]);
}
pthread_exit(rval);
return 0;
}
book@100ask:~/C_coding/0122$ sudo gcc -lpthread -pthread -o main_thread main_thread.c
book@100ask:~/C_coding/0122$ ./main_thread 12 34 56
main thread have 4 args
main thread args is ./main_thread
main thread args is 12
main thread args is 34
main thread args is 56
student age is 20
,name is zhangsan
,id is 0007
加了pthread_exit(rval),这样,主进程退出,子线程程序还会被执行。