AB进程做通信
要求:1、A进程发送一句话,B进程接收打印
2. 然后B进程发送给A进程一句话,A进程接收打印
3. 重复1,2步骤,直到A进程或者B进程收到quit,退出AB进程
A进程:
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<errno.h>
#include<unistd.h>
#include<string.h>
int main(int argc, const char *argv[])
{
if(mkfifo("./myfifo1",0664)<0)
{
if(errno!=17)
{
perror("myfifo");
return -1;
}
}
printf("create1 success\n");
if(mkfifo("./myfifo2",0664)<0)
{
if(errno!=17)
{
perror("myfifo");
return -1;
}
}
printf("create2 success\n");
char arr[128]="";
int fq = open("./myfifo1",O_WRONLY);
int fp = open("./myfifo2",O_RDONLY);
while(1)
{
scanf("%s",arr);
write(fq,arr,sizeof(arr)-1);
if(strcasecmp(arr,"quit")==0)
break;
char brr[128];
bzero(brr,sizeof(brr));
int res=0;
if((res=read(fp,brr,sizeof(brr)-1))==0)
break;
printf("B:%s\n",brr);
if(strcasecmp(brr,"quit")==0)
break;
}
close(fq);
close(fp);
return 0;
}
B进程:
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<errno.h>
#include<unistd.h>
#include<string.h>
int main(int argc, const char *argv[])
{
if(mkfifo("./myfifo1",0664)<0)
{
if(errno!=17)
{
perror("myfifo");
return -1;
}
}
printf("create1 success\n");
if(mkfifo("./myfifo2",0664)<0)
{
if(errno!=17)
{
perror("myfifo");
return -1;
}
}
printf("create2 success\n");
int fp = open("./myfifo1",O_RDONLY);
int fq = open("./myfifo2",O_WRONLY);
char arr[128]="";
char brr[128]="";
while(1)
{
bzero(arr,sizeof(arr));
int res;
if((res=read(fp,arr,sizeof(arr)-1))==0)
break;
printf("A:%s\n",arr);
if(strcasecmp(arr,"quit")==0)
break;
scanf("%s",brr);
write(fq,brr,sizeof(brr)-1);
if(strcasecmp(brr,"quit")==0)
break;
}
close(fp);
close(fq);
return 0;
}
演示结果:
AB进程实时通信
A进程:
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<errno.h>
#include<unistd.h>
#include<string.h>
#include<pthread.h>
#include<semaphore.h>
sem_t set1;
sem_t set2;
void *a_put(void*arg)
{
char arr[128]="";
while(1)
{
// sem_wait(&set1);
scanf("%s",arr);
write(*((int*)arg),arr,sizeof(arr)-1);
if(strcasecmp(arr,"quit")==0)
break;
// sem_post(&set2);
sleep(1);
}
close(*(int*)arg);
}
void *a_get(void*arg)
{
char brr[128];
bzero(brr,sizeof(brr));
int res=0;
while(1)
{
// sem_wait(&set2);
if((res=read(*(int*)arg,brr,sizeof(brr)-1))==0)
break;
printf("B:%s\n",brr);
if(strcasecmp(brr,"quit")==0)
break;
// sem_post(&set1);
sleep(1);
}
close(*(int*)arg);
}
int main(int argc, const char *argv[])
{
int fq = open("./myfifo1",O_WRONLY);
int fp = open("./myfifo2",O_RDONLY);
if(mkfifo("./myfifo1",0664)<0)
{
if(errno!=17)
{
perror("myfifo");
return -1;
}
}
printf("create1 success\n");
if(mkfifo("./myfifo2",0664)<0)
{
if(errno!=17)
{
perror("myfifo");
return -1;
}
}
printf("create2 success\n");
pthread_t pp1,pp2;
if(sem_init(&set1,0,1)<0)
{
perror("sem_init");
return -1;
}
if(sem_init(&set2,0,0)<0)
{
perror("sem_init");
return -1;
}
if(pthread_create(&pp1,NULL,a_put,(void*)&fq)<0)
{
perror("pthread_create");
return -1;
}
if(pthread_create(&pp2,NULL,a_get,(void*)&fp)<0)
{
perror("pthread_create");
return -1;
}
pthread_join(pp1,NULL);
pthread_join(pp2,NULL);
return 0;
}
B进程:
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<errno.h>
#include<unistd.h>
#include<string.h>
#include<pthread.h>
#include<semaphore.h>
sem_t set1;
sem_t set2;
void *b_put(void*arg)
{
char arr[128]="";
while(1)
{
// sem_wait(&set2);
scanf("%s",arr);
write(*((int*)arg),arr,sizeof(arr)-1);
// sem_post(&set1);
if(strcasecmp(arr,"quit")==0)
break;
sleep(1);
}
close(*(int*)arg);
}
void *b_get(void*arg)
{
char brr[128];
int res=0;
while(1)
{
// sem_wait(&set1);
bzero(brr,sizeof(brr));
if((res=read(*(int*)arg,brr,sizeof(brr)-1))==0)
break;
printf("A:%s\n",brr);
if(strcasecmp(brr,"quit")==0)
break;
// sem_post(&set2);
sleep(1);
}
close(*(int*)arg);
}
int main(int argc, const char *argv[])
{
int fq = open("./myfifo1",O_RDONLY);
int fp = open("./myfifo2",O_WRONLY);
if(mkfifo("./myfifo1",0664)<0)
{
if(errno!=17)
{
perror("myfifo");
return -1;
}
}
printf("create1 success\n");
if(mkfifo("./myfifo2",0664)<0)
{
if(errno!=17)
{
perror("myfifo");
return -1;
}
}
printf("create2 success\n");
pthread_t pp1,pp2;
if(sem_init(&set1,0,1)<0)
{
perror("sem_init");
return -1;
}
if(sem_init(&set2,0,0)<0)
{
perror("sem_init");
return -1;
}
if(pthread_create(&pp1,NULL,b_put,(void*)&fp)<0)
{
perror("pthread_create");
return -1;
}
if(pthread_create(&pp2,NULL,b_get,(void*)&fq)<0)
{
perror("pthread_create");
return -1;
}
pthread_join(pp1,NULL);
pthread_join(pp2,NULL);
return 0;
}
捕获2)3)20)号信号
#include<stdio.h>
#include<signal.h>
#include<unistd.h>
typedef void (*sighandler_t)(int);
void handler_2(int sig)
{
printf("handler_2 %d\n",sig);
}
void handler_3(int sig)
{
printf("handler_3 %d\n",sig);
}
void handler_20(int sig)
{
printf("handler_20 %d\n",sig);
}
int main(int argc, const char *argv[])
{
sighandler_t s = signal(2,handler_2);
if(SIG_ERR == s)
{
perror("signal");
return -1;
}
s = signal(3,handler_3);
if(SIG_ERR == s)
{
perror("signal");
return -1;
}
s = signal(3,handler_20);
if(SIG_ERR == s)
{
perror("signal");
return -1;
}
while(1)
{
printf("this is main\n");
sleep(1);
}
return 0;
}