进程与线程

embedded/2025/3/19 12:35:07/

进程与线程

  • proc
      • fork.c
      • getpid.c
      • n_proc.c
      • proc_num.c
      • exit/_exit
      • atexit.c
      • wait.c
      • waitpid.c
      • waitpid_lianxi.c
      • execl.c
      • exec_work
      • minishell
  • thread
      • pthread_self
      • pthread_exit
      • pthread_cancle
      • pthread_limit
      • pthread_join
      • pthread_join2
      • pthread_args
      • pthread_args2
      • pthread_detach
      • pthread_cleanup_push
      • pthread_cal
      • pthread_fun_point
  • pthread_con
      • pthread_10000
      • pthread_mutex
      • pthread_trylock
      • bank
      • bank_nonblock
      • sem
      • bank_sem
      • system for shared memory
      • find_define
  • ipc
      • pipe
      • pipe_write_block
      • pipe_write_broken.c
      • fifo
          • fifo_read
          • fifo_write
      • fifo_cp
          • fifo_read
          • fifo_write
      • fifo_chat
          • fifo_read
          • fifo_write
  • signal
      • kill
      • signal_alarm
      • signal_continue
      • signal_child
      • signal_user
      • signal_num
      • signal_work
  • shm
      • shm_read
      • shm_write
  • mailbox
      • list.h
      • list.c
      • queue.h
      • queue.c
      • main.c

proc

fork.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>int main(int argc, char *argv[])
{pid_t ret = fork();if(ret>0){while(1){printf("发视频,father\n");sleep(1);}}else if(0 == ret){while(1){printf("接受控制 child\n");sleep(1);}}else {perror("fork");return 1;}return 0;
}

getpid.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>int main(int argc, char *argv[])
{pid_t ret = fork();if(ret>0){while(1){printf("发视频,father,pid:%d ppid:%d\n",getpid(),getppid());sleep(1);}}else if(0 == ret){while(1){printf("接受控制 child pid:%d ppid:%d\n",getpid(),getppid());sleep(1);}}else {perror("fork");return 1;}return 0;
}

