Programming :: Synchronize 2 Posix Threads ?

Mar 15, 2011

I have 2 threads and both of them are deleting memory at the end nedded by both.

My problem is that maybe it can happen that a thread start and finish before the other one starts and so it deletes the memory nedded by the other thread. How can I synchronize them so that this can't happend.

As a design my threads look like this:


The other thread looks the same, but this isn't unoff to stop thread1 to finish before thread2 starts.

View 4 Replies


Programming :: Proper Implementation Of POSIX Threads

Apr 6, 2011

I've implemented a program URL... which reads digital IF data from a radio receiver through a named pipe, measures power levels, and sends the result to stdout. The program is interactive; there is a thread that reads from stdin to watch for commands, a thread that constantly either reads data from the named pipe or throws data away, and an array of processing threads. The program uses GTK+extra to plot the signals. The IF data stream bandwidth exists at the limits of today's technology (is very very fast).

Problem Statement:The program works fine with a few bugs. I've learned since I've made it that using global state variables to coordinate threads isn't a good way of doing it. I also only had knowledge of mutexes and polled the state variable instead of using other methods.My reimplementation will use the following:

- One "Stdin Command Monitoring" thread
- One "Get data from named pipe" thread
- One post-processor thread
- N Processing threads

All threads are alive during the life of main()There are N buffers. Data will come in from the named pipe, and the "Get data" thread will write the data to an "available" buffer. When the buffer is full it will be marked as "full". There will be N processing threads, one for each buffer. When a processing threads' buffer is full, it will process the buffer and save the result to a final buffer. At the end of a number of averages, the post-processor thread will perform a final process on the final buffer and send the results to stdout.

View 2 Replies View Related

Programming :: Posix Message Queues - Communicate Between Two Threads?

Jan 3, 2011

I want to communicate between two threads, each belonging to a different process. Iam using message queues for this. I use mq_open()call. I created the queues with the same queue name starting with a '/'. But when I open the queue, the queue ID is different in both the process. What should I do so that both the process have the same queue ID?

View 1 Replies View Related

Programming :: Closing / Unlinking POSIX Message Queues?

Apr 15, 2011

I have a pthread waiting forever on a POSIX message queue and then call mq_close and mq_unlink on the POSIX message queue. I've found that the pthread never wakes up from it's call to mq_receive and remains blocked indefinitely. Is there a way to wakeup all pthreads blocked on a POSIX message queue after calling mq_close/mq_unlink? The goal is to include error handling during message queue deletion to avoid leaving any pthreads blocked forever.

View 2 Replies View Related

Programming :: Find Number Of Child Processes (C / POSIX)?

May 26, 2010

