由于许多进程,pthread_create 的错误返回代码是 35 错误,我使用了 pthread_exit ,它应该杀死线程,不是吗?
我在 pthread_create 中创建了一个 pthread_create,我使用了套接字编程,在其中接收一个数据包,然后创建一个线程来写入文件。当我发送一个非常大的文件时,我收到此错误......?
代码如下...
void *writePack(void *sock)
{
size_t nbyte;
ssize_t writeSize;
nbyte = 1466;
off_t offset;
offset = (((struct writePacket *)sock)->seq * 1466);
char* buffer = new char();
buffer = ((struct writePacket *)sock)->datamsg;
writeSize = pwrite(((struct writePacket *)sock)->pp,(const void *)buffer, nbyte, offset );
free(buffer);
pthread_exit(NULL);
}
这是父接收代码的代码...
recvfrom(sockA->sockid, (void *)&recvdata, sizeof(struct data), 0, (struct sockaddr *) &cli_addr, &clilen);
if (n<0)
error("Error on reading");
pthread_mutex_lock(&qlock);
struct writePacket* a;
a=new writePacket;
a->sockID = sockA->sockid;
a->pp = sockA->pp;
a->seq = recvdata.seq;
memcpy(a->datamsg,recvdata.datamsg,1466);
pc = pthread_create(&write[counter], NULL, writePack,(void *) a);
if (pc)
{
printf("ERROR; return code from pthread_create() is %d\n", pc);
exit(-1);
}
I have create a pthread_create inside a pthread_create, I have used socket programming, where I receive a packet and then create a thread which does the writing to the file. When I send a very large file, i get this error...??
The code is as follows...
void *writePack(void *sock)
{
size_t nbyte;
ssize_t writeSize;
nbyte = 1466;
off_t offset;
offset = (((struct writePacket *)sock)->seq * 1466);
char* buffer = new char();
buffer = ((struct writePacket *)sock)->datamsg;
writeSize = pwrite(((struct writePacket *)sock)->pp,(const void *)buffer, nbyte, offset );
free(buffer);
pthread_exit(NULL);
}
this is the code of the parent receive code...
recvfrom(sockA->sockid, (void *)&recvdata, sizeof(struct data), 0, (struct sockaddr *) &cli_addr, &clilen);
if (n<0)
error("Error on reading");
pthread_mutex_lock(&qlock);
struct writePacket* a;
a=new writePacket;
a->sockID = sockA->sockid;
a->pp = sockA->pp;
a->seq = recvdata.seq;
memcpy(a->datamsg,recvdata.datamsg,1466);
pc = pthread_create(&write[counter], NULL, writePack,(void *) a);
if (pc)
{
printf("ERROR; return code from pthread_create() is %d\n", pc);
exit(-1);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能是您创建的线程没有机会执行,而您一直在创建越来越多的线程从套接字读取数据。
相反,创建一个从套接字读取数据的线程,另一个执行写入操作的线程,并在两个线程之间传递消息中的数据。
It could be that the threads you create didn't get a chance to execute, while you've been creating more and more threads reading from the socket.
Instead, create a thread that would do the reading from the socket, another thread that would do the writing, and pass the data in messages between the two threads.