/// void *thread(void *str); pthread_t pth; /*创建线程并执行线程执行函数*/ int ret = pthread_create(&pth, NULL, thread, NULL); pthread_t pthread_self();//得到线程id /*线程处理函数pthread_cleanup_push / pthread_cleanup_pop 线程可以安排它退出时需要调用的函数,这样的函数称为线程清理处理程序,线程可以建立多个清理处理程序。处理程序记录在栈中,也就是说它们的执行顺序与它们注册时的顺序相反。 pthread_cleanup_push来注册清理函数rtn,这个函数有一个参数arg。在以下三种情形之一发生时,注册的清理函数被执行: 1)调用pthread_exit。 2)作为对取消线程请求(pthread_cancel)的响应。 3)以非0参数调用pthread_cleanup_pop。 注意: 1)如果线程只是由于简单的返回而终止的,则清除函数不会被调用。 2)如果pthread_cleanup_pop被传递0参数,则清除函数不会被调用,但是会清除处于栈顶的清理函数。 */ #include <pthread.h> sem_t s1; sem_init (&thiz->s1, 0, 1);//初始化信号量,值为1. sem_destroy (&thiz->s1); sem_wait (&thiz->s2); sem_post (&thiz->s1); pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_lock(&mutex); pthread_mutex_unlock(&mutex); // /*调用execl函数,用可执行程序file_creat替换本进程*/ if(execl("./file_creat","file_creat",argv[1],NULL)<0) perror("execl error!"); sleep(1); //gcc库,使进程休眠,sleep()函数是以秒为单位的,sleep(1);就是休眠1秒。 void usleep(unsigned long usec); //使线程休眠函数,微妙为单位休眠,#include <system.h>。 delay(unsigned int msec);//如果想延时一秒钟的话,可以用delay(250); //无名管道 int pipe_fd[2]; pipe(pipe_fd);/*创建管道*/ if((pid=fork())==0) //子进程执行序列 { close(pipe_fd[1]);//子进程先关闭了管道的写端 r_num=read(pipe_fd[0],buf_r,100); close(pipe_fd[0]); } else if(pid>0) //父进程执行序列 { close(pipe_fd[0]); //父进程先关闭了管道的读端 write(pipe_fd[1],"Hello",5); close(pipe_fd[1]); } //有名管道写,创建 mkfifo("/tmp/myfifo",O_CREAT|O_EXCL|O_RDWR); /*创建有名管道*/ int fd=open("/tmp/myfifo",O_RDWR|O_NONBLOCK,0); write(fd,w_buf,100)/* 向管道写入数据 */ close(fd); //关闭管道 //有名管道读 fd=open("/tmp/myfifo",O_RDONLY|O_NONBLOCK,0);/* 打开管道 */ read(fd,buf_r,100); close(fd); //关闭管道 unlink("/tmp/myfifo"); //删除文件 /*注册信号处理函数*/ signal(SIGBUS,my_func); ... /*自定义信号处理函数*/ void my_func(int sign_no) { if(sign_no==SIGBUS) printf("I have get SIGBUS\n"); } ///\ /*创建消息队列*/ msgid=msgget((key_t)1234,0666 | IPC_CREAT); /*读取消息*/ msgrcv(msgid,(void *)&some_data,BUFSIZ,msg_to_receive,0); /*添加消息*/ msgsnd(msgid,(void *)&some_data,MAX_TEXT,0) /*从系统内核中移走消息队列*/ msgctl(msgid,IPC_RMID,0);
char cwd_buf[80]; getcwd(cwd_buf,sizeof(cwd_buf));/* 获取当前目录 */ DIR *dir = opendir(cwd_buf); while((ptr = readdir(dir)) != NULL){/* 遍历整个目录 */ if((ptr->d_type == 8) && (p=strstr(ptr->d_name,".mp3")) != NULL) {/*查找后缀名为mp3的普通文件 */ strcpy(name[i++],ptr->d_name); } } int memory_id; memory_id = shmget(IPC_PRIVATE, sizeof(struct share), IPC_CREAT|0666)) shmadd = shmat(memory_id, (void *)0, 0) //这就是获得这个id的共享内存地址 /* 父进程中使用共享内存, 获取孙子进程中记录的进程ID */ while(1){ node_tmp = node_tmp->next;//切换到下一首歌 if(fork() == 0){ shmadd = shmat(memory_id, (void *)0, 0);/* 映射共享内存 */ share_para = (struct share *)shmadd; share_para->id_tmp = getpid();/* 将ID存入共享内存 */ if(execlp("madplay","madplay",node_tmp->s_name,NULL)<0) perror("execlp "); } else wait(NULL);//父进程阻塞直到子进程死掉 } share_tmp = (struct share *)shmadd; kill(share_tmp->id_tmp, SIGSTOP);//pause kill(share_tmp->id_tmp,SIGCONT); //continue
学习国嵌实验手册
最新推荐文章于 2025-03-10 20:17:27 发布