进程间通信、信号捕获
进程间通信
main.c
#include
#include
#include
#include
int main(int argc, const char *argv[])
{
//创建一个管道
int pfd1[2] = {0};
if(pipe(pfd1) < 0)
{
perror("pipe1");
return -1;
}
printf("pipe1 create successn ");
int pfd2[2] = {0};
if(pipe(pfd2) < 0)
{
perror("pipe2");
return -1;
}
printf("pipe2 create successn ");
pid_t pid = fork();
if(pid > 0)
{
//关闭pfd1管道的读端和pfd2管道的写端,因为父进程没用到
close(pfd1[0]);
close(pfd2[1]);
char str[128] = "";
ssize_t res;
while(1)
{
bzero(str, sizeof(str));
printf("parent input:");
fgets(str, sizeof(str), stdin);
str[strlen(str)-1] = ' ';
if(write(pfd1[1], str, sizeof(str)) < 0)
{
perror("write");
exit(0);
}
if(strcasecmp(str, "quit") == 0)
break;
bzero(str, sizeof(str));
res = read(pfd2[0], str, sizeof(str));
if(res < 0)
{
perror("read");
return -1;
}
else if(0 == res)
{
printf("写端关闭n");
break;
}
printf("父进程收到子进程说:%sn", str)
if(strcasecmp(str, "quit") == 0)
break;
}
}
//阻塞等待子进程退出
wait(NULL);
close(pfd1[1]);
close(pfd2[0]);
else if(0 == pid)
{
close(pfd1[1]);
cloose(pfd2[0]);
//子进程从管道中读取数据
char buf[128] = "";
ssize_t res = 0;
while(1)
{
bzero(buf, sizeof(buf));
res = read(pfd1[0], buf, sizeof(buf));
if(res < 0)
{
perror("read");
return -1;
}
else if(0 == res)
{
printf("写端关闭n");
break;
}
printf("子进程收到父进程说%sn", buf);
if(stricasecmp(buf, "quit") == 0)
break;
else
printf("child = %sn", buf);
bzero(buf, sizeof(buf));
printf("chile input:");
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf)-1] = 0;
if(write(pfd2[1], buf, sizeof(buf)) < 0)
{
perror("write");
return -1;
}
if(stricasecmp(buf, "quit") == 0)
break;
}
}
else
{
perror("fork");
return -1;
}
return 0;
}
结果展示
进程间实时通信
进程A main.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
char buf[128] = "";
void *callBack_read(void *arg)
{
ssize_t res;
//以只读的方式打开有名管道文件1
int fd_r = open("./myfifo1", O_RDONLY);
if(fd_r < 0)
{
perror("open");
return NULL;
}
printf("open readnoly successn");
while(1)
{
bzero(buf, sizeof(buf));
res = read(fd_r, buf, sizeof(buf));
if(res < 0)
{
perror("read");
return NULL;
}
else if(res == 0)
{
printf("对方进程退出n");
break;
}
printf("read success,%sn", buf);
if(strcasecmp(buf, "quit") == 0)
{
printf("已退出n");
exit(0);
}
}
pthread_exit(NULL);
close(fd_r);
}
void *callBack_write(void *arg)
{
//以只写的方式打开有名管道文件2
int fd_w = open("./myfifo2", O_WRONLY);
if(fd_w < 0)
{
perror("open");
return NULL;
}
printf("open writenoly successn");
while(1)
{
bzero(buf, sizeof(buf));
//printf("请输入>>>");
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf)-1] = ' ';
if(write(fd_w, buf, sizeof(buf)) < 0)
{
perror("write");
return NULL;
}
if(strcasecmp(buf, "quit") == 0)
{
printf("已退出n");
exit(0);
}
}
pthread_exit(NULL);
close(fd_w);
}
int main(int argc, const char *argv[])
{
//创建两个有名管道
umask(0);
if(mkfifo("./myfifo1", 0777) < 0);
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
if(mkfifo("./myfifo2", 0777) < 0);
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
printf("mkfifo successn");
//创建两个线程
pthread_t tid1, tid2;
pthread_create(&tid1, NULL, callBack_read, NULL);
pthread_create(&tid2, NULL, callBack_write, NULL);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
return 0;
}
进程B main.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
char buf[128] = "";
void *callBack_read(void *arg)
{
ssize_t res;
//以只读的方式打开有名管道文件2
int fd_r = open("./myfifo2", O_RDONLY);
if(fd_r < 0)
{
perror("open");
return NULL;
}
printf("open readnoly successn");
while(1)
{
bzero(buf, sizeof(buf));
res = read(fd_r, buf, sizeof(buf));
if(res < 0)
{
perror("read");
return NULL;
}
else if(res == 0)
{
printf("对方进程退出n");
break;
}
printf("read success,%sn", buf);
if(strcasecmp(buf, "quit") == 0)
{
printf("已退出n");
exit(0);
}
}
pthread_exit(NULL);
close(fd_r);
}
void *callBack_write(void *arg)
{
//以只写的方式打开有名管道文件1
int fd_w = open("./myfifo1", O_WRONLY);
if(fd_w < 0)
{
perror("open");
return NULL;
}
printf("open writenoly successn");
while(1)
{
bzero(buf, sizeof(buf));
// printf("请输入>>>");
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf)-1] = ' ';
if(write(fd_w, buf, sizeof(buf)) < 0)
{
perror("write");
return NULL;
}
if(strcasecmp(buf, "quit") == 0)
{
printf("已退出n");
exit(0);
}
}
pthread_exit(NULL);
close(fd_w);
}
int main(int argc, const char *argv[])
{
//创建两个线程两个个有名管道
umask(0);
if(mkfifo("./myfifo1", 0777) < 0);
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
if(mkfifo("./myfifo2", 0777) < 0);
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
printf("mkfifo successn");
//创建两个线程
pthread_t tid1, tid2;
pthread_create(&tid1, NULL, callBack_read, NULL);
pthread_create(&tid2, NULL, callBack_write, NULL);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
return 0;
}
结果展示
信号捕获
main.c
#include
#include
#include
typedef void (*sighandler_t)(int);
void handler(int sig)
{
printf("this is handler %dn", sig);
}
int main(int argc, const char *argv[])
{
sighandler_t s;
s = signal(2, handler);
if(SIG_ERR == s)
{
perror("signal");
return -1;
}
s = signal(3, handler);
if(SIG_ERR == s)
{
perror("signal");
return -1;
}
s = signal(20, handler);
if(SIG_ERR == s)
{
perror("signal");
return -1;
}
while(1)
{
printf("this is mainn");
sleep(1);
}
return 0;
}
结果展示