I'm in the process of writing a program that is a server- it will accept connections and stuff, and spawn a child process for each. However, i've run into a small problem. I do NOT want to bother with keeping track of the processes unless i need to. So, i set SA_NOCLDWAIT (#ifdef) on a SIG_IGN to the SIGCHLD handler through sigaction interface. The standard says that it the kernel will then keep track of reaping zombie processes for me (a HUGE plus). However, upon receiving a SIGINT signal, i want to stop the server from accepting new connections (done), and then wait for there to be no new connections. I was thinking of just putting a loop like so:


while((wait(NULL) != (pid_t)-1) && errno != ECHILD);

However, I'm not *sure* that this will work, especially with SIGCHLD still ignored. So how can i tell if there are still child processes? I can't find any call like int getnumchld(pid_t proc); (i wish). Plus it would be inefficient to spin on that function anyway. OTOH, i would rather *NOT* have to do the same thing in a loop with a system("ps |...>file"); read(file); etc. either. Is there a way i can portably implement this feature (I was hoping i could run it on linux and the major BSDs, at least).


How can i tell if a process has no child processes if i've SIG_IGN'd SA_NOCLDWAIT'd the SIGCHLD? Is there a _reasonably_ portable way to do so? I *don't* want to manually wait for EVERY process. Maybe only those still active at the time of SIGTERM, but that requires keeping track of the number of connections and whether those have terminated...

EDIT: Does anyone know if the above code *would* work, even with SIGCHLD ignored and the kernel cleaning up zombies *for* me? I checked the manpage and it doesn't say much.

EDIT1: Note that all of the processes are in the same process group and session. SO i can find them through this as well. Perhaps even setting the uid/gid and finding all processes run by that group?

EDIT2: i have an idea if the above isn't feasible. If there is no "elegant" way to do it, i could reduce the complexity by sending a SIGUSR1 to the whole process group. Each process would then set a flag telling it to send a SIGUSR1 in reply and send a SIGUSR2 when it is done executing. Then i could keep a count of signals. Maybe that would be *easier*. Or perhaps a count of all child processes and just a termination signal to decrement the counter.

View 2 Replies View Related

Programming :: Related To Pthreads And Timers In POSIX Standard

Feb 17, 2010

I have created two threads:
Thread 1 and thread 2

In the main thread, i have put up a timer which generates a signal periodically when it expires, which reults in execution going to signal catcher function.when this function is invoked, under certain condition it goes to thread 1 and under another condition it goes to thread 2. The problem lies here that when it goes to thread2,i have a loop to process but it doesnt come out of the loop and hence from thread2, even though timer has expired.

View 2 Replies View Related

Programming :: Unix Programming - Single Process That Does Not Start Up Any Other Threads

Sep 28, 2010

i want a process that can operate as both a TCP echo server and a UDP echo server. The process can provide service to many clients at the same time, but involves a single process that does not start up any other threads.

View 3 Replies View Related

Programming :: Program Hang Stuck There Signal Handling On POSIX Message Queue UNIX C Pr

Jun 14, 2011

In a single main() function,so need signal handling. Use Posix Message Queue IPC mechanism , can ignore the priority and other linked list message,to implement the scenario:

View 1 Replies View Related

Programming :: How To Run The Threads Continuously

Dec 1, 2010

I have two threads & i want to run it continuously ,with while(1) it is possible . Is there any other way to run the threads continuously

View 12 Replies View Related

Programming :: Server With 2 Threads Per Client?

Nov 25, 2010

In all the examples I have found the server accepts the client's conection, proccess the data received and close the socket. In an very schematic way it would be something like:


select to see if there is data to read from socket fd
if there is something to read{


Should I use mutexs or semaphores to block the socket fd before read and write or it is not necesary?

View 4 Replies View Related

Programming :: Listing All Threads In A Process?

Dec 29, 2010

How to list all the threads spawned by a process?

View 4 Replies View Related

Programming :: Threads Not Getting Arguments Properly

Mar 26, 2011

I wrote a C program using Pthreads to compute the product of 2 matrices. Each element in the product matrix is computed in a separate thread. Eg: Thread (i,j) computes the element C[i][j] of the matrix C, where C=A*B. A is m*n, B is n*p, C is m*p. m,n,p are given as command-line arguments. A and B are initialized to random values from 1 to 10, while all elements of C are initialized to -1.But some threads do not get their arguments (i,j) correctly. So some elements C[i][j] still remain as -1, even after the program is over. My OS is Ubuntu 10.10 (Maverick Meerkat) 32-bit.I ran the program on another computer and it worked correctly. Is it due to a problem in the Pthreads library in my OS? Please help me. I have attached the source code.

View 3 Replies View Related

Programming :: CPU Sharing Between Process And Threads?

Mar 29, 2011

I have doubt regarding cpu sharing between process and threads.In my program iam creating 4threads=> 1 process+4 threads. How is cpu alloted to these all tasks. Is here process is getting cpu time like thread or having more cpu time than threads.

View 1 Replies View Related

Programming :: Data Transfer Between Threads?

Dec 14, 2010

In posix multi threading, how to send thread1 local data to thread2...?

View 3 Replies View Related

Programming :: Multiple Threads In Socket C++?

Nov 26, 2010

void* thread(void* arg)


View 5 Replies View Related

Programming :: Synchronize Two Directories (bash Script)?

Feb 11, 2011

It achieves a bash script that takes two parameters (command line). These two parameters must be two paths of two directories (existing, and different such as not being of the other sub-folder). Path1 and path2 are for simplicity.The script will recursively visit the two directories and all sub-directories, sub-sub-directories, etc.. Looking for differences between their contents (files or directories). In particular, suppose that we work with both path1 and path2 directory, and scanned the files contained therein. These situations can occur:

* Path1 exists in a file that is not present in path2: in this case, the script displays the path of the file to you (print screen) and asks if he wants the file is copied in path2 or not. Reads the user's response and acts accordingly.

* Path2 exists in a file that is not present in path1: in this case, the script displays the path of the file and asks if the user wants the file is copied in path1 or not. Reads the user's response and acts accordingly.

* Both path1 path2 in a file with the same name but different contents: in this case, the script displays the path of the two files (which of course will start with path1 and path2 but end with an equal) to the user together with the two dates have been changed last time. Then asks if you want the file to be replaced by one in path1 path2, or that the file is replaced by one in path2 path1, or that both are left unchanged. Reads the user's response and acts accordingly.

* Both path1 path2 in a file with the same name and same content, in which case the script does nothing and proceeds with the next file.This is done for all regular files in path1 and path2. The special file (socket, device, symbolic link ...) are ignored. For folders in path1 and path2 is recursively by:

* Path1 if there is a folder that is not present in path2: in this case, the script displays the path to the folder and asks if you want the folder and all its contents are copied to path2. Reads the user's response and acts accordingly.

* Path1 if there is a folder that is not present in path2: similar at first.

* Both path1 path2 that there are two folders with the same name as the script works recursively on the two folders.

View 2 Replies View Related

Programming :: Synchronize Sound Cards On Different Computers?

Dec 27, 2010

I've written a "C" program which transmits audio to a number of computers over a TCP LAN connection. I'm using ALSA, the preemptive kernel, and pthread. After running for 30 minutes or so the slight variation in sampling rates (~+-.01%) among the computers accumulates and manifests as a noticeable differential delay in the sound from the speakers. I know how to detect the variation and would like to dynamically compensate for it by individually varying the sampling rate (ever so slightly) of each playback device to oppose the variation.

Does anybody out there in Linux Land know how to dynamically vary the playback sample rate? I've tried using snd_pcm_hw_params_set_rate() and snd_pcm_hw_params_set_rate() followed by snd_pcm_hw_params() to no avail. They don't seem to work when playback is running.

View 8 Replies View Related

Programming :: Code Of Merge Sort With Threads In C?

Feb 17, 2011

Please I want Know the code of the Merge Sort with threads in C

View 7 Replies View Related

Programming :: Command For Seeing Number Of Threads Running?

Jun 8, 2011

IS there any command to see number of threads are running in a process .I have check ps -eLf but it wont show display for all the threads

View 1 Replies View Related

Programming :: How To Communicate Between Two Threads Using Message Queue

Jun 7, 2011

How to communicate between two threads using message queue?

View 4 Replies View Related

Programming :: Reduce Memory Cost Of Threads?

Jul 21, 2011

Fedora15 32bit. I write a test program, it creates new thread continually, the thread does nothing but sleep. I find virtual memory increases up almost 10Mb when a new thread is created. and when there's more than 200 threads, the virtual memory used by the program is 3Gb, and now cann't create new thread. but on windows, it costs little memory. What can I do to config the operation system to take less memory on threads?

View 11 Replies View Related

Programming :: Timings Of A Program That Uses 1-8 Threads On A Server That Has 4 D?

Jul 25, 2010

I am runig a program on a server at my university that has 4 Dual-Core AMD Opteron(tm) Processor 2210 HE and the O.S. is Linux version My program implements Conways Game of Life and it runs using pthreads and openmp. I timed the parrallel part of the program using the getimeofday() function using 1-8 threads. But the timings don't seem right. I get the biggest time using 1 thread(as expected), then the time gets smaller. But the smallest time i get is when i use 4 threads.

Here is an example when i use an array 1000x1000.

Using 1 thread~9,62 sec, Using 2 Threads~4,73 sec, Using 3 ~ 3.64 sec, Using 4~2.99 sec, Using 5 ~4,19 sec, Using 6~3.84, Using 7~3.34, Using 8~3.12.The above timings are when i use pthreads. When i use openmp the timing are smaller but follow the same pattern.I expected that the time would decrease from 1-8 because of the 4 Dual core cpus? I thought that because there are 4 cpus with 2 cores each, 8 threads could run at the same time. Does it have to do with the operating system that the server runs?

Also i tested the same programs on another server that has 7 Dual-Core AMD Opteron(tm) Processor 8214 and runs Linux version 2.6.18-194.3.1.el5. There the timings i get are what i expected. The timings get smaller starting from 1(the biggest) to 8(smallest excecution time).The program implements the Game of Life correct, both using pthreads and openmp, i just cant figure out why the timings are like the example i posted. So in conclusion, my questions are:

1) The number of threads that can run at the same time on a system depends by the cores of the cpus?it depends only by the cpus although each cpu has more than one cores? It depends by all the previous and the Operating System?

2) Does it have to do with the way i divide the 1000x1000 array to the number of threads? But if i did then the openmp code wouldn't give the same pattern of timings?

