C语言进程:两个对立的程序A和B,如何实现A退出B也退出,B退出A也退出
pthread_tpid1,pid2;intfd1,fd2;void*printf1(){fd1=open("pipe",O_RDONLY);while(1){charb...
pthread_t pid1,pid2;
int fd1,fd2;
void * printf1()
{
fd1=open( "pipe",O_RDONLY);
while(1)
{
char buff1[N]={0} ;
read(fd1,buff1,sizeof(buff1));
printf("%s\n",buff1);
fflush(stdout);
}
}
void * printf2()
{
fd2=open( "pipe1",O_WRONLY);
while(1)
{
char buff2[N]={0};
gets(buff2);
write (fd2,buff2,strlen(buff2));
}
}
int main(int argc, char const *argv[])
{
if( 0 != pthread_create(&pid1,NULL,printf1,NULL) )
{
perror("error1");
return -1;
}
if( 0 != pthread_create(&pid2,NULL,printf2,NULL) )
{
perror("error1");
return -1;
}
sleep(10000000);
return 0;
}
pthread_t pid1,pid2;
int fd1,fd2;
void * printf1()
{
fd2=open( "pipe1",O_RDONLY);
while(1)
{
char buff2[N]={0};
read(fd2,buff2,sizeof(buff2));
printf("%s\n",buff2 );
fflush(stdout);
}
}
void * printf2()
{
fd1=open( "pipe",O_WRONLY);
while(1)
{
char buff1[N]={0};
gets(buff1);
write (fd1,buff1,strlen(buff1));
}
}
int main(int argc, char const *argv[])
{
if( 0 != pthread_create(&pid1,NULL,printf1,NULL) )
{
perror("error1");
return -1;
}
if( 0 != pthread_create(&pid2,NULL,printf2,NULL) )
{
perror("error1");
return -1;
}
sleep(1000000);
return 0;
} 展开
int fd1,fd2;
void * printf1()
{
fd1=open( "pipe",O_RDONLY);
while(1)
{
char buff1[N]={0} ;
read(fd1,buff1,sizeof(buff1));
printf("%s\n",buff1);
fflush(stdout);
}
}
void * printf2()
{
fd2=open( "pipe1",O_WRONLY);
while(1)
{
char buff2[N]={0};
gets(buff2);
write (fd2,buff2,strlen(buff2));
}
}
int main(int argc, char const *argv[])
{
if( 0 != pthread_create(&pid1,NULL,printf1,NULL) )
{
perror("error1");
return -1;
}
if( 0 != pthread_create(&pid2,NULL,printf2,NULL) )
{
perror("error1");
return -1;
}
sleep(10000000);
return 0;
}
pthread_t pid1,pid2;
int fd1,fd2;
void * printf1()
{
fd2=open( "pipe1",O_RDONLY);
while(1)
{
char buff2[N]={0};
read(fd2,buff2,sizeof(buff2));
printf("%s\n",buff2 );
fflush(stdout);
}
}
void * printf2()
{
fd1=open( "pipe",O_WRONLY);
while(1)
{
char buff1[N]={0};
gets(buff1);
write (fd1,buff1,strlen(buff1));
}
}
int main(int argc, char const *argv[])
{
if( 0 != pthread_create(&pid1,NULL,printf1,NULL) )
{
perror("error1");
return -1;
}
if( 0 != pthread_create(&pid2,NULL,printf2,NULL) )
{
perror("error1");
return -1;
}
sleep(1000000);
return 0;
} 展开
2个回答
展开全部
//代码没有具体测试,只是添加了几句而已,如果有问题请留言
pthread_t pid1,pid2;
int fd1,fd2;
void * printf1()
{
fd2=open( "pipe1",O_RDONLY);
while(1)
{
char buff2[N]={0};
read(fd2,buff2,sizeof(buff2));
printf("%s\n",buff2 );
fflush(stdout);
}
}
void * printf2()
{
fd1=open( "pipe",O_WRONLY);
while(1)
{
char buff1[N]={0};
gets(buff1);
write (fd1,buff1,strlen(buff1));
}
}
int main(int argc, char const *argv[])
{
void *status1;
void *status2;
int ret;
if( 0 != pthread_create(&pid1,NULL,printf1,NULL) )
{
perror("error1");
return -1;
}
if( 0 != pthread_create(&pid2,NULL,printf2,NULL) )
{
perror("error1");
return -1;
}
ret = pthread_join(pid1,&status1);
if(ret == 0)
{
pthread_kill(pid2,SIGKILL);
}
ret = pthread_join(pid2,&status1);
if(ret == 0)
{
pthread_kill(pid1,SIGKILL);
}
//sleep(1000000);
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询