Problem with threads

R

Rodney Leger

I have written an app in C on Linux(Redhat 9) that produces a menu and
gives the user a number of options to choose. The menu is in main and
is part of of a while loop.

ie. while(1)
{
printf("1 - Process line 1\n");
......

process data
}

Prior to executing this loop, I set a thread to accept data from
another source.
The data that is accepted in the main while loop and the thread are
independent of each other and unrelated.

status = pthread_create(&thread_id, NULL, receive_loop, NULL);

receive_loop is similar to the main while loop in that it is composed
of a while loop that reads data from another source.

The problem is that the first main while loop only works once and
stops accepting data after it processes the first menu option.
However, the thread continues to process. The receive_loop thread
works flawlessly. When I remove the receive_loop thread call then the
main while loop works flawlessly.

Obviously, I am missing something. Should the main while loop which
is (currently part of main) be setup in its own thread?

What else can I try to resolve this problem.

Your help is appreciated.

Thanks-
Rodney
 
P

Patrick TJ McPhee

% I have written an app in C on Linux(Redhat 9) that produces a menu and
% gives the user a number of options to choose. The menu is in main and
% is part of of a while loop.

[example which doesn't actually show anything elided]

% Prior to executing this loop, I set a thread to accept data from
% another source.
% The data that is accepted in the main while loop and the thread are
% independent of each other and unrelated.

[...]

% The problem is that the first main while loop only works once and
% stops accepting data after it processes the first menu option.
% However, the thread continues to process. The receive_loop thread
% works flawlessly. When I remove the receive_loop thread call then the
% main while loop works flawlessly.

I suggest connecting in the debugger to see where main() is blocked.

% Obviously, I am missing something. Should the main while loop which
% is (currently part of main) be setup in its own thread?

Unlikely. Try whittling down to a minimal example. The process of doing
that will often help you identify the bug yourself, but if not, you
can post the minimal example, which should allow someone to spot it
for you.

I've set follow-ups to comp.programming.threads, as the problem seems to
be thread-related.
 
R

Rodney Leger

Thanks for the info. It seems that my problem is related to some sort
of blocking IO problem. What has me confused is that each thread is
operating on a different file descriptor, one on a local socket
connection(the thread) and the other on a serial device connection(the
main thread). I thought that this would let them act independently.


However, I am not sure how to fix the problem.

I hope this code example provides enough information to resolve my problem.

int main(void)
{

fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
socket_fd = socket(PF_LOCAL, SOCK_STREAM, 0);
... local socket code ...


pthread_t thread_id;

status = pthread_create(&thread_id, NULL, receive_loop, NULL);

do
{
... local socket stuff ..
int client_socket_fd;

... more local socket stuff ...

socket_server(client_socket_fd);

} ......
}

int socket_server(int client_socket)
{
while(1)
{
...

read(client_socket, &length, sizeof(length));


... process request

}

}

void *receive_loop(void)
{

while(1)
{
receive_data();
}
}

void receive_data(void)
{
.....

n = read(fd, &buf, 255);


... process data
}



The code seems to block in the read call of receive_data(Removing this
read allows the socket_server process to work as expected.)

As such, the socket_server code reads data once and then "stops"
reading. I thought that since the reads in socket_server and
receive_data are reading from different file descriptors then they
wouldn't block each other, however, this does not seem to be the case.


Any help is greatly appreciated.

Thanks-
Rodney
% I have written an app in C on Linux(Redhat 9) that produces a menu and
% gives the user a number of options to choose. The menu is in main and
% is part of of a while loop.

[example which doesn't actually show anything elided]

% Prior to executing this loop, I set a thread to accept data from
% another source.
% The data that is accepted in the main while loop and the thread are
% independent of each other and unrelated.

[...]

% The problem is that the first main while loop only works once and
% stops accepting data after it processes the first menu option.
% However, the thread continues to process. The receive_loop thread
% works flawlessly. When I remove the receive_loop thread call then the
% main while loop works flawlessly.

I suggest connecting in the debugger to see where main() is blocked.

% Obviously, I am missing something. Should the main while loop which
% is (currently part of main) be setup in its own thread?

Unlikely. Try whittling down to a minimal example. The process of doing
that will often help you identify the bug yourself, but if not, you
can post the minimal example, which should allow someone to spot it
for you.

I've set follow-ups to comp.programming.threads, as the problem seems to
be thread-related.
 
J

Joona I Palaste

Rodney Leger <[email protected]> scribbled the following
Thanks for the info. It seems that my problem is related to some sort
of blocking IO problem. What has me confused is that each thread is
operating on a different file descriptor, one on a local socket
connection(the thread) and the other on a serial device connection(the
main thread). I thought that this would let them act independently.

This is off-topic for comp.lang.c as the C standard does not define
threads at all. Please drop comp.lang.c from future replies. Thanks.

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"Outside of a dog, a book is a man's best friend. Inside a dog, it's too dark
to read anyway."
- Groucho Marx
 
C

CBFalconer

*** and topposted much off topic stuff on c.l.c ***
Thanks for the info. It seems that my problem is related to some sort
of blocking IO problem. What has me confused is that each thread is
.... which has been snipped ...
% I have written an app in C on Linux(Redhat 9) that produces a menu and
% gives the user a number of options to choose. The menu is in main and
% is part of of a while loop.

[example which doesn't actually show anything elided]

[...]

Unlikely. Try whittling down to a minimal example. The process
of doing that will often help you identify the bug yourself,
but if not, you can post the minimal example, which should
allow someone to spot it for you.

I've set follow-ups to comp.programming.threads, as the problem
seems to be thread-related.

Now Mr McPhee was kind enough to give you advice, and to remove
the off topic cross-postings. Yet in answering him you have
immediately put back the annoying off-topic cross postings. It
appears your objective is to annoy, rather than get or give
information.

I have also set follow-ups, so that this should not appear here
anymore.
 
R

Rodney Leger

After doing a little more research I have discovered the select
mechanism and realize that my problem was not related to threads but
to blocked IO. For the time being I have simply made one of the file
descriptors non-blocking until I can figure how to properly
incorporate the select system call into my code.

Thanks-
Rodney


Rodney Leger said:
Thanks for the info. It seems that my problem is related to some sort
of blocking IO problem. What has me confused is that each thread is
operating on a different file descriptor, one on a local socket
connection(the thread) and the other on a serial device connection(the
main thread). I thought that this would let them act independently.


However, I am not sure how to fix the problem.

I hope this code example provides enough information to resolve my problem.

int main(void)
{

fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
socket_fd = socket(PF_LOCAL, SOCK_STREAM, 0);
... local socket code ...


pthread_t thread_id;

status = pthread_create(&thread_id, NULL, receive_loop, NULL);

do
{
... local socket stuff ..
int client_socket_fd;

... more local socket stuff ...

socket_server(client_socket_fd);

} ......
}

int socket_server(int client_socket)
{
while(1)
{
...

read(client_socket, &length, sizeof(length));


... process request

}

}

void *receive_loop(void)
{

while(1)
{
receive_data();
}
}

void receive_data(void)
{
.....

n = read(fd, &buf, 255);


... process data
}



The code seems to block in the read call of receive_data(Removing this
read allows the socket_server process to work as expected.)

As such, the socket_server code reads data once and then "stops"
reading. I thought that since the reads in socket_server and
receive_data are reading from different file descriptors then they
wouldn't block each other, however, this does not seem to be the case.


Any help is greatly appreciated.

Thanks-
Rodney
% I have written an app in C on Linux(Redhat 9) that produces a menu and
% gives the user a number of options to choose. The menu is in main and
% is part of of a while loop.

[example which doesn't actually show anything elided]

% Prior to executing this loop, I set a thread to accept data from
% another source.
% The data that is accepted in the main while loop and the thread are
% independent of each other and unrelated.

[...]

% The problem is that the first main while loop only works once and
% stops accepting data after it processes the first menu option.
% However, the thread continues to process. The receive_loop thread
% works flawlessly. When I remove the receive_loop thread call then the
% main while loop works flawlessly.

I suggest connecting in the debugger to see where main() is blocked.

% Obviously, I am missing something. Should the main while loop which
% is (currently part of main) be setup in its own thread?

Unlikely. Try whittling down to a minimal example. The process of doing
that will often help you identify the bug yourself, but if not, you
can post the minimal example, which should allow someone to spot it
for you.

I've set follow-ups to comp.programming.threads, as the problem seems to
be thread-related.
 
J

Joona I Palaste

Rodney Leger <[email protected]> scribbled the following
After doing a little more research I have discovered the select
mechanism and realize that my problem was not related to threads but
to blocked IO. For the time being I have simply made one of the file
descriptors non-blocking until I can figure how to properly
incorporate the select system call into my code.

What part of "stop cross-posting off-topic messages on comp.lang.c" was
too difficult for you to understand?

(snip over 100 lines of needlessly quoted text)

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"I am not very happy acting pleased whenever prominent scientists overmagnify
intellectual enlightenment."
- Anon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top