0

Hi everyone!
I have a task that needs to be made in C in Linux. I use Kubuntu.
This is the task:
Process (coordinator) creates 10 working process (pool of processes). The working processes are waiting to be awakened by the coordinator, perform a job and wait again. The coordinator transmits tag (integer) to the first process and it wakes it up. The awakened first process adds 1 to the tag and transmit it to the next process, as it signals the coordinator. The coordinator awakens the next process, which performs same actions etc. The tag should pass through each process 10 times and then the coordinator should visualize its contents. Use semaphores for the signalization of the processes, and for the tag (marker) use channels.
Solve the problem also with threads by selecting the most appropriate way of communication.

Any ideas? I'm absolutely clueless about this.
Thanks in advance!

1
Contributor
1
Reply
3
Views
5 Years
Discussion Span
Last Post by denislaf
0

I've this program with 10 precess and use signals but not semaphores
help ! =))

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>

#define nWORKERS 10
#define nTOURS 10
#define FIRST_TAG 60


void sig_handle(int signum) { return; }

int main(int argc, char *argv[])
{
int i, j, tag, pipefd[2], end_trigger=-1;
pid_t worker[nWORKERS], co_pid;
const struct sigaction siga = { sig_handle, 0, 0, 0, 0 };
sigset_t sset, prev_sset;

sigemptyset(&sset);
sigaddset(&sset, SIGUSR1);
sigprocmask(SIG_BLOCK, &sset, &prev_sset);

if (sigaction(SIGUSR1, &siga, NULL) == -1) {
perror("sigaction()");
exit(EXIT_FAILURE);
}
if (pipe(pipefd) == -1) {
perror("pipe()");
exit(EXIT_FAILURE);
}

for (i=0; i < nWORKERS; i++)

if ((worker = fork()) == -1) {
perror("fork()");
exit(EXIT_FAILURE);
}

else if (worker == 0) { /* child worker */

co_pid = getppid();

while (1) {
kill(co_pid, SIGUSR1);
sigsuspend(&prev_sset);
if (read(pipefd[0], &tag, sizeof(int)) == -1) {
perror("read() in worker");
exit(EXIT_FAILURE);
}
if (tag == end_trigger) {
printf("---worker received end trigger\n");
close(pipefd[0]);
close(pipefd[1]);
exit(EXIT_SUCCESS);
}

tag++;
printf("---worker producing tag:%d\n", tag);
if (write(pipefd[1], &tag, sizeof(int)) == -1) {
perror("write() in worker");
exit(EXIT_FAILURE);
}
}
}
else /* parent coord. */
sigsuspend(&prev_sset);


for (tag=FIRST_TAG, i=0; i < nTOURS; i++) {

printf("Tour: %d\n", i);

for (j=0; j < nWORKERS; j++) {

printf("Sending tag:%d to worker %d\n", tag, j);

if (write(pipefd[1], &tag, sizeof(int)) == -1) {
perror("write in coord.");
exit(EXIT_FAILURE);
}
kill(worker[j], SIGUSR1);

sigsuspend(&prev_sset);
if (read(pipefd[0], &tag, sizeof(int)) == -1) {
perror("read in coord.");
exit(EXIT_FAILURE);
}

printf("Received tag:%d from worker %d\n", tag, j);
}
}

for (i=0; i < nWORKERS; i++) {
if (write(pipefd[1], &end_trigger, sizeof(int)) == -1) {
perror("write() end trigger");
exit(EXIT_FAILURE);
}
kill(worker, SIGUSR1);
waitpid(worker, NULL, 0);
}

close(pipefd[0]);
close(pipefd[1]);
return(EXIT_SUCCESS);
}

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.