作业1
进程B、A
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
int main(int argc, const char *argv[])
{
if(-1==mkfifo("./myfifo1",0664))
{
if(17!=errno)
{
perror("mkfifo");
return -1;
}
}
printf("mkfifo1 create success!\n");
if(-1==mkfifo("./myfifo2",0664))
{
if(17!=errno)
{
perror("mkfifo");
return -1;
}
}
printf("mkfifo2 create success!\n");
int res=0;
char buf[100]="";
char str[100]="";
while(1)
{
// printf("sizeof=%ld\n",sizeof(buf));
int fd=open("./myfifo1",O_WRONLY);
if(-1==fd)
{
perror("open");
return -1;
}
// printf("open success!\n");
printf("请输入字符串:");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
if(0==strcmp(buf,"quit"))
break;
if(-1==write(fd,buf,sizeof(buf)))
{
perror("write");
return -1;
}
/**********分割线**************/
//接收字符串,读取有名管道
int fd1=open("./myfifo2",O_RDONLY);
if(-1==fd1)
{
perror("open");
return -1;
}
printf("open success!\n");
res=read(fd1,str,sizeof(str));
if(0==strcmp(str,"quit"))
{
break;
}
if(-1==res)
{
perror("read");
return -1;
}
else if(0==res)
{
printf("myfifo2 has been read over!\n");
break;
}
printf("str=%s\n",str);
// printf("write myfifo success!\n");
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
int main(int argc, const char *argv[])
{
if(-1==mkfifo("./myfifo1",0664))
{
if(17!=errno)
{
perror("mkfifo");
return -1;
}
}
printf("mkfifo1 create success!\n");
if(-1==mkfifo("./myfifo2",0664))
{
if(17!=errno)
{
perror("mkfifo");
return -1;
}
}
printf("mkfifo2 create success!\n");
char buf[100]="";
char str[100]="";
ssize_t res=0;
while(1)
{
// bzero(buf,sizeof(buf));
int fd=open("./myfifo1",O_RDONLY);
if(-1==fd)
{
perror("open");
return -1;
}
res=read(fd,buf,sizeof(buf));
if(0==strcmp(buf,"quit"))
{
break;
}
if(-1==res)
{
perror("read");
return -1;
}
else if(0==res)
{
printf("myfifo1 has been read over!\n");
break;
}
printf("buf=%s\n",buf);
/***********分割线*************/
//输入字符串,写入有名管道
int fd1=open("./myfifo2",O_WRONLY);
if(-1==fd1)
{
perror("open");
return -1;
}
printf("请输入字符串:");
fgets(str,sizeof(str),stdin);
str[strlen(str)-1]=0;
if(0==strcmp(str,"quit"))
{
break;
}
if(-1==write(fd1,str,sizeof(str)))
{
perror("write");
return -1;
}
// printf("res=%ld buf=%s",res,buf);
}
return 0;
}
作业2:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
typedef void(*sighandler_t)(int);
void handler(int sig)
{
printf("出发sig=%d信号\n",sig);
}
void handler2(int sig)
{
printf("line=%d\n",__LINE__);
}
void handler3(int sig)
{
printf("这是新的处理函数!!!\n");
}
int main(int argc, const char *argv[])
{
//捕获2)SIGINT,注册新的处理函数
/* __sighandler_t s1=signal(2,handler);
if(SIG_ERR==s1)
{
perror("signal");
return -1;
}
*/
__sighandler_t s2=signal(3,handler2);
if(SIG_ERR==s2)
{
perror("signal");
return -1;
}
sighandler_t s3=signal(20,handler3);
if(SIG_ERR==s3)
{
perror("signal");
return -1;
}
while(1)
{
printf("signal......\n");
sleep(1);
}
return 0;
}