n_proc.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>int main(int argc, char *argv[])
{int n = 3;int i =  0;printf("father pid:%u ppid:%u\n",getpid(),getppid());for(i = 0 ;i<n;i++){pid_t pid = fork();if(pid>0){}else if(0 == pid){printf("child pid:%u ppid:%u\n",getpid(),getppid());exit(0);//break;}}return 0;
}

proc_num.c

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h> 
#include <dirent.h>
#include <ctype.h>
#include <time.h>
int isnum(char* filename)
{while(*filename) // 123abc123  457567{if(isdigit((unsigned char )*filename)){}else {return 0;}filename++;}return 1;
}
int do_count()
{int num = 0 ;DIR * dir = opendir("/proc"); //fopen open  if(NULL == dir){printf(" opendir error\n");return 1;}while(1){struct dirent * info = readdir(dir);if(NULL == info){break;}//printf("%s\n",info->d_name);if(DT_DIR ==info->d_type ){if(isnum(info->d_name)){num++;}}}closedir(dir);return num;
}
int main(int argc, char *argv[])
{int num = 0;FILE* fp = fopen("log","w");if(NULL == fp){perror("fopen");}pid_t pid = fork();if(pid>0){int i =3;while(i--){sleep(3);num =  do_count();time_t tm;time(&tm);fprintf(fp,"father %d  %d %s",getpid(),num,ctime(&tm));fflush(fp);}}else if(0 == pid){int i =2;while(i--){sleep(5);num =  do_count();time_t tm;time(&tm);fprintf(fp,"child %d  %d %s",getpid(),num,ctime(&tm));fflush(fp);}}else {perror("fork");exit(1);}fclose(fp);return 0;
}

exit/_exit

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>int main(int argc, char *argv[])
{FILE* fp = fopen("log","w");if(NULL == fp){perror("fopen");return 1;}fputs("aaa ,exit test",fp);exit(0);//_exit(0);return 0;
}

atexit.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
char* buf;
void clean(void)
{printf("clean func...\n");free(buf);
}
int main(int argc, char *argv[])
{FILE* fp = fopen("log","w");if(NULL == fp){perror("fopen");return 1;}buf = (char*)malloc(50);atexit(clean);strcpy(buf,"aaa,exit test");fputs(buf,fp);exit(0);return 0;
}

wait.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>int main(int argc, char *argv[])
{pid_t ret = fork();if(ret>0){printf("father pid %d ppid %d\n",getpid(),getppid());int status;wait(&status);if(WIFEXITED(status)){printf("child 正常结束 ret val %d\n",WEXITSTATUS(status));}if(WIFSIGNALED(status)){printf("child 异常结束,sinal num %d\n",WTERMSIG(status)); }printf("父回收子的资源\n");}else if(0 == ret){printf("child pid %d ppid %d\n",getpid(),getppid());sleep(10);exit(10);}else {perror("fork");return 1;}return 0;
}

waitpid.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>int main(int argc, char *argv[])
{pid_t ret = fork();if(ret>0){printf("father pid %d ppid %d\n",getpid(),getppid());int status;while(1){pid_t pid = waitpid(ret,&status,WNOHANG);if(pid == ret){if(WIFEXITED(status)){printf("child 正常结束 ret val %d\n",WEXITSTATUS(status));}if(WIFSIGNALED(status)){printf("child 异常结束,sinal num %d\n",WTERMSIG(status)); }printf("父回收子的资源\n");break;}else if(0 == pid ){printf("子进程未结束\n");}else if(-1 == pid){printf("pid error\n");break;}}}else if(0 == ret){printf("child pid %d ppid %d\n",getpid(),getppid());sleep(10);exit(10);}else {perror("fork");return 1;}return 0;
}

waitpid_lianxi.c

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>int main(int argc, char *argv[]) {int i = 0;pid_t ret[5] = {0};for (i = 0; i < 5; i++) {ret[i] = fork();if (ret[i] > 0) {} else if (0 == ret[i]) {printf("child pid %d ppid %d\n", getpid(), getppid());sleep(rand() % 5 + 1);exit(1);} else {}}int status;while (1) {pid_t pid = waitpid(ret[2], &status, WNOHANG);if (pid == ret[2]) {if (WIFEXITED(status)) {printf("child 正常结束 ret val %d\n", WEXITSTATUS(status));}if (WIFSIGNALED(status)) {printf("child 异常结束,sinal num %d\n", WTERMSIG(status));}printf("父回收子的资源 %d\n",pid);break;} else if (0 == pid){//printf("子进程未结束\n");} else if (-1 == pid) {printf("pid error\n");break;}}return 0;
}

execl.c

#include <unistd.h>
#include <stdio.h>// ./a.out 1 2 3 4
int	main(int argc, char **argv)
{printf("this is execl test\n");//  ls /home/linux/ -l -a//execl("/bin/ls","ls","/home/linux/", "-l", "-a","--color=auto",NULL);//p ==  echo  $PATH //execlp("ls","ls","/home/linux/", "-l", "-a","--color=auto",NULL);// cat 15execl.c //execlp("cat","cat","15execl.c",NULL);char *const args[]={"ls","/home/linux/", "-l", "-a","--color=auto",NULL};//execv("/bin/ls",args);execvp(args[0],args);printf("看见就错了\n");return 0;
}

exec_work

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>#include <sys/types.h>
#include <sys/wait.h>// ./a.out 1 2 3 4
int main(int argc, char **argv) {while (1) {printf("1.firefox\n");printf("2.calcu\n");printf("3.exit\n");char cmd[5] = {0};fgets(cmd, sizeof(cmd), stdin); // 1\nint choose = atoi(cmd);if(3 == choose ){exit(1);}pid_t pid = fork();if (pid > 0) {wait(NULL);}else if (0 == pid) {switch (choose) {case 1:execlp("/usr/bin/firefox", "firefox", "www.baidu.com", NULL);break;case 2:execlp("/snap/bin/gnome-calculator", "gnome-calculator", NULL);break;exit(1);printf("看见就错了\n");}}}return 0;
}

minishell

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
// ./a.out 1 2 3 4
int main(int argc, char **argv) 
{while(1){char path[512]={0};getcwd(path,sizeof(path));printf("[linux@ubuntu:%s]",path); fflush(stdout);  char cmd[256]={0};fgets(cmd,sizeof(cmd),stdin);cmd[strlen(cmd)-1]='\0';char *args[5]={NULL};args[0]=strtok(cmd," ");args[1]=strtok(NULL," ");args[2]=strtok(NULL," ");// if cd if(0==strcmp(args[0],"cd")){if(NULL == args[1]){chdir("/home/linux");}else  {chdir(args[1]);}}pid_t pid = fork();if(pid>0){wait(NULL);}else  if(0 == pid){// ls -alhF  ll     ll 1.cif(0 == strcmp(args[0],"ll")){args[0]="ls";if(NULL == args[1]){args[1]="-alhF";}else  {args[2]="-alhF";}}execvp(args[0], args);exit(1);}}return 0;
}

thread

pthread_self

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>void *th1 (void *arg){while(1){printf("接受控制 tid %lu\n",pthread_self());sleep(1);}return NULL;}void *th2 (void *arg){while(1){printf("发送视频 tid %lu\n",pthread_self());sleep(1);}return NULL;}int	main(int argc, char **argv)
{pthread_t tid1,tid2;pthread_create(&tid1,NULL,th1,NULL);pthread_create(&tid2,NULL,th2,NULL);while(1){printf("main_th tid %lu\n",pthread_self());sleep(1);}return 0;
}

pthread_exit

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
void* th(void* arg)
{printf("th1 ,tid %lu\n",pthread_self());pthread_exit(NULL);// exit//return NULL;
}
int	main(int argc, char **argv)
{pthread_t tid;pthread_create(&tid,NULL,th,NULL);while(1){}return 0;
}

pthread_cancle

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
void* th(void* arg)
{while(1){printf("th1 ,tid %lu\n",pthread_self());sleep(1);}pthread_exit(NULL);// exit
}
int	main(int argc, char **argv)
{pthread_t tid;pthread_create(&tid,NULL,th,NULL);int i = 0 ;while(1){i++;sleep(1);if(i>=10){pthread_cancel(tid);}}return 0;
}

pthread_limit

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
void* th(void* arg)
{pthread_exit(NULL);// exit
}
int	main(int argc, char **argv)
{pthread_t tid;int i = 0 ;for(i=0;i<50000;i++){int ret = pthread_create(&tid,NULL,th,NULL);if(0!=ret){printf("num %d\n",i);break;}}return 0;
}

pthread_join

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
void* th(void* arg)
{pthread_exit(NULL);// exit
}
int	main(int argc, char **argv)
{pthread_t tid;int i = 0 ;for(i=0;i<50000;i++){int ret = pthread_create(&tid,NULL,th,NULL);if(0!=ret){break;}pthread_join(tid,NULL);}printf("num %d\n",i);return 0;
}

pthread_join2

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
void *th(void *arg)
{//static char buf[]="th1 要消亡";char * buf = (char*)malloc(50);strcpy(buf,"th1 要消亡");pthread_exit(buf); // exit
}
int main(int argc, char **argv){pthread_t tid;int ret = pthread_create(&tid, NULL, th, NULL);void* ret_val=NULL;//pthread_join 是阻塞回收 pthread_join(tid,&ret_val); //ret_val = buf;printf("ret_val %s\n",(char*)ret_val);free(ret_val);return 0;
}

pthread_args

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
void* th(void* arg)
{//printf("th1 ,tid %lu\n",pthread_self());strcat((char*)arg,"th 在来一刀");pthread_exit(arg);// exit//return NULL;
}
int	main(int argc, char **argv)
{pthread_t tid;char buf[100]={0};strcpy(buf,"main th  先砍第一刀。");pthread_create(&tid,NULL,th,buf);void* ret;pthread_join(tid,&ret);strcat((char*)ret,"main th 第三刀");printf("%s\n",buf);return 0;
}

pthread_args2

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>typedef struct 
{char name[50];int age;float score;
}Per;
void* th(void* arg)
{Per* tmp = (Per*)arg;strcpy(tmp->name, "zhangsan");tmp->age  =20;tmp->score =59.9;return arg;
}int	main(int argc, char **argv)
{pthread_t tid;Per per;bzero(&per,sizeof(per));pthread_create(&tid,NULL,th,&per);void* ret;pthread_join(tid,&ret);printf("name:%s age:%d score:%.3f\n", ((Per*)ret)->name,((Per*)ret)->age,((Per*)ret)->score);return 0;
}

pthread_detach

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
void* th(void* arg)
{pthread_detach(pthread_self());pthread_exit(NULL);// exit
}
int	main(int argc, char **argv)
{pthread_t tid;int i = 0 ;for(i=0;i<50000;i++){int ret = pthread_create(&tid,NULL,th,NULL);if(0!=ret){break;}}printf("num %d\n",i);return 0;
}

pthread_cleanup_push

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>void clean(void *arg)
{printf("this is clean fun, %s\n",(char*)arg);free(arg);
}
void* th(void* arg)
{pthread_cleanup_push(clean,arg);printf("th1 ,tid %lu\n",pthread_self());strcpy(arg,"123");printf("%s\n",(char*)arg);pthread_cleanup_pop(1);pthread_exit(NULL);// exit//return NULL;
}
int	main(int argc, char **argv)
{pthread_t tid;char* buf=malloc(50);pthread_create(&tid,NULL,th,buf);pthread_join(tid,NULL);return 0;
}

pthread_cal

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>typedef struct 
{float a;float b;char c;float d;
}Cal;
void* th(void* arg)
{Cal* tmp = (Cal*)arg;switch (tmp->c) {case '+':tmp->d = tmp->a+ tmp->b;break;case '-':tmp->d = tmp->a- tmp->b;break;case '*':tmp->d = tmp->a* tmp->b;break;case '/':tmp->d = tmp->a/ tmp->b;break;default:tmp->d = tmp->a+ tmp->b;break;}return arg;}int	main(int argc, char **argv)
{pthread_t tid;Cal cal;bzero(&cal,sizeof(cal));printf("input express:"); // 3.3 + 4.4char express[20]={0};fgets(express,sizeof(express),stdin);char *args[3]={NULL};args[0] =strtok(express," ");args[1] =strtok(NULL," ");args[2] =strtok(NULL," ");cal.a = atof(args[0]);cal.b = atof(args[2]);cal.c = args[1][0];pthread_create(&tid,NULL,th,&cal);void* ret;pthread_join(tid,&ret);printf("values:%.3f\n", ((Cal*)ret)->d);return 0;
}

pthread_fun_point

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
typedef void* (*PFUN)(void*);
void* th1(void* arg)
{printf("tid %lu\n",pthread_self());pthread_exit(NULL);// exit//return NULL;
}
void* th2(void* arg)
{printf("tid %lu\n",pthread_self());pthread_exit(NULL);// exit//return NULL;
}
void* th3(void* arg)
{printf("tid %lu\n",pthread_self());pthread_exit(NULL);// exit//return NULL;
}
int	main(int argc, char **argv)
{pthread_t tid[5];PFUN th[5]={th1,th2,th3,th1,th2};int i = 0 ;for(i=0;i<5;i++){pthread_create(&tid[i],NULL,th[i],NULL); }for(i=0;i<5;i++){pthread_join(tid[i],NULL); }return 0;
}

pthread_con

pthread_10000

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
int a =  0;void * th(void* arg)
{int i = 5000;while(i--){int tmp = a;printf("a is %d\n",tmp+1);a = tmp+1;}return NULL;
}int	main(int argc, char **argv)
{pthread_t tid1,tid2;pthread_create(&tid1,NULL,th,NULL);pthread_create(&tid2,NULL,th,NULL);pthread_join(tid1,NULL);pthread_join(tid2,NULL);return 0;
}

pthread_mutex

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
int a =  0;
pthread_mutex_t mutex ;
void * th(void* arg)
{int i = 5000;while(i--){pthread_mutex_lock(&mutex);int tmp = a;printf("a is %d\n",tmp+1);a = tmp+1;pthread_mutex_unlock(&mutex);} return NULL;
}int	main(int argc, char **argv)
{pthread_t tid1,tid2;pthread_mutex_init(&mutex,NULL);pthread_create(&tid1,NULL,th,NULL);pthread_create(&tid2,NULL,th,NULL);pthread_join(tid1,NULL);pthread_join(tid2,NULL);pthread_mutex_destroy(&mutex);return 0;
}

pthread_trylock

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
int a = 0;
pthread_mutex_t mutex;
void *th(void *arg) {int i = 5000;while (i--){while (1){int ret = pthread_mutex_trylock(&mutex);if (0 == ret) {int tmp = a;printf("a is %d\n", tmp + 1);a = tmp + 1;pthread_mutex_unlock(&mutex);break;}else{printf("锁被其他线程占用,申请失败,稍后在试\n");}}}return NULL;
}int main(int argc, char **argv) {pthread_t tid1, tid2;pthread_mutex_init(&mutex, NULL);pthread_create(&tid1, NULL, th, NULL);pthread_create(&tid2, NULL, th, NULL);pthread_join(tid1, NULL);pthread_join(tid2, NULL);pthread_mutex_destroy(&mutex);return 0;
}

bank

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>int WIN = 3;
pthread_mutex_t mutex;
void *th(void *arg) {while (1) {pthread_mutex_lock(&mutex);if (WIN > 0){WIN--;pthread_mutex_unlock(&mutex);printf("get win %lu\n", pthread_self());sleep(rand() % 5 + 1);printf("releset win %lu\n", pthread_self());pthread_mutex_lock(&mutex);WIN++;pthread_mutex_unlock(&mutex);break;}else{pthread_mutex_unlock(&mutex);}}return NULL;
}int main(int argc, char **argv) {pthread_t tid[10] = {0};pthread_mutex_init(&mutex, NULL);int i = 0;for (i = 0; i < 10; i++) {pthread_create(&tid[i], NULL, th, NULL);}for (i = 0; i < 10; i++) {pthread_join(tid[i], NULL);}pthread_mutex_destroy(&mutex);return 0;
}

bank_nonblock

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>int WIN = 3;
pthread_mutex_t mutex_win1;
pthread_mutex_t mutex_win2;
pthread_mutex_t mutex_win3;
void do_bank()
{printf("get win %lu\n", pthread_self());sleep(rand() % 5 + 1);printf("releset win %lu\n", pthread_self());
}
void *th(void *arg){int ret = 0 ;while (1) {ret = pthread_mutex_trylock(&mutex_win1);if(0== ret){do_bank();pthread_mutex_unlock(&mutex_win1);break;}ret = pthread_mutex_trylock(&mutex_win2);if(0== ret){do_bank();pthread_mutex_unlock(&mutex_win2);break;}ret = pthread_mutex_trylock(&mutex_win3);if(0== ret){do_bank();pthread_mutex_unlock(&mutex_win3);break;}}return NULL;
}int main(int argc, char **argv) {pthread_t tid[10] = {0};pthread_mutex_init(&mutex_win1, NULL);pthread_mutex_init(&mutex_win2, NULL);pthread_mutex_init(&mutex_win3, NULL);int i = 0;for (i = 0; i < 10; i++) {pthread_create(&tid[i], NULL, th, NULL);}for (i = 0; i < 10; i++) {pthread_join(tid[i], NULL);}pthread_mutex_destroy(&mutex_win1);pthread_mutex_destroy(&mutex_win2);pthread_mutex_destroy(&mutex_win3);return 0;
}

sem

#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <unistd.h>
sem_t sem_H, sem_W;
void *th1(void *arg) {int i = 10;while (i--) {sem_wait(&sem_H); //  P   -1printf("hello,");fflush(stdout);sem_post(&sem_W); // V     +1}return NULL;
}void *th2(void *arg) {int i = 10;while (i--) {sem_wait(&sem_W);printf(" world\n");sem_post(&sem_H);sleep(1);}return NULL;
}int main(int argc, char **argv) {pthread_t tid1, tid2;sem_init(&sem_H, 0, 1);sem_init(&sem_W, 0, 0);pthread_create(&tid1, NULL, th1, NULL);pthread_create(&tid2, NULL, th2, NULL);pthread_join(tid1, NULL);pthread_join(tid2, NULL);sem_destroy(&sem_H);sem_destroy(&sem_W);return 0;
}

bank_sem

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <semaphore.h>//pthread_mutex_t mutex;
sem_t sem_WIN;
void *th(void *arg) 
{sem_wait(&sem_WIN);printf("get win %lu\n", pthread_self());sleep(rand() % 5 + 1);printf("releset win %lu\n", pthread_self());sem_post(&sem_WIN);return NULL;
}int main(int argc, char **argv) {pthread_t tid[10] = {0};//pthread_mutex_init(&mutex, NULL);sem_init(&sem_WIN,0,3);int i = 0;for (i = 0; i < 10; i++) {pthread_create(&tid[i], NULL, th, NULL);}for (i = 0; i < 10; i++) {pthread_join(tid[i], NULL);}//pthread_mutex_destroy(&mutex);sem_destroy(&sem_WIN);return 0;
}

system for shared memory

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <semaphore.h>pthread_mutex_t mutex;
sem_t sem_MEM;
int MEM[3]={0};
int get_mem()
{int ret = -1;sem_wait(&sem_MEM);int i = 0 ;pthread_mutex_lock(&mutex);for(i = 0 ;i<3;i++){if(0 == MEM[i]){MEM[i]=1;//return i;ret = i;break;}}pthread_mutex_unlock(&mutex);return ret;}int relese_mem(int id){MEM[id]= 0;sem_post(&sem_MEM);}
void *th(void *arg) 
{int id = get_mem();printf("tid:%lu get memid %d\n",pthread_self(),id);sleep(rand()%5+1);relese_mem(id);printf("tid:%lu relese  memid %d\n",pthread_self(),id);return NULL;
}int main(int argc, char **argv) {pthread_t tid[10] = {0};pthread_mutex_init(&mutex, NULL);sem_init(&sem_MEM,0,3);int i = 0;for (i = 0; i < 10; i++) {pthread_create(&tid[i], NULL, th, NULL);}for (i = 0; i < 10; i++) {pthread_join(tid[i], NULL);}pthread_mutex_destroy(&mutex);sem_destroy(&sem_MEM);return 0;
}

find_define


#include "SeqQueue.h"
#include <dirent.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>pthread_mutex_t mutex;
sem_t sem_task;
typedef struct {SeqQueue *sq;FILE *fp;
} TH_ARG;
int find_define(char *filename, FILE *dstfp) 
{if(strlen(filename)<3){return 1;}if (0 == strcmp(&filename[strlen(filename) - 2], ".h")) {FILE *fp = fopen(filename, "r");if (NULL == fp) {printf("fopen error\n");return 1;}char buf[128] = {0};int num = 1;while (1) {char *ret = fgets(buf, sizeof(buf), fp);if (NULL == ret) {break;}// printf("%s",buf);if (strstr(buf, "#define")) {fprintf(dstfp, "%s %d %s\n", buf, num, filename);fflush(dstfp);}num++;}fclose(fp);}return 0;
}
void *th(void *arg) {TH_ARG *tmp = (TH_ARG *)arg;DATATYPE *dat;DATATYPE backdata;while (1) {sem_wait(&sem_task);dat = GetHeadSeqQueue(tmp->sq);memcpy(&backdata, dat, sizeof(DATATYPE));QuitSeqQueue(tmp->sq);find_define(backdata.path, tmp->fp);}return NULL;
}int do_ls(char *path, SeqQueue *sq, FILE *fp) {DIR *dir = opendir(path);if (NULL == dir) {printf(" opendir error\n");return 1;}char newpath[512] = {0};DATATYPE data;while (1) {bzero(&data, sizeof(data));struct dirent *info = readdir(dir);if (NULL == info) {break;}// printf("%s\n",info->d_name);sprintf(newpath, "%s/%s", path, info->d_name); // /home/linux/1if (DT_DIR == info->d_type) {if (0 == strcmp(info->d_name, ".") || 0 == strcmp(info->d_name, "..")) {continue;}// do_ls(newpath);strcpy(data.path, newpath);EnterSeqQueue(sq, &data);sem_post(&sem_task);} else {find_define(newpath, fp);}}closedir(dir);return 0;
}int main(int argc, char **argv) {pthread_t tid[3] = {0};SeqQueue *sq = CreateSeqQueue(1024);FILE *fp = fopen("log", "w");if (NULL == fp) {return 1;}sem_init(&sem_task, 0, 0);TH_ARG args = {sq, fp};int i = 0;for (i = 0; i < 3; i++) {pthread_create(&tid[i], NULL, th, &args);}do_ls("/home/linux", sq, fp);for (i = 0; i < 3; i++) {pthread_join(tid[i], NULL);}sem_destroy(&sem_task);return 0;
}

ipc

pipe

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int	main(int argc, char **argv)
{int pipefd[2]={0};int ret = pipe(pipefd);pid_t pid = fork();if(pid>0){close(pipefd[0]);char buf[]="hello,this is pipe test";sleep(3);write(pipefd[1],buf,strlen(buf));exit(0);}else if (0 == pid){close(pipefd[1]);char buf[256]={0};read(pipefd[0],buf,sizeof(buf));printf("pipe %s\n",buf);}else  {perror("fork");exit(1);}return 0;
}

pipe_write_block

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int	main(int argc, char **argv)
{int pipefd[2]={0};int ret = pipe(pipefd);pid_t pid = fork();if(pid>0){close(pipefd[0]);char buf[1024]={0};memset(buf,'a',sizeof(buf));int i = 0 ;for(i=0;i<65;i++){write(pipefd[1],buf,sizeof(buf));printf("i is %d\n",i);}exit(0);}else if (0 == pid){close(pipefd[1]);sleep(3);char buf[4096]={0};read(pipefd[0],buf,sizeof(buf));sleep(1);printf("pipe %s\n",buf);}else  {perror("fork");exit(1);}return 0;
}

pipe_write_broken.c

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int	main(int argc, char **argv)
{int pipefd[2]={0};int ret = pipe(pipefd);pid_t pid = fork();if(pid>0){sleep(1);close(pipefd[0]);char buf[]="hello,this is pipe test";write(pipefd[1],buf,strlen(buf)); //需要gdb才能观测到printf("after write\n");exit(0);}else if (0 == pid){close(pipefd[1]);close(pipefd[0]);sleep(10);}else  {perror("fork");exit(1);}return 0;
}

fifo

fifo_read
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h> 
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
int main(int argc, char *argv[])
{int ret = mkfifo("myfifo",0666);if(-1 == ret){if(EEXIST==errno ){}else {perror("mkfifo");return 1;}}int fd = open("myfifo",O_RDONLY);if(-1 == fd){perror("open");return 1;}char buf[128]={0};read(fd,buf,sizeof(buf));printf("fifo:%s\n",buf);close(fd);//remove("myfifo");return 0;
}
fifo_write
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h> 
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
int main(int argc, char *argv[])
{int ret = mkfifo("myfifo",0666);if(-1 == ret){if(EEXIST==errno ){}else {perror("mkfifo");return 1;}}int fd = open("myfifo",O_WRONLY);if(-1 == fd){perror("open");return 1;}char buf[]="hello ,this is fifo test";write(fd,buf,strlen(buf));close(fd);return 0;
}

fifo_cp

fifo_read
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h> 
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
int main(int argc, char *argv[])
{int ret = mkfifo("myfifo",0666);if(-1 == ret){if(EEXIST==errno ){}else {perror("mkfifo");return 1;}}int fd = open("myfifo",O_RDONLY);if(-1 == fd){perror("open");return 1;}int dstfd = open("2.png",O_WRONLY|O_CREAT|O_TRUNC,0666);if(-1 == dstfd){perror("open");return 1;}while(1){char buf[1024]={0};int rd_ret = read(fd,buf,sizeof(buf));if(rd_ret<=0){break;}write(dstfd,buf,rd_ret);}close(fd);//remove("myfifo");return 0;
}
fifo_write
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h> 
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
int main(int argc, char *argv[])
{int ret = mkfifo("myfifo",0666);if(-1 == ret){if(EEXIST==errno ){}else {perror("mkfifo");return 1;}}int fd = open("myfifo",O_WRONLY);if(-1 == fd){perror("open");return 1;}int srcfd = open("/home/linux/1.png",O_RDONLY);if(-1 == srcfd){perror("srcfd");return 1;}while(1){char buf[1024]={0};int rd_ret = read(srcfd,buf,sizeof(buf));if( rd_ret<=0){break;}write(fd,buf,rd_ret);}close(fd);return 0;
}

fifo_chat

fifo_read
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h> 
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
void* th1(void* arg)
{int fd = *(int*)arg;while(1){char buf[256]={0};read(fd,buf,sizeof(buf));printf("from b: %s",buf);fflush(stdout);}return NULL;}
void* th2(void* arg)
{int fd = *(int*)arg;while(1){char buf[256]={0};printf("to B");fgets(buf,sizeof(buf),stdin);write(fd,buf,strlen(buf));}
}
int main(int argc, char *argv[])
{int ret = mkfifo("myfifo1",0666);if(-1 == ret){if(EEXIST==errno ){}else {perror("mkfifo1");return 1;}}ret = mkfifo("myfifo2",0666);if(-1 == ret){if(EEXIST==errno ){}else {perror("mkfifo2");return 1;}}int fd_r = open("myfifo1",O_RDONLY);if(-1 == fd_r){perror("open");return 1;}int fd_w = open("myfifo2",O_WRONLY);if(-1 == fd_r){perror("open");return 1;}pthread_t tid1,tid2;pthread_create(&tid1,NULL,th1,&fd_r);pthread_create(&tid2,NULL,th2,&fd_w);pthread_join(tid1,NULL);pthread_join(tid2,NULL);//remove("myfifo");return 0;
}
fifo_write
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h> 
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
void* th1(void* arg)
{int fd = *(int*)arg;while(1){char buf[256]={0};int ret =  read(fd,buf,sizeof(buf));if(ret<=0){//break;exit(1);}if(0 == strcmp(buf,"#quit\n")){exit(1);}printf("from a: %s",buf);fflush(stdout);}return NULL;}
void* th2(void* arg)
{int fd = *(int*)arg;while(1){char buf[256]={0};printf("to a");fgets(buf,sizeof(buf),stdin);write(fd,buf,strlen(buf));if(0 == strcmp(buf,"#quit\n")){exit(1);}}
}
int main(int argc, char *argv[])
{int ret = mkfifo("myfifo1",0666);if(-1 == ret){if(EEXIST==errno ){}else {perror("mkfifo1");return 1;}}ret = mkfifo("myfifo2",0666);if(-1 == ret){if(EEXIST==errno ){}else {perror("mkfifo2");return 1;}}int fd_w = open("myfifo1",O_WRONLY);if(-1 == fd_w){perror("open");return 1;}int fd_r = open("myfifo2",O_RDONLY);if(-1 == fd_r){perror("open");return 1;}pthread_t tid1,tid2;pthread_create(&tid1,NULL,th1,&fd_r);pthread_create(&tid2,NULL,th2,&fd_w);pthread_join(tid1,NULL);pthread_join(tid2,NULL);//remove("myfifo");return 0;
}

signal

kill

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
##include <sys/types.h>
#include <signal.h>// ./a.out 9 1000
int	main(int argc, char **argv)
{if(argc<3){printf("usage:./a.out signalnum pid\n");return 1;}pid_t pid = atoi(argv[2]);int signalenum = atoi(argv[1]);int ret = kill(pid,signalenum);if(-1 == ret){perror("kill");return 1;}return 0;
}

signal_alarm

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
int flag = 0;
void myhanle(int num)
{flag = 1;}
int	main(int argc, char **argv)
{signal(SIGALRM,myhanle);alarm(5);while(1){if(0 == flag){printf("i'm sleep\n"); }else  {printf("i'm working\n");}  sleep(1);}return 0;
}

signal_continue

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>void myhandle(int num)
{
}int	main(int argc, char **argv)
{signal(SIGCONT,myhandle);int i = 0 ;while(1){printf("i'm sleep %d\n",getpid());sleep(1);i++;if(5==i){pause();}}return 0;
}

signal_child

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <wait.h>
void myhandle(int num)
{pid_t pid = wait(NULL);printf("recycle pid %d\n",pid);
}int main(int argc, char *argv[])
{signal(SIGCHLD,myhandle);pid_t ret = fork();if(ret>0){int i = 10;while(i--){printf("发视频,father\n");sleep(1);}}else if(0 == ret){int i =5;while(i--){printf("接受控制 child pid:%d\n",getpid());sleep(1);}}else {perror("fork");return 1;}return 0;
}

signal_user

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>void myhandle1(int num)
{static int i =0;printf("老爸,叫你帮忙\n");i++;if(i>=3){signal(SIGUSR1,SIG_IGN);}}void myhandle2(int num)
{static int i =0;printf("老妈,叫你帮忙\n");i++;if(i>=3){signal(SIGUSR2,SIG_DFL);}}int	main(int argc, char **argv)
{signal(SIGUSR1,myhandle1);signal(SIGUSR2,myhandle2);while(1){printf("i'm play... %d\n",getpid());sleep(1);}return 0;
}

signal_num

#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>void myhandle1(int num) {static int i = 0;static int j = 0;if (SIGUSR1 == num) {printf("老爸,叫你帮忙\n");i++;if (i >= 3){signal(SIGUSR1, SIG_IGN);}}if (SIGUSR2 == num){printf("老妈,叫你帮忙\n");j++;if (j >= 3) {signal(SIGUSR2, SIG_DFL);}}
}int main(int argc, char **argv) 
{signal(SIGUSR1, myhandle1);signal(SIGUSR2, myhandle1);while (1) {printf("i'm play... %d\n", getpid());sleep(1);}return 0;
}

signal_work

#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>void do_cat(char *filename) {FILE *fp = fopen(filename, "r");if (NULL == fp) {printf("fopen error\n");return;}char buf[128] = {0};while (1) {char *ret = fgets(buf, sizeof(buf), fp);if (NULL == ret) {break;}printf("%s", buf);fflush(stdout);}fclose(fp);
}void myhandle1(int num) { do_cat("01kill.c"); }void myhandle2(int num) { do_cat("02alarm.c"); }int main(int argc, char **argv) {pid_t pid = fork();if (pid > 0) {while (1) {char buf[5] = {0};fgets(buf, sizeof(buf), stdin);int num = atoi(buf);switch (num) {case 1:kill(pid, SIGUSR1);break;case 2:kill(pid, SIGUSR2);break;case 3:kill(pid, SIGINT);exit(1);break;default:kill(pid, SIGINT);exit(1);}}} else if (0 == pid) {signal(SIGUSR1, myhandle1);signal(SIGUSR2, myhandle2);while (1) {printf("child ,pid %d\n", getpid());sleep(1);}} else {perror("fork");exit(1);}return 0;
}

shm

shm_read

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h> 
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>#include <sys/ipc.h>
#include <sys/shm.h>int	main(int argc, char **argv)
{key_t key = ftok("./",'#');if(-1 == key){perror("ftok");return 1;}int shmid = shmget(key,4096,IPC_CREAT|0666);if(-1 == shmid){perror("shmget");return 1;}void * p = shmat(shmid,NULL,!SHM_RDONLY);if( (void *) -1  ==  p){perror("shmat");return 1;}printf("mem:%s\n",(char*)p);shmdt(p);    //shmctl(shmid,IPC_RMID,NULL);return 0;
}

shm_write

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h> 
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>#include <sys/ipc.h>
#include <sys/shm.h>int	main(int argc, char **argv)
{key_t key = ftok("./",'#');if(-1 == key){perror("ftok");return 1;}printf("key 0x%x\n",key);int shmid = shmget(key,4096,IPC_CREAT|0666);if(-1 == shmid){perror("shmget");return 1;}void * p = shmat(shmid,NULL,!SHM_RDONLY);if( (void *) -1  ==  p){perror("shmat");return 1;}char buf[]="hello,shm test";memcpy(p,buf,strlen(buf));shmdt(p);    return 0;
}

mailbox

list.h

#ifndef __LIST_H__
#define __LIST_H__
#include <pthread.h>
#include <stdlib.h>
#include <string.h>typedef void*(*th_fun)(void* arg);
typedef char DATATYPE[256];
typedef struct mail_data
{pthread_t   id_of_sender;char       name_of_sender[256];pthread_t   id_of_recver;char       name_of_recver[256];DATATYPE data;
}MAIL_DATA;
typedef struct queue{MAIL_DATA data;struct queue* next;
//	int pro;
}Que, *pQue;
typedef struct thread_node
{pthread_t tid;         //线程id号char name[256];        //线程名字 ,必须唯一Que *mail_head, *mail_tail;th_fun th;
}LIST_DATA;typedef struct Link{LIST_DATA elem;struct Link *next;
}LIST_LINK;extern LIST_LINK * list_init();
extern LIST_LINK * list_for_each(LIST_LINK* head, char *name);extern void list_add(LIST_LINK *head, LIST_LINK *info);
#endif

list.c

#include "list.h"LIST_LINK * list_init()
{LIST_LINK *temp = malloc(sizeof(LIST_LINK));temp->next = NULL;return temp;
}void list_add(LIST_LINK* head, LIST_LINK *info)
{info->next = head->next;head->next = info;}LIST_LINK * list_for_each(LIST_LINK* head, char *name)
{LIST_LINK * tmp = NULL;tmp = head;while(tmp->next != NULL){if(strncmp(tmp->elem.name, name, strlen(name)) == 0)return tmp;tmp = tmp->next;}if(strncmp(tmp->elem.name, name, strlen(name)) == 0)return tmp;elsereturn NULL;
}

queue.h

#ifndef __QUEUE_H__
#define __QUEUE_H__
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>#include "list.h"extern int init_que(LIST_LINK *list_head);
extern int in_queue(LIST_LINK *list_head, MAIL_DATA *data);
extern int out_queue(LIST_LINK *list_head, MAIL_DATA *data);
extern void destroy(LIST_LINK *list_head);#endif

queue.c

#include "queue.h"/*
typedef	 int*  pi;
#define	p	int*
pi a, b;
p  c, d;Que* head, tail;
*///pQue head, tail;
Que *head, *tail;int init_que(LIST_LINK *list_head)
{Que* tmp = malloc(sizeof(Que));tmp->next = NULL;list_head->elem.mail_head = list_head->elem.mail_tail = tmp;return 0;
}int in_queue(LIST_LINK *list_head, MAIL_DATA *data)
{Que* tmp = malloc(sizeof(Que));memcpy(&tmp->data, data, sizeof(MAIL_DATA));tmp->next = NULL;list_head->elem.mail_tail->next = tmp;list_head->elem.mail_tail = list_head->elem.mail_tail->next;return 0;
}int out_queue(LIST_LINK *list_head, MAIL_DATA *data)
{if(list_head->elem.mail_head == list_head->elem.mail_tail){printf("queue is empty. \n");return -1;}if(list_head->elem.mail_head->next == list_head->elem.mail_tail){list_head->elem.mail_tail = list_head->elem.mail_head;}Que* del = list_head->elem.mail_head->next;list_head->elem.mail_head->next = del->next;memcpy(data, &del->data, sizeof(MAIL_DATA));free(del);return 0;
}void destroy(LIST_LINK *list_head)
{Que* tmp = list_head->elem.mail_head;while(list_head->elem.mail_head){tmp = list_head->elem.mail_head;list_head->elem.mail_head = list_head->elem.mail_head->next;free(tmp);		}
}

main.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include "list.h"#include "queue.h"
#define ENTER_CRITICAL_AREA(mutex)  do{pthread_mutex_lock(mutex);}while(0)
#define QUIT_CRITICAL_AREA(mutex)  do{pthread_mutex_unlock(mutex);}while(0)unsigned pthread_index;LIST_LINK *end_list = NULL;//typedef struct thread_node
//{
//    pthread_t tid;         //线程id号
//    char name[256];        //线程名字 ,必须唯一
//    Que *amil_head, *mail_tail;
//    th_fun th;              //线程函数//}TH_NODE;typedef struct mail_box_system
{pthread_mutex_t mutex;  //保护邮件系统LIST_LINK *thread_list;
}MBS;
MBS* mbs;int send_msg(MBS*msb,char*recvname,DATATYPE data);
int recv_msg(MBS*msb,char*sendname,DATATYPE data);MBS* create_mail_box_system()
{MBS *temp =(MBS*)malloc(sizeof(MBS));if(NULL ==  temp){perror("create_mail_box_system mutex malloc failure\n");return NULL;}int ret = pthread_mutex_init(&temp->mutex,NULL);if(0 != ret){perror("create_mail_box_system mutex init failure\n");return NULL;}temp->thread_list = malloc(sizeof(LIST_LINK));// memset(temp->thread_list, 0, sizeof(LIST_LINK));temp->thread_list->next = NULL;printf("mail box create ok!! \n");return temp;
}int destroy_mail_box_system(MBS*mbs)
{pthread_mutex_destroy(&mbs->mutex);LIST_LINK *temp = NULL;LIST_LINK *find = mbs->thread_list;while(find !=  NULL){temp = find;find = find->next;free(temp);}free(find);return 0;
}void* data_collect_th(void* arg)
{while(1){
//        printf("this is the data th\n");sleep(3);send_msg(mbs,"show","aabb");send_msg(mbs,"show","ccddee");send_msg(mbs,"show","nihao");send_msg(mbs,"sock","1122");send_msg(mbs,"sock","3344");send_msg(mbs,"sock","88182172");}return NULL;
}void* show_th(void* arg)
{while(1){
//        printf("this is the show th\n");char sendname[256];DATATYPE data;recv_msg(mbs,sendname,data);printf("show recv msg from %s msg is %s\n", sendname, data);sleep(1);}return NULL;
}void* sock_th(void* arg)
{while(1){
//        printf("this is the sock th\n");DATATYPE data;char sendname[256];recv_msg(mbs,sendname,data);send_msg(mbs, "show", "I'm sock");printf("scok recv msg from %s msg is %s\n", sendname, data);sleep(1);}return NULL;
}char *get_th_name(MBS*msb)
{pthread_t tid = pthread_self();LIST_LINK *find = msb->thread_list;LIST_LINK *end = end_list;while(find != end){if(find->elem.tid == tid)break;find = find->next;}if(find->elem.tid == tid){//printf("cant find the recv th\n");return find->elem.name;}elsereturn NULL;
}int  register_to_mail_system(MBS *mbs,char name[],th_fun th)
{LIST_LINK* temp =  malloc(sizeof(LIST_LINK));if(NULL == temp){perror("register to mail malloc  \n");return -1;}strcpy(temp->elem.name ,name);temp->elem.th = th;temp->next = NULL;init_que(temp);pthread_t ret = pthread_create(&(temp->elem.tid),NULL,th,NULL);if(0!=ret){perror("register to mail thread create\n");return -1;}list_add(mbs->thread_list, temp);printf("register mail system  |%s|  ok \n", temp->elem.name);return 0;
}int unregister_from_mailbox(MBS*msb,char*name)
{LIST_LINK* find=msb->thread_list->next;LIST_LINK *temp = NULL;while(find !=  NULL){temp = find;find = find->next;if(0 == strcmp(temp->elem.name ,name)){destroy(find);free(temp);return 0;}}if(0 == strcmp(find->elem.name ,name)){destroy(find);free(find);return 0;}return -1;
}int wait_all_end(MBS*msb)
{LIST_LINK *find=msb->thread_list->next;LIST_LINK *end=end_list;while(find != end){// pthread_join(find,NULL);pthread_join(find->elem.tid,NULL);find = find->next;}pthread_join(find->elem.tid,NULL);return 0;
}int send_msg(MBS*msb, char*recvname, DATATYPE data)
{MAIL_DATA* temp =  malloc(sizeof(MAIL_DATA));strcpy(temp->data, data);temp->id_of_sender = pthread_self();LIST_LINK *find = list_for_each(msb->thread_list, recvname);if (find == NULL){printf("can,t find msg \n");}char* name = get_th_name(msb);strcpy(temp->name_of_sender,name);strcpy(temp->name_of_recver,recvname);ENTER_CRITICAL_AREA(&msb->mutex);in_queue(find, temp);QUIT_CRITICAL_AREA(&msb->mutex);
//    printf("send msg is ok |%s| msg is %s\n", temp->name_of_recver, temp->data);return 0;}int recv_msg(MBS*msb,char*sendname,DATATYPE data)
{MAIL_DATA* temp =  malloc(sizeof(MAIL_DATA));pthread_t tid =  pthread_self();LIST_LINK *find = msb->thread_list;while(find != NULL){if( find->elem.tid == tid)break;find = find->next;}if( find->elem.tid == tid){while (1){if(find->elem.mail_head != find->elem.mail_tail){ENTER_CRITICAL_AREA(&msb->mutex);out_queue(find, temp);QUIT_CRITICAL_AREA(&msb->mutex);break;}}}strcpy(sendname, temp->name_of_sender);strcpy(data, temp->data);free(temp);return 0;}int main()
{mbs = create_mail_box_system();register_to_mail_system(mbs,"show",show_th);register_to_mail_system(mbs,"sock",sock_th);register_to_mail_system(mbs,"data",data_collect_th);wait_all_end(mbs);destroy_mail_box_system(mbs);printf("Hello World!");return 0;
}

http://www.ppmy.cn/embedded/173855.html

相关文章

51单片机和STM32 入门分析

51单片机和STM32是嵌入式开发中两种主流的微控制器&#xff0c;它们在架构、性能、应用场景等方面存在显著差异。以下是两者的对比分析及选择建议&#xff1a; 1. 51单片机与STM32的定义与特点 51单片机 定义&#xff1a;基于Intel 8051内核的8位微控制器&#xff0c;结构简单…

STM32 系统滴答定时器

定时器 定时器的本质:有规律的计数 有规律:计数速度 来自总线时钟频率 计数 :定时器中会后可控计数器 定时器形成时间的原理&#xff1a;(时间) 通过时钟源可知1s计数次数(计数频率) 通过要定时的秒数 * 每秒计数的次数 来决定定时器中的计数器所需要计数的次数 例子&am…

C++ 学习笔记(三)—— 入门+类和对象

1、内联函数&#xff08;inline&#xff09; 内联函数主要是解决C语言的宏的缺陷提出来的&#xff1b; 宏的缺陷&#xff1a; 1&#xff09;容易出错&#xff0c;语法坑很多&#xff1b; 2&#xff09;不能调试&#xff1b; 3&#xff09;没有类型安全的检查&#xff1b; 宏的…

藻华自用资料四——无人机(ardupilot搭建)

2025.3.17 无人机Ardupilot开发环境的搭建 1.安装git sudo apt-get install git 2.登陆官方仓库 https://github.com/ArduPilot/ardupilot 建立工作目录 mkdir fly 下载相关包 git clone -b Copter-4.0.7 https://github.com/ArduPilot/ardupilot.git 创建分支&#…

AI学习第二天--大模型压缩(量化、剪枝、蒸馏、低秩分解)

目录 1. 量化&#xff1a;压缩大象的“脂肪” 比喻 技术逻辑 2. 剪枝&#xff1a;修剪大象的“无效毛发” 比喻 技术逻辑 3. 知识蒸馏&#xff1a;让大象“师从巨象” 比喻 技术逻辑 4. 低秩分解&#xff1a;把大象“折叠成纸偶” 比喻 技术逻辑 5. 推理优化&#…

堆排序:力扣215.数组中的第K个大元素

一、问题描述 在一个整数数组 nums 中&#xff0c;需要找出第 k 个最大的元素。这里要注意&#xff0c;我们要找的是数组排序后的第 k 个最大元素&#xff0c;而不是第 k 个不同的元素。例如&#xff0c;对于数组 [3,2,1,5,6,4]&#xff0c;当 k 2 时&#xff0c;第 2 个最大…

LLVM学习-- 构建和安装

一 LLVM版本 二 适用预构建的二进制文件安装LLVM 三 适用包管理器安装LLVM 四 从源码构建用于Linux的LLVM 五 从源码构建用于Windows和Visual Studio的LLVM 六 从源码构建用于MacOS 和XCode的LLVM 1.1 LLVM项目从10年前第一次发布到版本3.4&#xff0c;其SVN存储库包含了超过20…

matlab 火电厂给水控制系统仿真

1、内容简介 略 matlab157-火电厂给水控制系统仿真 可以交流、咨询、答疑 2、内容说明 略 摘 要 虽然现在新能源发电领域比较火爆&#xff0c;但至今火力发电厂依然在我的的发电领域中拥有很重要的地位。我国虽然还是发展中国家&#xff0c;但是近年来GDP的增长已经处于世界…