当前位置: 技术问答>linux和unix
高手帮下忙 这事怎么回事啊 在线等
来源: 互联网 发布时间:2016-04-03
本文导语: #include #include #include #include #include #include #include #include #include #include #include #include #define BUFFER_SIZE 1000 #define SER_PORT 3999 struct send_data_buffer { char buffer[BUFFER_SIZE]; pthread_mutex_t lock; pthread_cond_t send_data_able; pthread_con...
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define BUFFER_SIZE 1000
#define SER_PORT 3999
struct send_data_buffer
{
char buffer[BUFFER_SIZE];
pthread_mutex_t lock;
pthread_cond_t send_data_able;
pthread_cond_t write_data_able;
int buffer_full;
};
struct receive_data_buffer
{
char buffer[BUFFER_SIZE];
pthread_mutex_t lock;
pthread_cond_t receive_data_able;
pthread_cond_t read_data_able;
int buffer_empty;
};
char ser_IP[]={"192.168.0.115"};
struct send_data_buffer *send_buffer;
struct receive_data_buffer *receive_buffer;
void send_buffer_init(struct send_data_buffer *b)
{
pthread_mutex_init(&b->lock,NULL);
pthread_cond_init(&b->send_data_able,NULL);
pthread_cond_init(&b->write_data_able,NULL);
b->buffer_full=0;
}
void receive_buffer_init(struct receive_data_buffer *b)
{
pthread_mutex_init(&b->lock,NULL);
pthread_cond_init(&b->receive_data_able,NULL);
pthread_cond_init(&b->read_data_able,NULL);
b->buffer_empty=1;
}
void *transmission(void *data)
{
pid_t child3,child4;
int sockfd, numbytes,status;
char buf[BUFFER_SIZE];
struct hostent *he;
struct sockaddr_in ser_addr;
if((he=gethostbyname(ser_IP))==NULL)
{
herror("gethostbyname error");
exit(1);
}
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket create error");
//return;
}
ser_addr.sin_family = AF_INET;
ser_addr.sin_port = htons(SER_PORT);
ser_addr.sin_addr = *((struct in_addr *)he->h_addr);
bzero(&(ser_addr.sin_zero),8);
printf("Start connectionn");
if (connect(sockfd, (struct sockaddr *)&ser_addr, sizeof(struct sockaddr)) == -1)
{
perror("connect error");
//return;
}
printf("connection has succedn");
if(!(child3=fork()))
{
printf("process 1 in transmission starts!n");
int len;
while(1)
{
printf("client send start");
pthread_mutex_lock(&send_buffer->lock);
while(send_buffer->buffer_full==0)
{
pthread_cond_wait(&send_buffer->send_data_able,&send_buffer->lock);
}
printf("client send");
if (send(sockfd,send_buffer->buffer,strlen(send_buffer->buffer),0) == -1)
{
perror("send error");
//return;
}
printf("client sending data has succedn ");
send_buffer->buffer_full=0;
printf("the buffer_full is %d after sending data!n",send_buffer->buffer_full);
pthread_cond_signal(&send_buffer->write_data_able);
pthread_mutex_unlock(&send_buffer->lock);
}
//exit(0);
}
printf("transmission is running!n");
if(!(child4=fork()))
{
printf("process 2 in transmission starts");
char buf[BUFFER_SIZE];
while(1)
{
if (( numbytes = recv(sockfd, buf, BUFFER_SIZE, 0)) == -1||numbytes==0)
{
perror("recv error");
}
buf[numbytes] = '';
pthread_mutex_lock(&receive_buffer->lock);
while(receive_buffer->buffer_empty==0)
{
pthread_cond_wait(&receive_buffer->receive_data_able,&receive_buffer->lock);
}
printf("client receive");
strcpy(receive_buffer->buffer,buf);
receive_buffer->buffer_empty=0;
printf("the buffer_empty is %d after receiving data!n ",receive_buffer->buffer_empty);
pthread_cond_signal(&receive_buffer->read_data_able);
pthread_mutex_unlock(&receive_buffer->lock);
}
}
printf("transmission is still running!n");
waitpid(child3,&status,0);
waitpid(child4,&status,0);
printf("all over!^_^n");
close(sockfd);
// return;
}
void *infor_format(void *data)
{
pid_t child1,child2;
int status;
if(!(child1=fork()))
{
printf("process 1 in infor_format starts!n");
char buf[BUFFER_SIZE];
while(1)
{
printf("输入要发送的数据:n");
gets(buf);
//buf=strcat("client send:",buf);
//buf=strcat(buf,'n');
pthread_mutex_lock(&send_buffer->lock);
while(send_buffer->buffer_full==1)
{
pthread_cond_wait(&send_buffer->write_data_able,&send_buffer->lock);
}
strcpy(send_buffer->buffer,buf);
send_buffer->buffer_full=1;
printf("the buffer_full is %d after writing data into send_buffer!n",send_buffer->buffer_full);
printf("buffer ");
pthread_cond_signal(&send_buffer->send_data_able);
pthread_mutex_unlock(&send_buffer->lock);
}
}
if(!(child2=fork()))
{
printf("process 2 in infor_mat starts!n");
char buf[BUFFER_SIZE];
while(1)
{
printf("start read buffer");
pthread_mutex_lock(&receive_buffer->lock);
while(receive_buffer->buffer_empty==1)
{
pthread_cond_wait(&receive_buffer->read_data_able,&receive_buffer->lock);
}
strcpy(buf,receive_buffer->buffer);
// buf=strcat("client receive:",buf);
// buf=strcat(buf,'n');
puts(buf);
receive_buffer->buffer_empty=1;
printf("the buffer_empty is %d after reading data from receive_buffer!n ",receive_buffer->buffer_empty);
pthread_cond_signal(&receive_buffer->read_data_able);
pthread_mutex_unlock(&receive_buffer->lock);
}
}
waitpid(child1,&status,0);
waitpid(child2,&status,0);
}
int main(void)
{
pthread_t th_a,th_b;
void *retval;
send_buffer=(struct send_data_buffer *)malloc(sizeof(struct send_data_buffer));
receive_buffer=(struct receive_data_buffer *)malloc(sizeof(struct receive_data_buffer));
send_buffer_init(send_buffer);
receive_buffer_init(receive_buffer);
pthread_create(&th_a,NULL,transmission,0);
pthread_create(&th_b,NULL,infor_format,0);
pthread_join(th_a,&retval);
pthread_join(th_b,&retval);
return 0;
}
以上是我写的SOCKET发送的客户端
有两个共享区 一个是发送共享区 一个是接收共享区
线程transmission 用来和客户端信息的交互
线程infor_format 用来向共享区读取或写数据
在线程transmission中又建了两个进程 分别用来发送和接受
在线程infor_format中也建了两个进程 一个是写发送共享区 一个是读接收共享区
程序编译没问题
就是运行时,运行结果为:
Start connection
connection has succed
process 1 in infor_format starts!
输入要发送的数据:
transmission is running!
transmission is still running!
process 2 in infor_mat starts!
socket test //这是输入的数据
the buffer_full is 1 after writing data into send_buffer!
buffer 输入要发送的数据:
我向下 ,为什么线程transmission中的两个进程 没能运行起来呢
本来也能输出
process 1 in transmission starts!
但不能输出
printf("client send start");
我捣鼓了一下 连上面的那句都输出不了了
高手帮个忙 解释下!在线等
谢谢了 !!!!!
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define BUFFER_SIZE 1000
#define SER_PORT 3999
struct send_data_buffer
{
char buffer[BUFFER_SIZE];
pthread_mutex_t lock;
pthread_cond_t send_data_able;
pthread_cond_t write_data_able;
int buffer_full;
};
struct receive_data_buffer
{
char buffer[BUFFER_SIZE];
pthread_mutex_t lock;
pthread_cond_t receive_data_able;
pthread_cond_t read_data_able;
int buffer_empty;
};
char ser_IP[]={"192.168.0.115"};
struct send_data_buffer *send_buffer;
struct receive_data_buffer *receive_buffer;
void send_buffer_init(struct send_data_buffer *b)
{
pthread_mutex_init(&b->lock,NULL);
pthread_cond_init(&b->send_data_able,NULL);
pthread_cond_init(&b->write_data_able,NULL);
b->buffer_full=0;
}
void receive_buffer_init(struct receive_data_buffer *b)
{
pthread_mutex_init(&b->lock,NULL);
pthread_cond_init(&b->receive_data_able,NULL);
pthread_cond_init(&b->read_data_able,NULL);
b->buffer_empty=1;
}
void *transmission(void *data)
{
pid_t child3,child4;
int sockfd, numbytes,status;
char buf[BUFFER_SIZE];
struct hostent *he;
struct sockaddr_in ser_addr;
if((he=gethostbyname(ser_IP))==NULL)
{
herror("gethostbyname error");
exit(1);
}
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket create error");
//return;
}
ser_addr.sin_family = AF_INET;
ser_addr.sin_port = htons(SER_PORT);
ser_addr.sin_addr = *((struct in_addr *)he->h_addr);
bzero(&(ser_addr.sin_zero),8);
printf("Start connectionn");
if (connect(sockfd, (struct sockaddr *)&ser_addr, sizeof(struct sockaddr)) == -1)
{
perror("connect error");
//return;
}
printf("connection has succedn");
if(!(child3=fork()))
{
printf("process 1 in transmission starts!n");
int len;
while(1)
{
printf("client send start");
pthread_mutex_lock(&send_buffer->lock);
while(send_buffer->buffer_full==0)
{
pthread_cond_wait(&send_buffer->send_data_able,&send_buffer->lock);
}
printf("client send");
if (send(sockfd,send_buffer->buffer,strlen(send_buffer->buffer),0) == -1)
{
perror("send error");
//return;
}
printf("client sending data has succedn ");
send_buffer->buffer_full=0;
printf("the buffer_full is %d after sending data!n",send_buffer->buffer_full);
pthread_cond_signal(&send_buffer->write_data_able);
pthread_mutex_unlock(&send_buffer->lock);
}
//exit(0);
}
printf("transmission is running!n");
if(!(child4=fork()))
{
printf("process 2 in transmission starts");
char buf[BUFFER_SIZE];
while(1)
{
if (( numbytes = recv(sockfd, buf, BUFFER_SIZE, 0)) == -1||numbytes==0)
{
perror("recv error");
}
buf[numbytes] = '';
pthread_mutex_lock(&receive_buffer->lock);
while(receive_buffer->buffer_empty==0)
{
pthread_cond_wait(&receive_buffer->receive_data_able,&receive_buffer->lock);
}
printf("client receive");
strcpy(receive_buffer->buffer,buf);
receive_buffer->buffer_empty=0;
printf("the buffer_empty is %d after receiving data!n ",receive_buffer->buffer_empty);
pthread_cond_signal(&receive_buffer->read_data_able);
pthread_mutex_unlock(&receive_buffer->lock);
}
}
printf("transmission is still running!n");
waitpid(child3,&status,0);
waitpid(child4,&status,0);
printf("all over!^_^n");
close(sockfd);
// return;
}
void *infor_format(void *data)
{
pid_t child1,child2;
int status;
if(!(child1=fork()))
{
printf("process 1 in infor_format starts!n");
char buf[BUFFER_SIZE];
while(1)
{
printf("输入要发送的数据:n");
gets(buf);
//buf=strcat("client send:",buf);
//buf=strcat(buf,'n');
pthread_mutex_lock(&send_buffer->lock);
while(send_buffer->buffer_full==1)
{
pthread_cond_wait(&send_buffer->write_data_able,&send_buffer->lock);
}
strcpy(send_buffer->buffer,buf);
send_buffer->buffer_full=1;
printf("the buffer_full is %d after writing data into send_buffer!n",send_buffer->buffer_full);
printf("buffer ");
pthread_cond_signal(&send_buffer->send_data_able);
pthread_mutex_unlock(&send_buffer->lock);
}
}
if(!(child2=fork()))
{
printf("process 2 in infor_mat starts!n");
char buf[BUFFER_SIZE];
while(1)
{
printf("start read buffer");
pthread_mutex_lock(&receive_buffer->lock);
while(receive_buffer->buffer_empty==1)
{
pthread_cond_wait(&receive_buffer->read_data_able,&receive_buffer->lock);
}
strcpy(buf,receive_buffer->buffer);
// buf=strcat("client receive:",buf);
// buf=strcat(buf,'n');
puts(buf);
receive_buffer->buffer_empty=1;
printf("the buffer_empty is %d after reading data from receive_buffer!n ",receive_buffer->buffer_empty);
pthread_cond_signal(&receive_buffer->read_data_able);
pthread_mutex_unlock(&receive_buffer->lock);
}
}
waitpid(child1,&status,0);
waitpid(child2,&status,0);
}
int main(void)
{
pthread_t th_a,th_b;
void *retval;
send_buffer=(struct send_data_buffer *)malloc(sizeof(struct send_data_buffer));
receive_buffer=(struct receive_data_buffer *)malloc(sizeof(struct receive_data_buffer));
send_buffer_init(send_buffer);
receive_buffer_init(receive_buffer);
pthread_create(&th_a,NULL,transmission,0);
pthread_create(&th_b,NULL,infor_format,0);
pthread_join(th_a,&retval);
pthread_join(th_b,&retval);
return 0;
}
以上是我写的SOCKET发送的客户端
有两个共享区 一个是发送共享区 一个是接收共享区
线程transmission 用来和客户端信息的交互
线程infor_format 用来向共享区读取或写数据
在线程transmission中又建了两个进程 分别用来发送和接受
在线程infor_format中也建了两个进程 一个是写发送共享区 一个是读接收共享区
程序编译没问题
就是运行时,运行结果为:
Start connection
connection has succed
process 1 in infor_format starts!
输入要发送的数据:
transmission is running!
transmission is still running!
process 2 in infor_mat starts!
socket test //这是输入的数据
the buffer_full is 1 after writing data into send_buffer!
buffer 输入要发送的数据:
我向下 ,为什么线程transmission中的两个进程 没能运行起来呢
本来也能输出
process 1 in transmission starts!
但不能输出
printf("client send start");
我捣鼓了一下 连上面的那句都输出不了了
高手帮个忙 解释下!在线等
谢谢了 !!!!!
|
子进程是对父进程数据的copy 子进程对共享区的访问并不是父进程的共享区内存 而是其COPY的数据共享区