Using multiple threads with the console - c

I am using pthreads to create an multithreaded application (in this case a chat client - that mostly works, actually.)
The problem I have is that I have one thread trying to read and the other thread trying to print to the same console window.
For example if the user types in something to the console, but receives a message from the other thread, another, he could be looking at somethinganother - but when he presses enter the only data he submits is something!
Could anyone be so kind as to tell me how to deal with this?

Related

How to design a single background process that can be called multiple times independently?

I am writing for generic linux/unix systems and have an unusual use case for which the design of the program is not clear to me.
(FYI: for those not familiar with the unix mentality, sometimes called the Unix Philosophy, the idea is to provide functionality by making lots of small programs that run independently, rather than making large conglomerate applications.)
So, in my case I want an application that will provide alerts. The application would be run from the command line with parameters defining the characteristics of the alert, then it would put itself into the background and wake up when the alert needs to be issued.
The problem is when a second alert is created. Rather than create a second process, I would like the application to notify the existing background process of the new alert and add it to its list of alerts. That way I have only a single "alert" process at any given time. One advantage of this is that since only one process controls all the alerts, it can list them. For example, the user might give a command like "alert list" and alert(2) will notify the existing alert process of the request and exit, then the existing process will print out all the alerts that are pending, then go into the background again.
What is the right way to do this?

How can I determine whether a specific process is responsive?

I'm working on something that's basically a task manager. I have a working prototype but now I'd like to be able to show applications that are not responsive.
Are there APIs that can tell me whether this is the case under Window, macOS, Linux? I need to do this without the cooperation of the processes, so I can't just send a custom message and wait for a response.
Preferably C, but I can work with Obj-C & co.

I don't know how to get orders from the terminal in a multithread situation

I am working on an UDP Socket project. I use 2 threads in my client program. The first thread registers and maintain connexion with the server. The second thread needs to answer some orders from the terminal, for example sending the configuration of the client to the server.
I would need to be able to type onto the terminal when the program is running to inform the second thread of what it needs to do but I don't know how to implement it in my code, and I don't even know what to look for on google.
I haven't tried anything since I have absolutely no idea how to do this
I would like to be able to type "sendconf" for example in the terminal while the program is running and have the second thread answering to that. I know how to send the configuration of the client to the server, I just don't know how to inform the thread it needs to do this.
Message queues?
Add a "message" (the command) to a queue that the second thread polls, it then processes the message and sends a reply to another queue which is polled by the first thread.
If you want to be synchronous instead, the second thread can instead set data directly in the message structure for the reply, and then set a flag that it's finished. The first thread keeps waiting on the flag and then gives the result to the user.

Receiving asynchronous user and external input using ncurses

I'm making a 2-player game which is controlled by a player from keyboard and at the same time receives input from a server program using message queues. Initially I tried to implement the player console in the parent process and make a child process to react to messages from the server, but every time I modified the screen with the child process it deleted all the changes I made in the parent process - in other words, the processes had separate screens. It looks like there has to be one process which manages both the keyboard input and the server input, but I have no idea how to do this asynchronously.
Does anyone know how to do it? Alternatively, what other libraries could I use to make a game like that with a GUI?
OK, if anyone's interested, I have the solution. I used the STDIN_FILENO stream from unistd.h and the poll function to read data from both the keyboard and the pipe which informs about messages from the server.

How to find where a process is stuck using DDD

I have a TCP Svr process written in C and running on CentOS 5.5. It acts as a TCP Server for external clients and also does some IPC communication with other processes in the system using Unix Domain Sockets it has establised. It's not a multi threaded process. It does one task at a time. There's an epoll_wait() I use to listen for requests on either the TCP socket or any of the IPC sockets it has established with internal processes. When the epoll_wait() function breaks,I process the request for whoever it is and then go back into epoll_wait()
I have a TCP Client that connects to this Process from outside (not IPC). It connects sucessfully, sends a request msg, gets a response back. I've put this in an infinite loop
just to test out its robustness etc.
After a while, the TCP Server stops responding to requests coming from TCP Client. The TCP client connects successfully, sends a request message, but it doesnt get any response msg back from the TCP server.
So I reckon the TCP server is stuck somewhere else, trying to do something and has not returned to the epoll_wait() to process
other requests coming in. I've tried to figure it out using logs, but thats not helping me understand where exactly the process is stuck.
So I wanted to use any debugger that can give me some information (function name would be great), as to what the process is doing. Putting breakpoints, is overwhelming cause the TCP Server process has tons of files and functions....
I'm trying to use DDD on CentOS 5.5, to figureout whats going on. I attach to the process successfully. Then I click on "Step" or "Stepi" or "Next" button....
but nothing happens....
btw when I use Eclipse for debugging, and attach to this process (or any process), I always get "__kernel_vsyscall()"....Does this mean, the program breaks by default at
whatever its doing? If thats the case, how do I come out of the __kernel_vsyscall() call, to continue within my program? If I press f8, it comes out, but then I dont know where it was, since I loose the stack trace....Like I said earlier. Since I cant figure where it was, I dont know where to put breakpoint....
In summary, I want to figureout where my process is stuck or what its doing and try to debug from that point on....
How do I go about this?
Thanks
Amit
1) Attaching to a C process can often cause problems in itself, is there any way for you to start the process in the debugger?
2) Using the step functions of DDD need to be done after you've set a breakpoint and the program is stopped on a command. From reading your question, I'm not sure you've done that. You may not want to set many breakpoints, but is setting one or two in critical sections of code possible?
In summary, What I wanted to accomplish was to be able to find where my program is stuck, when it hangs. I figured it out - It was so simple. Create a configuration in Eclipse ...."Debug Configurations->C/C++ attach to application"...
Let the process run normally from shell (preferably with a terminal attached). When it hangs, open eclipse, click on the debug icon and run the configured process. It'll ask you to attach to a process. Look for your process name and attach to it.
Now, just look at the entire stack trace....you'll see some of your own function calls mixed with kernel function calls. That tells you where the program is stuck.

Resources