3)What is the reason i might get such timmings?

View 4 Replies View Related

Programming :: Good Book To Learn About Threads In C / C++?

Oct 30, 2010

A good book to learn about threads in C/C++?

View 3 Replies View Related

Programming :: Mutexes Necessary - Gets Locked / Unlocked Each Time One Of The 60 Threads?

Jun 24, 2011

I'm a bit worried about "too many mutexes" in my little curses-based app and would like to get confirmation/opinions that I'm doing this right. I've got an array: int nums[60] I've got 61 threads. 1-60 are doing math on the value in their array index (ie: thread1 increments nums[1], threadN increments nums[N]), then sleep(1) The 61st thread is my curses thread which does a for-loop over the array and prints out all the values to the screen, then sleep(1)

Right now, I've got 1 mutex which gets locked/unlocked each time one of the 60 threads needs to update its array-index with a new value, and the 61st thread locks the same mutex just before the for-loop beings reading the values and unlocks after ending the loop.

My questions:
A) Does the above seem OK? (I know it's ok, cause everything works right now but would like opinions on it)
B) Do I even need the mutexes since 1-60 only ever update their own index and 61 just reads?
C) If I do need the mutex protection, is there a better, more efficient way?

View 11 Replies View Related

Programming :: Synchronization Between Threads Fails - Resource Unavailable

