seereddi sekhar
seereddi sekhar

Reputation: 881

Kill all the process from main after specific time

Below is my query. I am having problem with the bold text. "You will write a program that uses multiple processes to compute the sum of a set of (small) positive integers. There are two types of processes for this homework: I). A set of "slaves" processes: Each slave process gets two small integers from its argv, computes its sum and returns the result using the exit system call. So, a slave process is created for every sum. II) A "master" process: This process is responsible for creating the slave processes, and coordinating the computation. Note that all the computation is done by the "slave" processes. All the numbers are provided in the command line argv. The master process also set a timer at the start of computation to 3 seconds. If the computation has not been finished by this time, the master process kills all the slaves and then exits. You should print an appropriate message(s) in this case. Note that the master process may have to create multiple sets of slave processes. For example, if there are 8 numbers to be added, then the master process will first create 4 slaves and get the result from them. At this point there are 4 numbers, and it creates 2. slaves. Finally one slave is created to compute the overall sum. To make it simpler, if the number of integers to add is odd, the master adds a 0 to the list of numbers. This may happen at any step during the computation. The code for master process should be compiled separately and its executable code should be called master. The executable code for the slave process should be called slave. So, to compute the sum of the numbers 1 through 7, the command line will look like master 1 2 3 4 5 6 7 Since the results are passed around by exit system call, keep the numbers small (single digit). Each slave process prints its process id, its operands, and their sum. Each time the master gets a result from a slave, it prints the pid of the slave and the partial sum."

Upvotes: 1

Views: 576

Answers (3)

zoska
zoska

Reputation: 1723

You are asked to use processes. It means you need to fork for every worker that is needed. Also

The master process also set a timer at the start of computation to 3 seconds. If the computation has not been finished by this time, the master process kills all the slaves and then exits.

It would be best to set alarm, which would send you a signal SIGALARM and when it's received you will use kill on specific processes ids (you can get them from fork). If your workers will finish, so will your main process and alarm won't trigger.

It's somehow complicated task if you are new to multiprocess programming. Now just try creating some processes and killing them after for e.g. 3 seconds. If you will have more problems, post your code and ask specific questions.

Upvotes: 2

tristan
tristan

Reputation: 4322

not sure if i get your question right. You may want to start a helper thread for each worker thread that does the computing. Pass the tid of the worker thread to its helper thread and let the helper thread sleep for 3 seconds and then check if the worker thread has exited and if not just kills it. Or alternatively if you have a dedicated timer module/task, you could start a timer for each worker thread.

Upvotes: 0

unwind
unwind

Reputation: 399863

  1. When creating threads, store their identifiers.
  2. After creating threads in main(), pause using e.g. sleep(3) or whatever.
  3. Loop over the stored thread ID:s, calling pthread_cancel() to kill them. Killing threads that have already terminated will cause this call to fail, but that's fine.

Upvotes: 0

Related Questions