1、捕获control+c信号,进行处理posix信号量
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fcntl.h>
#include<sys/sem.h>
#include<sys/shm.h>
#include<sys/ipc.h>
#include<sys/types.h>
#include<signal.h>
#include<semaphore.h>
#include<sys/stat.h>
sem_t *space=NULL;
sem_t *data=NULL;
void func(int num)
{
//关闭posix信号量
if(-1 == sem_close(space))
{
perror("close space failed:");
exit(-1);
}
int cd = sem_close(data);
if(-1 == cd)
{
perror("close data failed");
exit(-1);
}
//删除posix信号量
int us = sem_unlink("/space");
if(-1 == us)
{
perror("unlink data failed");
exit(-1);
}
int ud = sem_unlink("/data");
if(-1 == ud)
{
perror("unlink data failed");
exit(-1);
}
//退出程序
exit(0);
}
int main()
{
signal(SIGINT, func);
//创建一个key值
key_t key = ftok("./",0);
//创建一个共享空间
int shm_id = shmget(key,1024, IPC_CREAT | 0666);
if(-1 == shm_id)
{
perror("create share memory failed:");
return -1;
}
//映射共享空间
char *p = shmat(shm_id, NULL, 0);
if(NULL == p)
{
perror("map share memory failed.");
return -1;
}
//创建信号量
space = sem_open("/space",O_CREAT, 0777, 1);
data = sem_open("/data",O_CREAT, 0777, 0);
//写入共享内存
while(1)
{
char *msg = "1234567890";
for(int i=0; i<10; i++)
{
sem_wait(space);
memcpy(p, msg+i, 1);
sem_post(data);
}
}
}
2、通过线程与有名管道进行两个进程间的同时读写
ject的进程
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<pthread.h>
#include<fcntl.h>
#include<unistd.h>
void *j_to_r(void *);
void *r_to_j(void *);
int main()
{
//创建线程
pthread_t tid_w;
pthread_t tid_r;
int pthread_ret = pthread_create(&tid_r, NULL, j_to_r, NULL);
if(pthread_ret)
{
perror("create write thread failed:");
return -1;
}
pthread_ret = pthread_create(&tid_w, NULL, r_to_j, NULL);
if(pthread_ret)
{
perror("create read thread failed:");
return -1;
}
int w_ret = pthread_join(tid_w,NULL);
int r_ret = pthread_join(tid_r,NULL);
return 0;
}
void *r_to_j(void *argv)
{
//创建管道
int fifo_w = mkfifo("./r_to_j_fifo", 0777);
//打开管道文件
int fifo_fd = open("./r_to_j_fifo", O_WRONLY);
//写入数据
char *buf = malloc(101);
while(1)
{
memset(buf, 0, 101);
fgets(buf,100,stdin);
write(fifo_fd, buf, strlen(buf));
if(strcmp(buf,"bey\n") == 0)
break;
}
return NULL;
}
void *j_to_r(void *argv)
{
//创建管道
int fifo_r = mkfifo("./j_to_r_fifo", 0777);
//打开管道文件
int fifo_fd = open("./j_to_r_fifo", O_RDONLY);
//写入数据
char *buf = malloc(101);
while(1)
{
memset(buf, 0, 101);
int n = read(fifo_fd, buf, 100);
printf("%s",buf);
if(strcmp(buf,"bey\n") == 0)
break;
}
return NULL;
}
rose的进程
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<pthread.h>
#include<fcntl.h>
#include<unistd.h>
void *j_to_r(void *);
void *r_to_j(void *);
int main()
{
//创建线程
pthread_t tid_w;
pthread_t tid_r;
int pthread_ret = pthread_create(&tid_w, NULL, j_to_r, NULL);
if(pthread_ret)
{
perror("create write thread failed:");
return -1;
}
pthread_ret = pthread_create(&tid_r, NULL, r_to_j, NULL);
if(pthread_ret)
{
perror("create read thread failed:");
return -1;
}
int w_ret = pthread_join(tid_w,NULL);
int r_ret = pthread_join(tid_r,NULL);
return 0;
}
void *j_to_r(void *argv)
{
//创建管道
int fifo_w = mkfifo("./j_to_r_fifo", 0777);
//打开管道文件
int fifo_fd = open("./j_to_r_fifo", O_WRONLY);
//写入数据
char *buf = malloc(101);
while(1)
{
memset(buf, 0, 101);
fgets(buf,100,stdin);
write(fifo_fd, buf, strlen(buf));
if(strcmp(buf,"bey\n") == 0)
break;
}
return NULL;
}
void *r_to_j(void *argv)
{
//创建管道
int fifo_r = mkfifo("./r_to_j_fifo", 0777);
//打开管道文件
int fifo_fd = open("./r_to_j_fifo", O_RDONLY);
//写入数据
char *buf = malloc(101);
while(1)
{
memset(buf, 0, 101);
read(fifo_fd, buf, 100);
printf("%s",buf);
if(strcmp(buf,"bey\n") == 0)
break;
}
return NULL;
}