Reputation: 435
I'm writing a program for synchronization between two child processes. The request is:
Implement a concurrent program in C language that creates two children: a sender and a receiver. The sender loops reading a string from the standard input and preparing a “message” of a single line in a file, then it signals to the receiver that the message is ready, it waits a signal from receiver that the string has been processed (simply transformed to upper case) and prints the processed string. The receiver loops waiting the signal of the sender, transforming it in upper case, and signalling to the sender that the string can be printed. The parent process waits the end on the two processes, which terminate when the message sent is the string “end”. The main process then has to print the number of sent messages.
Since for synch I have to use only signal, kill system calls, in my solution the kill system call doesn't wake up the Receiver process. For passing the PID of two childs I used two different files, each one for each PID. When the Sender works, and concurrently the Receiver goes to pause(), at the end of Sender, when I do kill(cpid2,SIGUSR1), the Receiver remains on pause()... Can you help me? The code is here: http://pastebin.com/Prxq9E2G
Upvotes: 0
Views: 777
Reputation: 903
I think that you simply need to set a signal handler for the signal used, using the system call
signal(SIGUSR1, signalhandler);
and the signal handler function could simple contain the same code (like the following function):
void signalhandler() {
signal(SIGUSR1, signalhandler);
}
Upvotes: 1
Reputation: 36401
There is a lot of problems with your code, at least:
message
points to a 1-byte length array, and you try to fill up to 80 chars in it (fgets
)...message
can't be shared like this among processes, each forked
child will have its own copy of the memory, i.e. its own message
array different from arrays of other processes.Upvotes: 0