May 6, 2009

I am working with a C++ program consisting of two threads. The first threads receives packets through an UDP unicast connection and stores them in a buffer. The second thread reads the packets from the buffer and sends them through an UDP multicast connection. Both use blocking sockets and share a common buffer and a linked list L1, which are protected by mutexes. The program seemed to work just fine, receiving a packet and sending it almost immediately, but started giving some trouble recently. The synchronization between both thread started failing, and I decided to use a non-blocking socket in the sending thread. As a consequence, sendto() doesn't work in some cases, causing an errno 11 (Resource unavailable).

[Code] ...

View 4 Replies View Related

Programming :: Find Number Of Threads In The Process In C Language?

Dec 23, 2009

I need to find how many threads are alive with respect to the current process for my further processing. Is there any means to trace this number ?[URL]I referred the above link. But sys/pstat.h is not in my system. Don't know which library gives this header.

View 14 Replies View Related

Programming :: Can Two Threads Safely Work On A Linked List Without Locking

Nov 9, 2010

I have a linked list that two threads work on simultainiousley.The first thread is adding elements to end the linked list while the second is removing them from the front. Can this be done without a lock on the linked list head when elements are beingadded/removed?

I think this lock is causing a performance hit to my application. If there isnt any safe way without it then thats fine but just thought I would check.The first thread uses this fuction to add elements to the list. Full source here. [URL]


/* Lets add the new packet to the queue. */
pthread_mutex_lock(&workers[queuenum].queue.lock); // Grab lock on queue.
if (workers[queuenum].queue.qlen == 0){ // Check if any packets are in the queue.


View 8 Replies View Related

Programming :: C Code Profiling Technique That Supports Multiple Threads?

Mar 8, 2011

I am using GPROF for my code profiling ,but it seems GPROF doesn't supports multithread .Does any one know any other technique for profiling the application code I have checked oprofile but it profile kernel ,I just required for C code profiling technique that supports multiple threads

View 1 Replies View Related

Programming :: Pthread - Optimize Computing Function / Loop With Threads?

Sep 4, 2010

I have to compute prime numbers with threads in C. So I choose the algorithm "Sieve of Eratosthenes" and I tried to create 2 threads, but I don't know how to optimize my computing function with threads I beg for help. This is my function:


but it seems like the both of the threads are calculating the same function twice? Is it possible to be optimized or not?

View 7 Replies View Related

Programming :: Pthread_cond_wait - When Thread1 Invokes The Mutex Lock - No Other Threads Can Access It In Parallel

Mar 2, 2011

If multiple threads operate on a single shared resource, we can lock it using pthread_mutex_lock. Uptill that it is fine, but why again pthread_cond_wait?

int shared=0; // global
// Thread 1

When thread1 invokes the mutex lock, no other threads can access it in parallel. So why again and what for we use, pthread_cond_wait( ).

View 2 Replies View Related

Copyrights 2005-15, All rights reserved