1, main content
1. Process and program: The program is static. It is an orderly collection of some instructions saved on the disk without any concept of execution. The process is a dynamic concept. It is the execution process of the program, including creation, scheduling and demise.
Note: The process includes not only the instructions and data of the program, but also the program counter, the value of all the registers of the CPU, and the process stack of the temporary data.
2. In the unified process, multiple threads share the following resources: executable instructions, static data, file descriptors opened in the process, signal processing function, current working directory, user ID, user group ID.
3. Private resources of threads: thread ID (TID), PC (program counter) and related registers, stacks (local variables and return addresses), error number (ERRNO), signal mask and priority, execution status and attributes Essence
4. Create a thread in multi -threaded programming Pthread_create (). The definition of it in Linux is as follows:
required header file: #include
#include<stdio.h>
#include <pthread.h>
void *ThreadFunc(void *arg)
{
printf("hello world\r\n");
}
int main()
{
pthread_t tID = 0;
if(0 != pthread_create(&tID,NULL,ThreadFunc,NULL))
{
printf("create error\r\n");
return -1;
}
sleep(1);
return 0;
}
2、pthread_arg.c
#include<stdio.h>
#include <pthread.h>
void *ThreadFunc(void *arg)
{
if(NULL == arg)
{
return (void *)NULL;
}
//printf("hello world\r\n");
int b = *(int *)arg;
printf("b=%d\r\n",b);
}
int main()
{
pthread_t tID = 0;
int a = 10;
if(0 != pthread_create(&tID,NULL,ThreadFunc,(void *)&a))
{
printf("create error\r\n");
return -1;
}
// sleep(1);
pthread_join(tID,NULL);
return 0;
}
3、pthread_join.c
#include<stdio.h>
#include <pthread.h>
void *ThreadFunc(void *arg)
{
printf("hello world\r\n");
pthread_exit("thread exit");
}
int main()
{
pthread_t tID = 0;
if(0 != pthread_create(&tID,NULL,ThreadFunc,NULL))
{
printf("create error\r\n");
return -1;
}
// sleep(1);
char *str = NULL;
pthread_join(tID,(void *)&str);
printf("%s\r\n",str);
return 0;
}
4, pthread_mutex.c uses mutual locks to achieve synchronization and mutual exclusion between processes, play a role in protecting the scene. Ensure that the first function calls can be executed before the second function call can be executed.
#include<stdio.h>
#include <pthread.h>
#define BUF_SIZE 11
pthread_mutex_t mutex;
void *Func(void *arg)
if(NULL == arg)
{
return (void *)NULL;
}
char *p = (char *)arg;
pthread_mutex_lock(&mutex);
static char buf[BUF_SIZE]={
0};
int i = 0;
for(;i<10;i++)
{
buf[i]=p[i];
sleep(1);
}
printf("buf=%s\r\n",buf);
pthread_mutex_unlock(&mutex);
}
int main()static char buf[BUF_SIZE]={
0};
int i = 0;
for(;i<10;i++)
{
buf[i]=p[i];
sleep(1);
}
printf("buf=%s\r\n",buf);
{
pthread_t tID1 = 0;
pthread_t tID2 = 0;
char arr1[]={
'a','b','c','d','e','f','g','h','i','j'};
char arr2[]={
'1','2',3,'4','5','6','7','8','9','0'};
if(-1 == pthread_mutex_init(&mutex,NULL))
{
return -1;
}
if(0 != pthread_create(&tID1,NULL,Func,arr1))
{
return -1;
}
if(0 != pthread_create(&tID2,NULL,Func,arr2))
{
return -1;
}
pthread_join(tID1,NULL);
pthread_join(tID2,NULL);
return 0;
}
5, pthread_sem.c function to implement mutually exclusive operation between processes
#include<stdio.h>
#include <pthread.h>
#include <semaphore.h>
sem_t sem1;
sem_t sem2;
void *ThreadFunc1(void *arg)
{
int i=10;
while(i--)
{
sem_wait(&sem1);
printf("hello\r\n");
usleep(5000);
sem_post(&sem2);
}
}
void *ThreadFunc2(void *arg)
{
int i=10;
while(i--)
{
sem_wait(&sem2);
printf("world\r\n");
usleep(5000);
sem_post(&sem1);
}
}
int main()
{
pthread_t tID1 = 0;
pthread_t tID2 = 0;
if(-1 == sem_init(&sem1,0,1) || -1 == sem_init(&sem2,0,0))
{
return -1;
}
if(0 != pthread_create(&tID1,NULL,ThreadFunc1,NULL))
{
printf("create error\r\n");
return -1;
}
if(0 != pthread_create(&tID2,NULL,ThreadFunc2,NULL))
{
printf("create error\r\n");
return -1;
}
pthread_join(tID1,NULL);
pthread_join(tID2,NULL);
return 0;
}
6、shm_r.c
#include<stdio.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <string.h>
#define SHM_SIZE 10
int main()
{
//get key
key_t key =ftok(".",11);
if(-1 == key)
{
return -1;
}
printf("key ok\r\n");
//shmget
int shmid = shmget(key,SHM_SIZE,IPC_CREAT | 0666);
if(-1==shmid)
{
return -1;
}
printf("shmget ok\r\n");
//shm attach
void *p =shmat(shmid,NULL,0);
if((void*)-1==p)
{
return -1;
}
char *pTmp = (char *)p;
int i = 0;
char c = 0;
for(;i<SHM_SIZE;i++)
{
c = pTmp[i];
printf("%d ",c);
}
printf("\r\n");
shmdt(p);
//delete shm
shmctl(shmid,IPC_RMID,NULL);
return 0;
}
7、shm_w.c
#include<stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <string.h>
#define SHM_SIZE 10
int main()
{
//get key
key_t key = ftok(".",11);
if(-1 == key)
{
return -1;
}
//shmget
int shmid=shmget(key,SHM_SIZE,IPC_CREAT | 0666);
if(-1==shmid)
{
return -1;
}
printf("create success\r\n");
//shm attch
void *p=shmat(shmid,NULL,0);
if((void*)-1==p)
{
return -1;
}
memset(p,13,SHM_SIZE);
shmdt(p);
return 0;
}