/信号******/
/*
#include <signal.h>//信号
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct sigaction
{
void (sa_handler)(int);//新的信号处理函数
void (sa_sigaction)(int,siginfo_t,void);//
sigset_t sa_mask;
int sa_flags;
void (*sa_restorer)(void);
}
void HandleSecondSig(int sig);//新的新号处理函数
int main(int argc,char* argv[])
{
struct sigaction act;//定义一个信号结构体表量
act.sa_handler=HandleSecondSig;
sigemptyset(&act.sa_mask);//清空此信号集
act.sa_flags=0;//其他标志位
sigaction(2,&act,NULL);//设置信号处理方式
while(1)
{
printf("hello world\n");
sleep(1);
}
return 0;
}
void HandleSecondSig(int sig)
{
printf("%d coming\n",sig);
}
//匿名创建管道
#include <sys/types.h>//系统
#include <unistd.h>
#include <stdio.h>//c库文件
#include <stdlib.h>
#include <string.h>
int main(int argc,char* argv[])
{
int arrfd[2]={0}; //两个文件描素符,f[0]读管道 f[1]写管道
int ret=0;
pid_t cid; //表示fork的返回值
ret=pipe(arrfd); //创建管道 int pipe(int pipefd[2]);参数实指针
if(ret!=0)
{
printf("create pipe failed");
return 1;
}
cid=fork(); //创建进程
if (cid<0)
{
printf("create fork failed");
close(arrfd[1]); //关闭管道
close(arrfd[0]);
}
else if(cid==0) //子进程
{
close(arrfd[1]);
char buf[6]="";
read(arrfd[0],buf,6);
printf("%s from parent process ",buf);
close(arrfd[0]);
}
else //主进程
{
char buf[]="Hello";
close(arrfd[0]);
write(arrfd[1],buf,6);
close(arrfd[1]);
}
return 0;
}
//example 2
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc,char* argv[])
{
int arrfd[2];
int ret=0;
pid_t cid;
char buf[64]="";
ret=pipe(arrfd);
if(ret!=0)
{
printf("create pipe failed");
return 1;
}
cid=fork();
if(cid<0)
{
printf("create fork failed");
close(arrfd[0]);
close(arrfd[1]);
return 2;
}
else if(cid==0)
{
close(arrfd[1]);
while(1)
{
memset(buf,0,64);
read(arrfd[0],buf,64);
printf("%s come from parent process\n",buf);
if(strcmp(buf,"exit")==0)
{
break;
}
}
close(arrfd[0]);
}
else
{
int len=0;
close(arrfd[0]);
while(1)
{
printf("Please input a string:\n");
fgets(buf,64,stdin);//
len=strlen(buf);
if(buf[len-1]=='\n')//最后一个字符
{
buf[len-1]='\0';
len--;
}
write(arrfd[1],buf,len);
if(strcmp(buf,"exit")==0)
{
break;
}
}
close(arrfd[1]);
}
return 0;
}
//创建一个有名管道
#include <unistd.h>
#include <sys/types.h>//类型头文件,定义了基本的数据类型
#include <fcntl.h>//文件控制选项头文件 主要是fcntl函数 open选项
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FIFO_NAME “/tmp/myfifo” //宏定义
int main(int argc,char* argv[])
{
int ret=0;
int rfd=0;
char buf[8]="";
if(access(FIFO_NAME,F_OK)!=0) //判断文件访问权限 判断文件是否存在
{
ret=mkfifo(FIFO_NAME,0666);//int mkfifo(const char* pathname,mode_e mode) 成功返回零 失败返回-1 指定目录路径 设置权限
if(ret!=0)
{
printf("mkfifo failed\n");
return 1;
}
}
rfd=open(FIFO_NAME,O_RDONLY);//打开管道文件描素符
read(rfd,buf,5);
printf("%s from first process\n",buf);
close(rfd);
return 0;
}
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FIFO_NAME “/tmp/myfifo”
int main(int argc,char* argv[])
{
int ret=0;
int wfd=0;
if(access(FIFO_NAME,F_OK)!=0)
{
ret=mkfifo(FIFO_NAME,0666);
if(ret!=0)
{
printf(“create fifo failed”);
return 1;
}
}
wfd=open(FIFO_NAME,O_WRONLY);
write(wfd,“hello”,5);
printf("%s\n",“hello”);
close(wfd);
return 0;
}
*/
//可变长结构体
//struct Data
//{
// int data;
// char buf[1];
//};
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include “test.h” //可理解为进程间的协议
#define FIFO_NAME “/tmp/myfifo”
int main(int argc,char* argv[])
{
int ret=0; //接受fifo返回值
int len=0;
int rfd=0; //接受open 返回值 作为文件描素符
struct Data* px=NULL; //定义一个结构体指针
int count=0; //计算程度
if(access(FIFO_NAME,F_OK)!=0)//判断文件是否存在
{
ret=mkfifo(FIFO_NAME,0666); //创建有名管道
if(ret!=0) //出错处理
{
printf("create fifo failed");
return 1;
}
}
rfd=open(FIFO_NAME,O_RDONLY); //打开管道文件
while(1)
{
read(rfd,&len,sizeof(int));//读取信息
count=sizeof(int)+len;
px=(struct Data*)malloc(count); //申请空间
if(NULL==px)
{
printf("malloc failed");
break;
}
memset(px,0,count); //初始化
px->len=len;
read(rfd,px->buf,len);
printf("%s from frist process %d\n",px->buf,px->len);
if(strncmp(px->buf,"exit",4)==0) //退出循环条件
{
free(px);
break;
}
free(px);//释放空间
px=NULL;
}
close(rfd);
return 0;
}
/*
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FIFO_NAME “/tmp/myfifo”
int main(int argc,char* argv[])
{
int ret=0;
int wfd=0;
if(access(FIFO_NAME,F_OK)!=0)
{
ret=mkfifo(FIFO_NAME,0666);
if(ret!=0)
{
printf(“create fifo failed”);
return 1;
}
}
wfd=open(FIFO_NAME,O_WRONLY);
write(wfd,“hello”,5);
printf("%s\n",“hello”);
close(wfd);
return 0;
}
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include “test.h”
#define FIFO_NAME “/tmp/myfifo”
int main(int argc,char* argv[])
{
int ret=0;
int wfd=0;
char buf[64]="";
int len=0;
struct Data *px=NULL;//变长结构体指针
if(access(FIFO_NAME,F_OK)!=0)
{
ret=mkfifo(FIFO_NAME,0666);//创建广告
if(ret!=0)
{
printf("create fifo failed");
return 1;
}
}
wfd=open(FIFO_NAME,O_WRONLY);//文件描素符
while(1)
{
printf("Please input string:\n");
fgets(buf,64,stdin); //输入字符
len=strlen(buf); //计算长度
px=(struct Data*)malloc(sizeof(struct Data));//申请空间
if(px==NULL)
{
printf("Malloc Failed");
continue;
}
memset(px,0,sizeof(int)+len);//初始化
px->len=len; //初始化
strcpy(px->buf,buf);
write(wfd,px,sizeof(int)+len);//写入管道
if(strncmp(px->buf,"exit",4)==0) //条件语句
{
free(px);
break;
}
free(px);
px=NULL;
}
close(wfd);
}
//创建线程 子线程结束主线程工作
#include <unistd.h>
#include <pthread.h> //线程
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void* enter(void pv);//线程入口
int main(int argc,char argv[])
{
pthread_t tid; //线程号
int ret=0;
int i=0;
int arr[5]={1,2,3,4,5};//入口函数参数
void *pexit=NULL;//接受线程返回值
ret=pthread_create(&tid,NULL,enter,(void*)arr);//创建线程
if(ret!=0)
{
printf("pthread_create failed");
return 0;
}
//pthread_detach(tid); 会造成内存泄漏
ret=pthread_join(tid,&pexit);//以阻塞的形式等待线程结束
//参数 线程id 成功返回0 pexit接受返回值
printf("ret value is %d\n",ret);
if(ret==0)
{
printf("exit value is %d\n",(int)pexit);
}
for(i=0;i<5;i++)
{
printf("hello world\n");
sleep(1);
}
return 0;
}
void enter(void pv)
{
int* parr=(int*)pv;//入口函数参数
int i=0;
for(i=0;i<5;i++)
{
printf("%d\n",(parr+i));
sleep(1);
}
return (void)6;//pthread_exit(9); void pthread_eixt(void value_ptr)函数的返回代码
}
*/
//这段程序有点问题
#include <semaphore.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void *printfThread(void *pv);
void *printfThread1(void pv);//线程入口函数
struct StringBuf //线程函数的参数 信号量
{
char buf[64];
sem_t sem;
};
int main(int argc,char argv[])
{
pthread_t tid; //定义一个线程号
pthread_t tid1;
int ret=0;
struct StringBuf st={""};//初始化结构体
sem_init(&st.sem,0,0); //初始化信号量
ret=pthread_create(&tid,NULL,printfThread,&st);//创建线程
if(ret!=0)
{
printf("pthread create failed");
sem_destroy(&st.sem);//清理线程
return 1;
}
ret=pthread_create(&tid1,NULL,printfThread1,&st);//创建线程
if(ret!=0)
{
printf("pthread create failed");
sem_destroy(&st.sem);//清理线程
return 1;
}
while(1)
{
fgets(st.buf,64,stdin); //输入字符
sem_post(&st.sem); //给信号量加1
if(strncmp(st.buf,"exit",4)==0)
{
break;
}
}
pthread_join(tid,NULL);//
pthread_join(tid1,NULL);
sem_destroy(&st.sem);//销毁一个匿名信号
return 0;
}
void* printfThread(void* pv)
{
struct StringBuf* pst=(struct StringBuf*)pv;//定义一个结够体指针接受参数
while(1)
{
sem_wait(&pst->sem);//使信号量减1
if(strncmp(pst->buf,"exit",4)==0)
{
break;
}
printf("In Thread buf is %s",pst->buf);
memset(pst,0,64);//清零
}
return (void*)0;
}
void* printfThread1(void* pv)
{
struct StringBuf* pst=(struct StringBuf*)pv;//定义一个结够体指针接受参数
while(1)
{
sem_wait(&pst->sem);//使信号量减1
if(strncmp(pst->buf,"exit",4)==0)
{
break;
}
printf("In Thread1111111111 buf is %s",pst->buf);
memset(pst,0,64);//清零
}
return (void*)0;
}