Operating Systems This homework is to get you familiar with system calls related
ID: 3880940 • Letter: O
Question
Operating Systems
This homework is to get you familiar with system calls related to processes in the UNIX operating system. You will write a program that uses multiple processes to compute the sum of a set of (small) positive integers. This is a very strange method of computing the sum of a set of numbers, but we will use it to learn about processes. There are two kinds of processes for this assignment: ( In order to do this assignment, you should get familiar with several system calls. The important ones are fork, execlp, exit, and wait system calls. You will also need getpid and perror system calls)
• A set of “worker” processes. Each worker process gets two small integers as command line parameters (they will be available in its argv), computes their sum and returns the result using exit system call. So, for every sum a worker process is created.
• A “coordinator” process. It is responsible for creating the “worker” processes, and coordinating the computation. Note that all the computation is done by the “worker” processes. All the numbers to be added are provided in the command line for the coordinator process.
Note that the coordinator may have to create multiple sets of processes. For example, if there are 8 numbers, the coordinator will first create 4 workers and get the results from them. At this point there are 4 numbers, and it creates 2 workers. Finally one worker is created to compute the overall sum. To make it simpler, if the number of integers to add is odd, the coordinator adds a 0 to the list of numbers. Note that this may happen at any step during the computation.
Create a subdirectory called cs3000 in your home directory. Create a subdirectory called assign1 in your cs3000 directory. Use that subdirectory to store all the files concerning this assignment and nothing else. You need to follow these general guidelines for all your future assignments as well. Name the two source files worker.c and coordinator.c. The code for worker process should be compiled separately and its executable be called worker. It should also be possible to execute the “worker” program as a standalone program. The executable for the coordinator process should be called coordinator. So, to compute the sum of the numbers 1 . . . 7, the command line would look something like: coordinator 1 2 3 4 5 6 7 Since the results are passed around by exit keep the numbers small (single digit). Note that this is not a good way for communication between processes. Each worker process should print its process id, its operands and their sum. Each time the coordinator gets a result from a worker, it must print the pid of the worker, and the result received from that worker. If you are not using makefile, please include the name of the compiler you are using and any special options needed as comments (along with other traditional comments) at the beginning of your source code.
Explanation / Answer
//Copyable Code:
#include <stdio.h>
#include<iostream>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
using namespace std;
int main(int arga, char *argval[])
{
int PartialSumVal[20]={0},i,processes,p, counter=0;
int pid[20]= {0};
int processesStatus= 0;
int numberOfSlaves1=0;
int id;
string argument1[10];
if((arga-1 % 2) != 0) // CHECK FOR TOTAL NUMBER OF OPERANDS IS EVEN OR ODD
{
argval[arga] = 0; // IF ARGA IS ODD, THEN INCLUDE 0 AS THE LAST OPERAND
arga ++; // AND INCREMENT arga
}
numberOfSlaves1=arga;
for(i=0;i<arga;i++)
{
argument1[i]=argval[i];
}
while(numberOfSlaves1>1)
{
numberOfSlaves1=numberOfSlaves1/2;
p=0;
processes=0;
for(i=0;i<numberOfSlaves1;i++)
{
pid[counter]= fork();
switch(id)
{
case -1: cout<<"SLAVE PROCESS CANNOT BE
CREATED"<<endl;
exit(-1);
case 0: //execlp("work","work",argument1[p],argument1[p+1],NULL);
p+=2;
default:
PartialSumVal[processes]=wait(&processesStatus);
cout<<"THE PID OF THE SLAVE PROCESS:"<<pid[counter++]<<endl;
cout<<"PARTIAL SUM:"<<PartialSumVal[processes++]<<endl;
break;
}
} // END FOR
for(i=0;i<numberOfSlaves1;i++)
{
argument1[i]=PartialSumVal[i];
}
} // END WHILE
if(numberOfSlaves1==1)
cout<<"SUM OF GIVEN INTEGERS:"<<argument1[0]<<endl;
for(i=0;i<counter;i++) // TO KILL ALL SLAVES
{
waitpid(pid[i], NULL, WUNTRACED); // WAIT UNTIL THE CHILD IS STOPPED
kill(pid[i], SIGCONT);
}
return 0;
}
Slave process
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
using namespace std;
//MAIN METHOD
int main(int arga, char *argv1[])
{
//VARIABLE DECLARATION
int v1= atoi(argv1[1]);
int v2= atoi(argv1[2]);
//PRINTING THE RESULTS
cout<<"PID OF THE SLAVE PROCESS:"<<getpid()<<endl;
cout<<"FIRST OPERAND:"<<v1<<endl;
cout<<"SECOND OPERAND: "<<v2<<endl;
cout<<"SUM OF THE OPERANDS:"<<(v1+v2)<<endl;
exit(v1+v2);
}
Feel free to reach out if you have any doubts.
Rate if the answer was helpful.
Thanks
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.