The objective of this is to gain experience in programming concurrent tasks/proc
ID: 672663 • Letter: T
Question
The objective of this is to gain experience in programming concurrent tasks/processes in Unix environment by making system calls to Unix.
The following system calls might be useful to do this :
• fork() -- to create a new process.
• exec() -- to execute a file.
• wait() -- to wait for a process to terminate.
• signal() -- to handle asynchronous event.
• alarm() -- to set a timer.
• kill() -- to send a signal to kill a process.
• exit() -- to terminate a process
pipe() -- oldest form of UNIX interprocess communication • dup(), dup2() -- duplicates a file descriptor
To get help information about these routines, use the Unix "man" command. For instance, entering "man fork" will display the manual page entry for fork on the terminal. The manual pages are organized into sections. Section 1 is for Unix commands, section 2 is for Unix system calls and section 3 is for Unix library routines. Some entries are contained in more than one section. For example to obtain information about the system call wait() (rather than the command wait) use "man 2 wait" where the section is explicitly given.
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:
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.
Test your programs for the following cases:
master 1
master 1 2 3 4 5
master 1 2 3 4 5 6 7 8
Explanation / Answer
/* Master Program */
#include <stdio.h>
#include <iostream.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(int argc, char *argv[])
{
int PartialSum[20]=0,i,process,p, count=0;
int pid[20]= 0;
int ProcessStatus= 0;
int numberOfSlaves=0;
string arguments[];
if((argc-1 % 2) != 0) // check for total number of operands is even or odd
{
argv[argc] = 0; // if argc is odd, then include 0 as the last operand
argc ++; // and increment argc
}
numberOfSlaves=argc;
for(i=0;i<argc;i++)
{
arguments[i]=argv[i];
}
while(numberOfSlaves>1)
{
numberOfSlaves=numberOfSlaves/2;
p=0;
process=0;
for(i=0;i<numberOfSlaves;i++)
{
pid[count]= fork();
switch(pid)
{
case -1: cout<<"Slave Process cannot be created"<<endl;
exit(-1);
case 0: //execlp("work","work",arguments[p],arguments[p+1],NULL);
//here we have to pass the arguments to slave program
//SlaveProcess(2,arguments);
p+=2;
default: PartialSum[process]=wait(&ProcessStatus);
cout<<"The PID of the Slave process:"<<pid[count++]<<endl;
cout<<"Partial sum:"<<PartialSum[process++]<<endl;
break;
}
} // end for
for(i=0;i<numberOfSlaves;i++)
{
arguments[i]=PartialSum[i];
}
} // end while
if(numberOfSlaves==1)
cout<<"Sum of given integers:"<<arguments[0]<<endl;
for(i=0;i<count;i++) // to kill all slaves
{
waitpid(pid[i], NULL, WUNTRACED); // wait until the child is stopped
kill(pid[i], SIGCONT);
}
return 0;
}
===================================================================
/*Slave Program*/
#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int value1= atoi(arg[1]);
int value2= atoi(arg[2]);
cout<<"PID of the slave process:"<<getpid()<<endl;
cout<<"First Operand:"<<value1<<endl;
cout<<"Second Operand: "<<value2<<endl;
cout<<"Sum of the Operands:"<<(value1+value2)<<endl;
exit(value1+value2);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.